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

Link to comment
Share on other sites

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';
}

?>

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.