Altec Posted January 2, 2009 Share Posted January 2, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/139175-solved-crazy-inifinite-while-loop/ Share on other sites More sharing options...
sasa Posted January 2, 2009 Share Posted January 2, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/139175-solved-crazy-inifinite-while-loop/#findComment-727941 Share on other sites More sharing options...
btherl Posted January 2, 2009 Share Posted January 2, 2009 What is $c{$c + 1} supposed to do? Quote Link to comment https://forums.phpfreaks.com/topic/139175-solved-crazy-inifinite-while-loop/#findComment-727943 Share on other sites More sharing options...
Altec Posted January 2, 2009 Author Share Posted January 2, 2009 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. Sasa, what is different between that and my current code? Quote Link to comment https://forums.phpfreaks.com/topic/139175-solved-crazy-inifinite-while-loop/#findComment-727944 Share on other sites More sharing options...
btherl Posted January 2, 2009 Share Posted January 2, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/139175-solved-crazy-inifinite-while-loop/#findComment-727946 Share on other sites More sharing options...
Mchl Posted January 2, 2009 Share Posted January 2, 2009 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. It doesn't even look like correct syntax to me... And BTW: arrays are much better for such things. Quote Link to comment https://forums.phpfreaks.com/topic/139175-solved-crazy-inifinite-while-loop/#findComment-727947 Share on other sites More sharing options...
Altec Posted January 2, 2009 Author Share Posted January 2, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/139175-solved-crazy-inifinite-while-loop/#findComment-727973 Share on other sites More sharing options...
Altec Posted January 2, 2009 Author Share Posted January 2, 2009 Thanks for all your help guys! I really appreciate it, especially btherl. I read up on variable variables. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/139175-solved-crazy-inifinite-while-loop/#findComment-727987 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.