squiblo Posted September 25, 2009 Share Posted September 25, 2009 this code works well but does not order how i want, i would like the unread to be displayed first and by date order, then the unread displayed by date order, how can i do this? <?php $query = mysql_query("SELECT * FROM mail WHERE pageid='$pageid'"); $checkbox = 1; while ($row = mysql_fetch_assoc($query)){ $subject = $row['subject']; $message = $row['message']; $from = $row['from']; $when = $row['when']; $read = $row['read']; $date = date("d/m/Y", strtotime(" $when +0 year ")); //this is the only way i know of changing date format if ($read == "n") { $readicon = "<img src='./Images/pageimages/unread_icon.png' alt='not read'>"; $startbold = "<b>"; $endbolt = "</b>"; } else $readicon = "<img src='./Images/pageimages/opened_icon.png' alt='read'>"; echo "<tr><td width='40%'><hr width='100%' height='1px' color='#E6E6E6'> <input type='checkbox' name='checkbox$checkbox'>$startbold $readicon - $subject $endbold </td></tr>"; $checkbox++; } echo "<tr><td><hr width='100%' height='1px' color='#E6E6E6'>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/175444-solved-ordering-results-from-db/ Share on other sites More sharing options...
smerny Posted September 25, 2009 Share Posted September 25, 2009 What are the fields in the table and what values do you use to say something is read or unread? edit: figured it out from the rest your code... ORDER BY read, when Quote Link to comment https://forums.phpfreaks.com/topic/175444-solved-ordering-results-from-db/#findComment-924548 Share on other sites More sharing options...
squiblo Posted September 25, 2009 Author Share Posted September 25, 2009 - the date field in the table is called "when". - the value that shows if something has been read is "n" for if it is unread and "y" for if it is read and these values are stored in a field called "read". Quote Link to comment https://forums.phpfreaks.com/topic/175444-solved-ordering-results-from-db/#findComment-924549 Share on other sites More sharing options...
smerny Posted September 25, 2009 Share Posted September 25, 2009 $query = mysql_query("SELECT * FROM mail WHERE pageid='$pageid' ORDER BY read, when"); Quote Link to comment https://forums.phpfreaks.com/topic/175444-solved-ordering-results-from-db/#findComment-924550 Share on other sites More sharing options...
squiblo Posted September 25, 2009 Author Share Posted September 25, 2009 problem is described in the code... <?php $query = mysql_query("SELECT * FROM mail WHERE pageid='$pageid' ORDER BY read, when"); //from changing this line $checkbox = 1; while ($row = mysql_fetch_assoc($query)){ // i get an error on this line saying "supplied argument is not a valid MySQL result" $subject = $row['subject']; $message = $row['message']; $from = $row['from']; $when = $row['when']; $read = $row['read']; $date = date("d/m/Y", strtotime(" $when +0 year ")); //unread = n if ($read == "n") { //unread $readicon = "<img src='./Images/pageimages/unread_icon.png' alt='not read'>"; $startbold = "<b>"; $endbolt = "</b>"; } else //read $readicon = "<img src='./Images/pageimages/opened_icon.png' alt='read'>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/175444-solved-ordering-results-from-db/#findComment-924555 Share on other sites More sharing options...
PFMaBiSmAd Posted September 25, 2009 Share Posted September 25, 2009 read is a reserved mysql keyword. If you truly have a column by that name, you should rename it to something else or you can enclose it in back-ticks everywhere you reference it in a query. Quote Link to comment https://forums.phpfreaks.com/topic/175444-solved-ordering-results-from-db/#findComment-924561 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.