TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   MySQL & Databases (http://www.talkphp.com/mysql-databases/)
-   -   Re-order display of data-base (http://www.talkphp.com/mysql-databases/6291-re-order-display-data-base.html)

Timms 05-24-2012 03:51 PM

Re-order display of data-base
 
I have a table on my site of names and comments that is pulled from a sql data-base. Works perfect you can add comments aswell.

How would i go about displaying it as the last comment that was posted to be on top rather than at the bottom?

my current code for displaying the table is:

PHP Code:

mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

while($rows=mysql_fetch_array($result)){
?>
<table width="100%" border="0">
<tr>
<td><table width="100%" border="0" bordercolor="#FF0000">
<tr>
<td class="commentsname"><? echo $rows['name']; ?>:</td>
<td class="commentstxt"><? echo $rows['comment']; ?></td>
</tr>
</table></td>
</tr>
</table>

which displays perfect only i want the latest comment on top instead of pusshed down to the bottom ;-) help appriciated

tony 05-24-2012 05:40 PM

does your table only has the fields name and comment? you would need an extra field that would hold the date and time when the comment was created then you can order them. For example lets say the field is named created, the sql query would be something like this

sql Code:
SELECT * FROM $tbl_name ORDER BY `created` DESC
the DESC is for descending order, the other option is ASC for ascending order.

I hope this helps.

Timms 05-24-2012 06:53 PM

ye it does actually have a date and time but i did try that with date and time but think i got it wrong. Either way i just did the same as what you did only witht he ID from the db which in my eyes is ideal anyway unless thats something that should not be done never the less it seems to work :D thanks for that :D

Timms 05-25-2012 12:43 PM

ok now i have another problem...

it does order correctly to a certain degree

i have changed the order to a different field now which is an alt date which is put in to a field and subbmited via a form

trouble is the dates get mixed up.

if i have 1,5,2012 in one record
13,5,2012 in the second record
and 1,6,2012 in the third record

1,6,2012 will display as the second record on the table i have which is not what i need becase 1,6,2012 is after 13,5,2012

if you refer to my screenshot for a better description
http://www.chulse.co.uk/date-error.png

you can see friday `1, june 2012 is the 2nd row down which should be the last row. It is not ordering from this date it is ordering from a hidden date which is the same just in a different format. dateFormat: 'd-m-y',

how would i go about getting this in the right order?

Thanks

caiyanfang 03-30-2013 07:54 AM

Nike Free 3.0 V2 : Nike Free Run Shoes
 
Don't worry Additionally there is a as Nike encompasses a line of green shoes for you guessed it-your camera. cheap air jordan shoes In fact however , there is more options easily to get lime green Nike shoes for ladies though all tailored specifically to suit running. Choose shoes involving Nike Air Max Line and also LunarGlide and and the wonderful Kukini Fee wrinkles. You can find full green shoes or see black shoes with green cheap air jordans swoosh and trimmings. Run and train in comfort and style with Nike shoes for women.


All times are GMT. The time now is 10:22 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0