Jump to content

dropdown list from mysql


jaylam13

Recommended Posts

Hi.

 

I am using this script to populate a dropdown list box from sql, it works but does anyone know how to sort the list in alphabetical order?

 

$sql="SELECT * FROM Fish WHERE ***** = '".$_GET['stocktype']."'"; 
$result=mysql_query($sql); 

$options=""; 

while ($row=mysql_fetch_array($result)) { 

    $ID=$row["ID"]; 
    $Stock=$row["Commonn"]; 
    $Options.="<OPTION VALUE=\"$ID\">".$Stock; 
} 


<SELECT NAME='stock1'> 
<OPTION VALUE='$Options'>$Options</option>

</SELECT>

Link to comment
Share on other sites

Yea.

 

$sql="SELECT * FROM Fish WHERE ***** = '".$_GET['stocktype']." order by fish asc;'"; 
$result=mysql_query($sql); 

$options=""; 

while ($row=mysql_fetch_array($result)) { 

    $ID=$row["ID"]; 
    $Stock=$row["Commonn"]; 
    $Options.="<OPTION VALUE=\"$ID\">".$Stock; 
} 


<SELECT NAME='stock1'> 
<OPTION VALUE='$Options'>$Options</option>

</SELECT>

 

That should call the fish in alphabetical order.

Link to comment
Share on other sites

yea, a mistake in the SQL syntax.  Revised:

 

$sql="SELECT * FROM Fish WHERE ***** = '".$_GET['stocktype']."' order by fish asc;"; 
$result=mysql_query($sql); 

$options=""; 

while ($row=mysql_fetch_array($result)) { 

    $ID=$row["ID"]; 
    $Stock=$row["Commonn"]; 
    $Options.="<OPTION VALUE=\"$ID\">".$Stock; 
} 


<SELECT NAME='stock1'> 
<OPTION VALUE='$Options'>$Options</option>

</SELECT>

 

Although, it being an sql error, it would be ideal if you could find it yourself :)

Link to comment
Share on other sites

yea, a mistake in the SQL syntax.  Revised:

 

$sql="SELECT * FROM Fish WHERE ***** = '".$_GET['stocktype']."' order by fish asc;"; 
$result=mysql_query($sql); 

$options=""; 

while ($row=mysql_fetch_array($result)) { 

    $ID=$row["ID"]; 
    $Stock=$row["Commonn"]; 
    $Options.="<OPTION VALUE=\"$ID\">".$Stock; 
} 


<SELECT NAME='stock1'> 
<OPTION VALUE='$Options'>$Options</option>

</SELECT>

 

Although, it being an sql error, it would be ideal if you could find it yourself :)

 

That gave the same error and didnt populate the dropdown box.

 

 

Link to comment
Share on other sites

yea, a mistake in the SQL syntax.  Revised:

 

$sql="SELECT * FROM Fish WHERE ***** = '".$_GET['stocktype']."' order by fish asc;"; 
$result=mysql_query($sql); 

$options=""; 

while ($row=mysql_fetch_array($result)) { 

    $ID=$row["ID"]; 
    $Stock=$row["Commonn"]; 
    $Options.="<OPTION VALUE=\"$ID\">".$Stock; 
} 


<SELECT NAME='stock1'> 
<OPTION VALUE='$Options'>$Options</option>

</SELECT>

 

Although, it being an sql error, it would be ideal if you could find it yourself :)

 

That gave the same error and didnt populate the dropdown box.

 

 

 

Yea, i just noticed what i have done there.

 

Where it says: "order by fish asc;" in the SQL statement, change the word "fish" to what you want the database to order the results by.  As i don't know your columns i cant suggest what one.

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.