Jump to content

"SELECT DISTINCT" -- Sort Order


39corp

Recommended Posts

Hi there Guys

 

I have a small problem with the sort-order when using "SELECT DISTINCT" for a search script. This is what I have:

 

<select name="type" class="textField" id="select2">

<?php

if ($type == ""){

print " <option value=\"0\" selected>All Types</option>";

}else{

print " <option value=\"0\" >All Types</option>";

}

$res = dbsql("SELECT DISTINCT (type) FROM carsz");

while (($row = dbfetch($res)) != NULL) {

$res2 = dbsql("SELECT * FROM type WHERE id = '$row[type]' ORDER BY type ASC");

$row2 = dbfetch($res2);

if ($type == $row2[id]){

print "<option value=\"".$row2[id]."\" selected>".$row2[type]."</option>\n ";

}else{

print "<option value=\"".$row2[id]."\">".$row2[type]."</option>\n ";

}

}

?>

</select>

 

 

Basically there are 2 tables:

1) table with listings

2) table with types

 

So the "type" in table 1 is just an ID that refer back to table 2. But when I use the script the sort order of the different types are not alphabetical.

 

PLEASE HELP

Andy

Link to comment
Share on other sites

Your php Freaks are really fast thanks you are great!!!

 

Could you indicate where at the end to put "Order By" please or simply modify my sample code for me I really would appreciate it. Sorry but I'm new to PHP and rely on guys like you for help. Really appreciate this!!!

 

Andy

Link to comment
Share on other sites

Your php Freaks are really fast thanks you are great!!!

 

Could you indicate where at the end to put "Order By" please or simply modify my sample code for me I really would appreciate it. Sorry but I'm new to PHP and rely on guys like you for help. Really appreciate this!!!

 

Andy

 

ORDER BY should be the last statement in your query, after any WHEREs.

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.