Jump to content

2 separate order by dates


LanceyDavid

Recommended Posts

here is my code

$query = "SELECT * from links ORDER BY field_4 DESC";  // assumes that your date column is a DATE data type so that ordering by it will sort the dates
$result = mysql_query($query);

// check if the query executed without error and if it returned any rows here...

// retrieve and process the data from the query
$last_date = ''; // initialize to a value that will never exist as data
while($row = mysql_fetch_assoc($result)){
    // test for a new date and output the date heading
if($last_date != $row['field_4']){
        echo "<p><font face='Arial, Helvetica, sans-serif'>";
	echo $row['field_4'] . "<br />";
	echo "</font></p>";
        $last_date = $row['field_4']; // remember the new date
    }

    // output the data (under each date heading)
    echo "<img src=" . $row['field_1'] . " border=0>";
echo "<a href='" . $row['field_3'] . "'>";
echo $row['field_2'] . "</a><br />";
}
mysql_close($con);

 

i would like set the data under each heading to be ordered by a different column, how do i do so?

Link to comment
https://forums.phpfreaks.com/topic/222774-2-separate-order-by-dates/
Share on other sites

You would add it to the ORDER BY term in the query -

 

ORDER BY field_4 DESC, your_other_field_here

 

You can add DESC after the second field if you want it to be ordered descending.

 

The above will first order the rows by field_4 DESC, then within each same field_4 value, it will order the rows by the next field you specify.

For my header display, I just have the URL pass a Sortby key, then read it.

 

<? php
if ($_GET['sortby'] == "id") {
$query = 'SELECT * FROM boats ORDER BY bid';
}
elseif ($_GET['sortby'] == "name") {
$query = 'SELECT * FROM boats ORDER BY bname';
}
elseif ($_GET['sortby'] == "status") {
$query = 'SELECT * FROM boats ORDER BY bstatus';
}
elseif ($_GET['sortby'] == "divers") {
$query = 'SELECT * FROM boats ORDER BY bdivers';
}

?>

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.