Jump to content

Simple Post Loop Help


chasethemetal

Recommended Posts

Hey all.

 

I am a php newbie. Just starting to get my feet wet. So my issue is this.

 

I have a MySQL database table called "blog"

 

Within that table are "ID, photo_id, post_head, post_body"

 

I have connected to the database via php and have been successfully echoing the fields onto my page.

 

But the problem is that I don't know how to create a loop. I need it to show the latest post (biggest ID #) first then display descending down.

 

Here is my code. As you can see I'm echoing the same variables and logically its displaying the SAME post 2 times. Any help would be just great!

 

<table width="925" border="0" cellspacing="0" cellpadding="0">

      <tr>

       

        <?

$data = mysql_query("SELECT * FROM blog ORDER BY id DESC") or die(mysql_error());

 

while($info = mysql_fetch_array( $data ))

 

 

{

 

$id = $info['ID'];

$photo_id = $info['photo_id'];

$post_head = $info['post_head'];

$post_body = $info['post_body'];

 

 

        }

?>

       

        <!-- POST 1 START -->

       

        <td width="180">

       

        <img src="<? echo $photo_id ?>" />

       

        </td>

       

        <td id="box_bg2" class="padding_state" width="275" valign="top">

        <br />

        <font id="post_head">

        <? echo $post_head ?>

        </font>

       

        <br />

       

        <font id="post_body">

        <? echo $post_body ?>

        </font>

       

        </td>

       

        <td width="15"> </td>

       

        <!-- POST 2 START -->

       

     

               

        <td width="180">

       

        <img src="<? echo $photo_id ?>" />

       

        </td>

       

        <td id="box_bg2" class="padding_state" width="275" valign="top">

        <br />

        <font id="post_head">

        <? echo $post_head ?>

        </font>

       

        <br />

       

        <font id="post_body">

        <? echo $post_body ?>

        </font>

     

        </td>

      </tr>

    </table>

Link to comment
https://forums.phpfreaks.com/topic/232335-simple-post-loop-help/
Share on other sites

Your while loop is already there, and that's where some or all of the action can take place. Either you can echo each individual item, or you can store them in an array for later echoing. The way it's looking now echoing immediately is probably the simplest approach.

 

Just put an echo statement containing those variables inside the while loop you already created, and format them as you see fit

 

ie:

 

echo "<tr><td>Message ID: ". $id ." </td><td>". $post_head . "<br>". $post_body . "</td></tr>";

 

or something crazy like that.

 

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.