NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page PHP Problems

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 07-05-2006, 06:49 AM THREAD STARTER               #1 (permalink)
NamePros Member
Join Date: Nov 2005
Posts: 66
nigelwong is an unknown quantity at this point
 



PHP Problems


Hello,

On my site, it consists of a few PHP scripts. This is how it works:
Loads functions.php
Loads a php include, which contains a function that functions.php needs to process. However, within that function contains another variable, which another script needs to process (done afterwards).

The problem is, since the function requested the processing of functions.php, but the variable inside the function has not yet loaded, it does not work properly.

Ill try and make it more simple:
????: NamePros.com http://www.namepros.com/programming/213670-php-problems.html
<?php include functions.php ?> (Function contains $id function processing)
<?php include content.php ?>

Content.php contains $id=$value. Function needs to process $id, but $value still has not been processed yet, therefore it does not work properly. Later in content.php it processes the value, but it has already asked functions to process the id, therefore the value is still unknown.

I've tried ob_start, but it doesnt work.

Any ideas? Thanks
nigelwong is offline  
Old 07-05-2006, 09:47 AM   #2 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,069
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
ob_start only stops output being sent to the browser.

If a function requires a variable and that variable is not available yet then you shouldn't be calling it.

Why are you calling it before you are ready?
Peter is offline  
Old 07-05-2006, 09:18 PM THREAD STARTER               #3 (permalink)
NamePros Member
Join Date: Nov 2005
Posts: 66
nigelwong is an unknown quantity at this point
 



Thats why i am asking how do you delay calling it?

Actually i am not entirely sure if its the function that's not ready, or mysql playing tricks on me. If you set the variable down, function abc(x) , the mysql statment:
$query=mysql_query("SELECT total_votes, total_value, used_ips FROM ratings WHERE id=x ")or die(" Error: ".mysql_error()); works fine.

However, if you put a variable in, (function abc($x); ), the mysql statement

$query=mysql_query("SELECT total_votes, total_value, used_ips FROM ratings WHERE id='$x' ")or die(" Error: ".mysql_error());

doesnt work.

However later on in the script, you ask it to <?php echo $x ?>, $x echos.

Does anyone have any ideas?
nigelwong is offline  
Old 07-05-2006, 09:27 PM   #4 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
Uh.. function abc(x) wouldn't do anything and probably give you an error.

function abc($x) is correct and does not add quotes unless the quotes are in the variable and it should work.

I'm still not completely sure what you are doing and what you are trying to do..
Dan is offline  
Old 07-05-2006, 10:11 PM THREAD STARTER               #5 (permalink)
NamePros Member
Join Date: Nov 2005
Posts: 66
nigelwong is an unknown quantity at this point
 



Well here is whats happening:

If i have
<?php abc('1') ?> run through the CMS it works

if i have
<?php abc('$x') ?> run through the CMS, it Doesnt Work

it has a $output = str_replace("$x", "news_arr[0]", "$output");
in the script which replaces the $x, but the function has already been called, and it is sending $x without the processed variable.

Therefore, i need a way to delay the function, even when it is called.

Okay this is the script:

Functions.php:
PHP Code:
<?php
function rating_bar($id) { 
// this is to get the current rating(s) and display it visually

//Connect to Database
    
$dbhost 'localhost';
    
$dbuser 'xxxx';
    
$dbpass 'xxxxx';
    
$dbname 'xxxxx';
    
$conn mysql_connect($dbhost$dbuser$dbpass) or die  ('Error connecting to mysql');
    
mysql_select_db($dbname);
    
$tableName="ratings";

//id of the thing you want to rate

$query=mysql_query("SELECT total_votes, total_value, used_ips FROM ratings WHERE id='$id' ")or die(" Error: ".mysql_error());
while (
$numbers=mysql_fetch_assoc($query));
$count2=$numbers["total_votes"];//how many votes total
$current_rating=$numbers['total_value'];//total number of rating added together and stored
$tense=($count2==1) ? "vote" "votes";//plural form votes/vote
$test=1;

$ip $_SERVER['REMOTE_ADDR'];

$result=mysql_query("SELECT count(*) FROM $tableName WHERE used_ips LIKE '%".$ip."%' AND id='$id' "); //Pattern match ip:suggested by Bramus! [url]http://www.bram.us/[/url] - this variable searches through the previous ip address that have voted and returns true or false
$voted mysql_result($result00); 

mysql_close($conn);

// draw the rating bars
// still to do: need to not allow them to vote if they've already been here and voted

?>

        <div id="unit_long<?php echo $id ?>">
        <ul class="unit-rating"">
        <li class='current-rating' style="width:<?php echo $count2 ?>px;">Currently <?php echo @number_format($current_rating/$count2,2); ?>/5</li>
????: NamePros.com http://www.namepros.com/showthread.php?t=213670
            <?php
            
for ($ncount 1$ncount <= 5$ncount++) { ?>
                <li><a href="#" title="<?php echo $ncount ?> out of 5" class="r<?php echo $ncount ?>-unit" onClick="javascript:sndReq('<?php echo $ncount ?>','<?php echo $id ?>','<?php echo $ip ?>')"><?php echo $ncount ?></a></li>
        <?    }
            
$ncount=0// resets the count
            
?>
        </ul>
        </div>
<?
}
?>
Article:
PHP Code:
<?php rating_bar('{news-id}'); ?>
Part of the Output File:
PHP Code:
$output str_replace("{news-id}"$news_arr[0], $output); 
????: NamePros.com http://www.namepros.com/showthread.php?t=213670
Last edited by nigelwong; 07-05-2006 at 10:27 PM.
nigelwong is offline  
Old 07-06-2006, 03:10 AM THREAD STARTER               #6 (permalink)
NamePros Member
Join Date: Nov 2005
Posts: 66
nigelwong is an unknown quantity at this point
 



Update: Somebody told me i have to use the $globals command, but how would i do it?
nigelwong is offline  
Old 07-06-2006, 04:17 AM   #7 (permalink)
tm
Senior Member
 
tm's Avatar
Join Date: Nov 2005
Location: on a oil rig just off Ireland
Posts: 1,408
tm is a glorious beacon of lighttm is a glorious beacon of lighttm is a glorious beacon of lighttm is a glorious beacon of lighttm is a glorious beacon of light
 



<?php abc('$x') ?> won't work. Try <?php abc("$x") ?> or <?php abc($x) ?>.
__________________
You design in photoshop, I code into valid XHTML/CSS.
Professional PSD, PNG or HTML to tableless XHTML/CSS designs.
For more info, send me a PM.
tm is offline  
Old 07-06-2006, 04:46 AM   #8 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
You don't put quotes when you use a variable inside a function.

abc($x)
Dan is offline  
Old 07-06-2006, 07:44 AM   #9 (permalink)
NamePros Member
Join Date: Mar 2006
Location: USA - RI
Posts: 49
Horranus is an unknown quantity at this point
 



Originally Posted by nigelwong
Update: Somebody told me i have to use the $globals command, but how would i do it?

A global command tells the function that the variable you set inside the function should be available to the rest of the script.

PHP Code:

function yada()
{
  global 
$abc
  $abc 
123
}

echo 
$abc 
The above code would allow you to use $abc outside of the function;
The below code would not

????: NamePros.com http://www.namepros.com/showthread.php?t=213670
PHP Code:

function yada()
{
 
$abc 123
}

echo 
$abc 
The top set of code would output 123, the bottom would give you an error because you did not tell the function to make the variable accessible to the rest of the script and / or other functions.

Hope that helps.
Horranus is offline  
Old 07-06-2006, 08:41 PM THREAD STARTER               #10 (permalink)
NamePros Member
Join Date: Nov 2005
Posts: 66
nigelwong is an unknown quantity at this point
 



Am i supposed to put the function script inside the same php file as my output script? Cause currently they are both php included on the page.

I dont think the globals command will help. Here is a more detailed rundown of whats happening:


The functions script is php included onto my page (script posted in previous post)

A template for my CMS is stored like this:
<div>
{title}
{story}
<?php ranking('{news-id}')?> (ive removed the ' ' but i get Parse error: syntax error, unexpected '{', expecting ')' )
</div>

In the output script, it loads news.txt file, then the template.
It looks in the template for {title}, {news-id} etc. then does a series of $output = str_replace("{news-id}", "news_arr[0]", "$output");
????: NamePros.com http://www.namepros.com/showthread.php?t=213670
then echo's $output.

Hopefully what i wanted it to echo the output like:
<div>
Title Goes here
Story Goes here
<?php ranking('0001')?>
</div>
Last edited by nigelwong; 07-06-2006 at 08:55 PM.
nigelwong is offline  
Old 07-06-2006, 08:44 PM   #11 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
As long as you include the function, you can run it.

You don't need to do anything with global.
Dan is offline  
Old 07-06-2006, 09:36 PM THREAD STARTER               #12 (permalink)
NamePros Member
Join Date: Nov 2005
Posts: 66
nigelwong is an unknown quantity at this point
 



Ok im making abit of process.

I have this:
<?php rating_bar($newsid) ?>

And my function:
PHP Code:
<?php
????: NamePros.com http://www.namepros.com/showthread.php?t=213670
function rating_bar($id) { 

// this is to get the current rating(s) and display it visually

//Connect to Database
    
$dbhost 'localhost';
    
$dbuser 'xx';
    
$dbpass 'x';
    
$dbname 'xx';
    
$conn mysql_connect($dbhost$dbuser$dbpass) or die  ('Error connecting to mysql');
    
mysql_select_db($dbname);
    
$tableName="ratings";

//id of the thing you want to rate

$query=mysql_query("SELECT total_votes, total_value, used_ips FROM ratings WHERE id='$id' ")or die(" Error: ".mysql_error());
while (
$numbers=mysql_fetch_assoc($query));
$count=$numbers["total_votes"];//how many votes total
????: NamePros.com http://www.namepros.com/showthread.php?t=213670
$current_rating=$numbers['total_value'];//total number of rating added together and stored
$tense=($count2==1) ? "vote" "votes";//plural form votes/vote
$test=1;

$ip $_SERVER['REMOTE_ADDR'];

$result=mysql_query("SELECT count(*) FROM $tableName WHERE used_ips LIKE '%".$ip."%' AND id='$id' "); //Pattern match ip:suggested by Bramus! http://www.bram.us/ - this variable searches through the previous ip address that have voted and returns true or false
$voted mysql_result($result00); 

mysql_close($conn);

// draw the rating bars
// still to do: need to not allow them to vote if they've already been here and voted


?>

        <div id="unit_long<?php echo $id ?>">
        <ul class="unit-rating"">
        <li class='current-rating' style="width:<?php echo @number_format($current_rating/$count,2)*25?>px;">Currently <?php echo @number_format($current_rating/$count,2); ?>/5</li>
            <?php
            
for ($ncount 1$ncount <= 5$ncount++) { ?>
                <li><a href="#" title="<?php echo $ncount ?> out of 5" class="r<?php echo $ncount ?>-unit" onClick="javascript:sndReq('<?php echo $ncount ?>','<?php echo $id ?>','<?php echo $ip ?>')"><?php echo $ncount ?></a></li>
        <?    }
            
$ncount=0// resets the count
            
?>
        </ul>
        </div>
<?
}
?>
However, the only line that the script can process properly is:
<div id="unit_long<?php echo $id ?>">

This line won't work:
<?php echo @number_format($current_rating/$count,2)*25; ?>

If i have something like $count
<div id="unit_long<?php echo $count ?>">

it outputs nothing.
Last edited by nigelwong; 07-06-2006 at 09:54 PM.
nigelwong is offline  
Old 07-06-2006, 09:40 PM   #13 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
<?php ratings($newsid) ?>..

There is no function called ratings.. it is rating_bar

<?php rating_bar($newsid); ?>
Dan is offline  
Old 07-06-2006, 09:54 PM THREAD STARTER               #14 (permalink)
NamePros Member
Join Date: Nov 2005
Posts: 66
nigelwong is an unknown quantity at this point
 



Sorry that was a typo.

But if i had something like:
<?php
function rating_bar($id2) {
echo $id2
}
?>

It would work

However, with all the sql stuff, it doesn't work ($count etc.).
nigelwong is offline  
Old 07-06-2006, 10:01 PM   #15 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
So that means you coded the function wrong..

Doesn't have anything to do with calling it.
Dan is offline  
Old 07-06-2006, 10:11 PM THREAD STARTER               #16 (permalink)
NamePros Member
Join Date: Nov 2005
Posts: 66
nigelwong is an unknown quantity at this point
 



It was coded right, i typed it in my post wrong.

I now found out its my SQL Statement:

This works:
$query=mysql_query("SELECT total_votes, total_value, used_ips FROM ratings WHERE id=1152076887")

This doesnt.
$query=mysql_query("SELECT total_votes, total_value, used_ips FROM ratings WHERE id='$id'")

Even though if you echo $id in the function, it gives you 1152076887
nigelwong is offline  
Old 07-06-2006, 10:34 PM   #17 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
$query=mysql_query("SELECT total_votes, total_value, used_ips FROM ratings WHERE id=$id")

You don't put quotes around a number. ($id)
Dan is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 08:43 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger