Help With <select></select>

Discussion in 'PHP' started by minhazikram, Feb 2, 2012.

  1. #1
    I have a database table which have around 106 numbers of id. I want to display the id in a selected list

    my code is given below:

    // connection open string

    <select>
    <?php
    $list = array();
    $query1 = "SELECT id FROM brandname";
    $doquery1 = mysql_query($query1,$connect);
    while($result = mysql_fetch_array($doquery1))
    {
    $list[] = $result; //
    }

    foreach($list as $lists)
    {
    echo "<option>$lists</option>";
    }

    ?>

    </select>

    // connection close string

    i am inserting all id in the array but when i echo it, the result comes out like:
    ARRAY,ARRAY.

    how can i print my array elements in the droplist of <select>
     
    Last edited: Feb 2, 2012
    minhazikram, Feb 2, 2012 IP
  2. RamMurti

    RamMurti Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you need to change your echo statement as : echo "<option>".$lists."</option>";
     
    RamMurti, Feb 2, 2012 IP
  3. frank007

    frank007 Well-Known Member

    Messages:
    160
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    123
    #3

    Hello,

    You got array there in $lists variable thats why you are getting ARRAY in output. See here:

    $result is an ARRAY and you are saving ARRAY in $list variable. Now

    Here you are printing each element of $list which contains all ARRAY elements :) so better make it like:

    Thank you
     
    frank007, Feb 2, 2012 IP
  4. inmonarch2012

    inmonarch2012 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    hi you can just do that
    <select name='myname'>
    <?
    $query1 = "SELECT id FROM brandname";
    $doquery1 = mysql_query($query1,$connect);
    while($result = mysql_fetch_array($doquery1))
    {
    echo "<option >$result['id']</option>":
    }
    ?>
    </select>
     
    inmonarch2012, Feb 3, 2012 IP
  5. pukhtoogle

    pukhtoogle Member

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #5
    100% accurate, i had already done this job 5, 6 times before, this is the most simple and fast method, no need to put it in array and then grabbing back from array.
     
    pukhtoogle, Feb 5, 2012 IP
  6. SingaHost

    SingaHost Guest

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    agree with this. just call it directly from the DB and throw it as a select.
    I'll only put it in array if i wanna manipulate the select values.
     
    SingaHost, Feb 5, 2012 IP