Jump to content

[SOLVED] Advice on mysql php while


DamienRoche

Recommended Posts

I have a table with some information in...obvioulsy. Want I want to do is present this information, one line at a time, in a particular order (by timestamp).

 

I've searched for php while and mysql tuts but can't find what I'm looking for.

 

Any one know how I would go about this?

 

Something like:

 

->order entries by timestamp (earliest to latest)

->loop through entry

->for each entry (can't use foreach) - echo entry then <br>

 

Any help much appreciated. Thanks.

 

Link to comment
Share on other sites

Some code that you can adapt: 

 

<?php

include('inc-dbconnect.php');

//store the SQL query in the result variable
$result = mysql_query("SELECT * FROM Reviews ORDER BY ReviewDate DESC");

if(!$result) die("Query Failed.");

if(mysql_num_rows($result))
{
echo ("<table border='1' cellpadding='3' cellspacing='0'>");
echo ("<tr><td>Review Id</td><td>Review Rating</td><td>Review Desc</td><td>Review Date</td></tr>");

//output as long as there are still available fields
//while($row = mysql_fetch_row($result))

//associative array allows you to use field names to get results
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

/*  
$row[0] now contains the first column of the current row,
index 1 is the second, etc.
*/


echo ("<tr>");
echo '<td>' . $row[ReviewId] . '</td>';
echo '<td>' . $row[ReviewRating] . '</td>';
echo '<td>' . nl2br($row[ReviewDesc]) . '</td>';
echo '<td>' . $row[ReviewDate] . '</td>';
echo ("</tr>");
}
}
//if no fields exist
else
{
echo ("There is nothing in the database");
}

echo ("</table>");
//echo ("Username: $username ");

mysql_close(); 
?>

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.