Jump to content

[SOLVED] Crazy Inifinite While Loop


Altec

Recommended Posts

Basically, I have a file which contains an HTML template, with {TAGS}. I'm trying to loop through the results from a query, process the file into a variable, replace the current result into the file, and append it to $comm_d, which will later replace the {CONTENT} tag in my main template file.

 

Kinda confusing, but here is the code I have right now:

$comm_f = file_get_contents('style/comm_template.html');

$query = "SELECT * FROM `{$comm_conn->table_s}` WHERE `public` = 'true' ORDER BY `timestamp` DESC";

while($d = mysql_fetch_assoc($comm_conn->query($query))) {
static $c = 0;
$comm_tags = array(
		'{COMM_TITLE}',
		'{COMM_DATE}',
		'{COMM_POSTER}',
		'{COMM_BODY}',
	);
$comm_data = array(
		$d['title'],
		date('m/d/y',$d['timestamp']),
		$d['poster'],
		$d['text'],
	);
$c{$c + 1} = str_replace($comm_tags,$comm_data,$comm_f); // LINE 30
$comm_d .= $c{$c + 1};
$c++;
}
$body_f = str_replace('{CONTENT}',$comm_d,$body_f);

Kinda crazy, I know, but I get an infinite listing of:

Warning: Cannot use a scalar value as an array in index.php on line 30

Line 30 is marked with a comment in the above code.

Link to comment
https://forums.phpfreaks.com/topic/139175-solved-crazy-inifinite-while-loop/
Share on other sites

try

$comm_f = file_get_contents('style/comm_template.html');

$query = "SELECT * FROM `{$comm_conn->table_s}` WHERE `public` = 'true' ORDER BY `timestamp` DESC";
$res = $comm_conn->query($query);
while($d = mysql_fetch_assoc($res)) {
static $c = 0;
...

you query database again in each loop

What is $c{$c + 1} supposed to do?

It should take the value of $c and add one to it, then append that to $c, so the first iteration would be $c1, second $c2, and so forth. I believe it is an evaluated variable name. :P

 

Sasa, what is different between that and my current code?

Aha.  $c{$c + 1} actually takes the $c+1'th character of the string $c, which doesn't make much sense at all, and that's why php gives you a warning.  You can do this:

 

$cname = "c" . ($c + 1);
$$cname = 'blah';

 

The double $$ does the "evaluated variable name" thing (aka "variable variables").

 

Also do what sasa suggested .. otherwise you are re-executing the sql every time around the loop, which will have you loop forever.

It should take the value of $c and add one to it, then append that to $c, so the first iteration would be $c1, second $c2, and so forth. I believe it is an evaluated variable name. :P

 

It doesn't even look like correct syntax to me... And BTW: arrays are much better for such things.

Aha.  $c{$c + 1} actually takes the $c+1'th character of the string $c, which doesn't make much sense at all, and that's why php gives you a warning.  You can do this:

 

$cname = "c" . ($c + 1);
$$cname = 'blah';

 

The double $$ does the "evaluated variable name" thing (aka "variable variables").

 

Also do what sasa suggested .. otherwise you are re-executing the sql every time around the loop, which will have you loop forever.

I'm confused as to "variable variables." In your example, $$cname would evaluate to what? Whatever $c+1 is?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.