patriotfan Posted April 19, 2009 Share Posted April 19, 2009 Hi. I'm working on a simple, basic CMS. I have come upon an major error though. For some reason, this: $result = mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT 3"); while($row = mysql_fetch_array($result)) { $blog_title = $row['post_title']; $blog_title = $blog_title .''. $space; $blog_da = $by .''. $row['post_author']; $blog_da = $blog_da .''. $on; $blog_da = $blog_da .''. $row['post_date']; $blog_text = $row['post_text']; } is only displaying 1 row while I have 3 in the database. The demo is here: http://e4.pfmashup.co.cc/ Quote Link to comment https://forums.phpfreaks.com/topic/154702-displaying-only-1-but-i-need-3/ Share on other sites More sharing options...
JasonLewis Posted April 19, 2009 Share Posted April 19, 2009 Are you echoing it in the loop? Quote Link to comment https://forums.phpfreaks.com/topic/154702-displaying-only-1-but-i-need-3/#findComment-813484 Share on other sites More sharing options...
patriotfan Posted April 19, 2009 Author Share Posted April 19, 2009 Are you echoing it in the loop? It gets displayed later $page_content = str_replace("!!blog_title!!", $blog_title, $page_content); $page_content = str_replace("!!blog_da!!", $blog_da, $page_content); $page_content = str_replace("!!blog_text!!", $blog_text, $page_content); Quote Link to comment https://forums.phpfreaks.com/topic/154702-displaying-only-1-but-i-need-3/#findComment-813485 Share on other sites More sharing options...
DarkSuperHero Posted April 19, 2009 Share Posted April 19, 2009 the only problem i see is, when you go to use the viriables in which you have stored your returned rows....you only have one "instance" which was overridden 2 times... <?php $result = mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT 3"); while($row = mysql_fetch_array($result)) { //variables set inside this if, will be overwritten every time the loop iterates. $blog_title = $row['post_title']; $blog_title = $blog_title .''. $space; $blog_da = $by .''. $row['post_author']; $blog_da = $blog_da .''. $on; $blog_da = $blog_da .''. $row['post_date']; $blog_text = $row['post_text']; } try <?php $result = mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT 3"); while($row = mysql_fetch_array($result)) { //variables set inside this if, will be overwritten every time the loop iterates. $blog_title[] = $row['post_title'] .''. $space; $blog_da[] = $by .''. $row['post_author'] .''. $on .''. $row['post_date']; $blog_text[] = $row['post_text']; } //then you can loop through the resulting arrays for your information... Quote Link to comment https://forums.phpfreaks.com/topic/154702-displaying-only-1-but-i-need-3/#findComment-813486 Share on other sites More sharing options...
patriotfan Posted April 19, 2009 Author Share Posted April 19, 2009 the only problem i see is, when you go to use the viriables in which you have stored your returned rows....you only have one "instance" which was overridden 2 times... try <?php $result = mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT 3"); while($row = mysql_fetch_array($result)) { //variables set inside this if, will be overwritten every time the loop iterates. $blog_title[] = $row['post_title'] .''. $space; $blog_da[] = $by .''. $row['post_author'] .''. $on .''. $row['post_date']; $blog_text[] = $row['post_text']; } //then you can loop through the resulting arrays for your information... Now I get literally 3 arrays Quote Link to comment https://forums.phpfreaks.com/topic/154702-displaying-only-1-but-i-need-3/#findComment-813530 Share on other sites More sharing options...
.josh Posted April 19, 2009 Share Posted April 19, 2009 obviously. The way you had it before, every time the loop iterates, your previous values get overwritten by the new ones, so when you go to display it later, you only have the last entry. So you put them into an array, so that each value is saved in an element of the array. When you get to the code where you actually display it, you have to loop through the arrays. Quote Link to comment https://forums.phpfreaks.com/topic/154702-displaying-only-1-but-i-need-3/#findComment-813579 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.