Results 1 to 7 of 7

Thread: Print as menu (Mysql)

  1. #1
    Join Date
    Jul 2008
    Posts
    81
    Thanks
    38
    Thanked 2 Times in 2 Posts

    Default Print as menu (Mysql)

    Hi,
    I have a simple databse with few columns, 2 of them are: "id" and "name"

    I want to print the database values as a menu:
    <select>
    <option value="the id value">the name value</option>
    <option value="the id value">the name value</option>
    etc.
    </select>

    How can I do it?

    Thanks!

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    assuming you have a server-side language and your host supports it, you can do a loop through the results and dynamically create the dropdown select list... I will demonstrate in PHP

    Code:
    $link = mysqli_connect(host,username,password,database);
    
    if($query = mysqli_query('SELECT fields FROM table WHERE condition', $link))
    {
        while($rs = mysqli_fetch_array($query))
        {
            echo '<option value="'.$rs["ID"].'">'.$rs["VALUE"].'</option>';
        }
    }
    where the blue values correspond to your specific information...

    Also something to note is that its generally advised to separate the code from the output, so if you have some type of templating system, that would be best.

  3. #3
    Join Date
    Jul 2008
    Posts
    81
    Thanks
    38
    Thanked 2 Times in 2 Posts

    Default

    Thank you very much!!
    I'm just new in SQL and it wrote me some error, which I don't understand:

    I wrote this:

    PHP Code:
     if($query mysqli_query('SELECT * FROM `my_categories`'$link)) 
    And what it wrote was:

    PHP Code:
    Warningmysqli_query() expects parameter 1 to be mysqlistring given 

  4. #4
    Join Date
    Jul 2007
    Location
    Azerbaijan, Baku
    Posts
    144
    Thanks
    11
    Thanked 27 Times in 25 Posts

    Default

    boogyman, I have never seen mysqli. What is it? Or it must be mysql?

  5. #5
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    MySQL Lite

  6. #6
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by lord22 View Post
    Thank you very much!!
    I'm just new in SQL and it wrote me some error, which I don't understand:

    I wrote this:

    PHP Code:
     if($query mysqli_query('SELECT * FROM `my_categories`'$link)) 
    And what it wrote was:

    PHP Code:
    Warningmysqli_query() expects parameter 1 to be mysqlistring given 
    sorry I havent use the actual function in a while, I switched the order, should be
    PHP Code:
    if($query mysqli_query($link,'SELECT * FROM `my_categories`', )) 
    Quote Originally Posted by allahverdi View Post
    boogyman, I have never seen mysqli. What is it? Or it must be mysql?
    MySQLi stands for mysql improved. Basically its a newer version of mysql

    see http://forge.mysql.com/wiki/Converting_to_MySQLi or http://www.johnjawed.com/benchmarks/ for more information

  7. #7
    Join Date
    Jul 2008
    Posts
    81
    Thanks
    38
    Thanked 2 Times in 2 Posts

    Default

    Thank You Very Much!!

Bookmarks

Posting Permissions

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