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

 

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.