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
https://forums.phpfreaks.com/topic/245896-dropdown-list-from-mysql/
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.

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 :)

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

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.