Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How to alphabetize a dropbox from sql

    Hi,

    I really new to Php and I'm probably over my head, haha.

    I'm trying to have a drop box from a SQL database that is organized in alphabetical order. Here is my code for php:

    Code:
    $sql_unit="SELECT UnitID, UnitName FROM unit";
    $result_unit=mysql_query($sql_unit);
    
    $unit_options="";
    
    while ($row=mysql_fetch_array($result_unit)) 
    	{
        $unitid=$row["UnitID"];
        $unitname=$row["UnitName"];
        $unit_options.="<OPTION VALUE=\"$unitid\">".$unitname;
    	}
    And the html code below it

    Code:
    	<form>
    		<select name="remove_unit">
    		<option><?php echo $unit_options?></option>
    		</select>
    		<input type="button" value="Remove" />
    	</form>
    But here is the thing, I need to be able to remove the selected item from the database. Which I don't know how to do....what how I need to structure this?

    Thanks!

  • #2
    New Coder
    Join Date
    Oct 2008
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Okay I solved the first part.

    I changed
    $sql_unit="SELECT UnitID, UnitName FROM unit";

    to
    $sql_unit="SELECT UnitID, UnitName FROM unit ORDER BY UnitName";


    But...what information do I need to remove that selection from the database?

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP Code:
    if ( isset( $_POST['remove_unit'] ) )
    {
        
    $sql 'DELETE FROM `unit` WHERE `UnitID` = ' . ( int ) $_POST['remove_unit'] . ' LIMIT 1';
        
    mysql_query$sql );


  • #4
    New Coder
    Join Date
    Oct 2008
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks, I had to take out some (')s to make it work but it got the job done.

  • #5
    Regular Coder
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    528
    Thanks
    8
    Thanked 8 Times in 8 Posts
    Just sticking my nose in...

    You shouldn't have had to remove any of the single quotes, as kbluhm's code was perfectly correct. You did notice the difference between the single quote and the `? - not sure what ` is called lol.

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Haha... good question. Backtick?

    I was curious as to what needed removing as well.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •