Jump to content

Recommended Posts

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.

sc85g9.png

 

The demo is here: http://e4.pfmashup.co.cc/

Link to comment
https://forums.phpfreaks.com/topic/154702-displaying-only-1-but-i-need-3/
Share on other sites

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

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

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

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.

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.