Unstoppable Domains

PHP Problems

Spaceship Spaceship
Watch

nigelwong

Established Member
Impact
3
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:
<?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
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
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?
 
0
•••
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?
 
0
•••
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..
 
0
•••
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:
<?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($result, 0, 0); 

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>
			<?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:
<?php rating_bar('{news-id}'); ?>

Part of the Output File:
PHP:
$output = str_replace("{news-id}", $news_arr[0], $output);
 
Last edited:
0
•••
Update: Somebody told me i have to use the $globals command, but how would i do it?
 
0
•••
<?php abc('$x') ?> won't work. Try <?php abc("$x") ?> or <?php abc($x) ?>.
 
0
•••
You don't put quotes when you use a variable inside a function.

abc($x)
 
0
•••
nigelwong said:
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:
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

PHP:
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.
 
0
•••
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");
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:
0
•••
As long as you include the function, you can run it.

You don't need to do anything with global.
 
0
•••
Ok im making abit of process.

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

And my function:
PHP:
<?php
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
$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($result, 0, 0); 

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:
0
•••
<?php ratings($newsid) ?>..

There is no function called ratings.. it is rating_bar

<?php rating_bar($newsid); ?>
 
0
•••
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.).
 
0
•••
So that means you coded the function wrong..

Doesn't have anything to do with calling it.
 
0
•••
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
 
0
•••
$query=mysql_query("SELECT total_votes, total_value, used_ips FROM ratings WHERE id=$id")

You don't put quotes around a number. ($id)
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Appraise.net
Unstoppable Domains
Domain Recover
DomainEasy โ€” Live Options
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back