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
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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.