Forum Moderators: coopster

Message Too Old, No Replies

Ordering a query's results

running a query for one value but having the result ordered by another

         

shimeal

9:07 pm on Jan 9, 2006 (gmt 0)

10+ Year Member



Hello,

I have two fields in my table (brand and type). The following query displays results based on brand (which is perfect). However, I ALSO need the query to organize the results by type, not by brand. IOW, a brand is selected and the data that is returned is based upon this selected brand but is ordered into the two different types (i.e. all of the brand data pertinent to Type 1 is displayed first and all of the brand data pertinent to Type 2 is displayed second). Can someone tell me what I am missing from the following query to make this happen?

<select name="brand_id" onchange="go(this);">
<option value=""></option>
<?
$sql = "SELECT brand_id, brand FROM cart_brand ORDER BY brand";
$result = mysql_query($sql, $db);
while($row = mysql_fetch_assoc($result)) {
echo " <option value=\"content_update.php?brand=".$row['brand_id']."\">".stripslashes($row['brand'])."</option>\n";
}
mysql_free_result($result);
?>
</select>

Maybe I could put in a second query that would enable the user to choose between Type 1 and Type 2 (similar to the query ran above to choose the brand) but the answer that the query returns (i.e. by sorting based on Type 1 or Type 2) would have to still be contained within the previously-chosen brand?

coopster

9:41 pm on Jan 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Just change your ORDER BY clause to ORDER BY type instead of brand when you get the brand from the POSTed data.