Two different drop downs from mySQL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • knoak

    Two different drop downs from mySQL

    Hi there,

    I have a admin area for a website, and on some part the admin can
    select options from two different drop down boxes. Now i thought that
    it would be better to use one Query for both dropdowns. How can i get
    them together?
    I now have something similair twice:

    <select name="series_id " class="dropdown box" id="series_id" >
    <option value="0">- select -</option>

    <?php
    $db = mysql_connect(" localhost", "name", "pass");
    mysql_select_db ("db_name");
    $result = mysql_query("SE LECT * FROM series ORDER BY name");

    if ($data = mysql_fetch_arr ay($result)){
    do {
    $current_id = $data['series_id'];
    if($current_id == $id){$selected = "selected"; };
    echo "<option value=\"".$data['series_id']."\"
    ".$selected.">" .ucfirst($data['name'])."</option>\n";
    } while ($data = mysql_fetch_arr ay($result));
    } else {
    echo "";
    };
    mysql_close($db );?>
    </select>


    I hope it's clear what i mean. I do not make connection twice, but the
    rest is similair except for the tablename of course.

    Thanks in advance..

    Greetings knoakske
  • Andy Hassall

    #2
    Re: Two different drop downs from mySQL

    On 21 Jan 2005 03:32:55 -0800, knoakske@hotmai l.com (knoak) wrote:
    [color=blue]
    >I have a admin area for a website, and on some part the admin can
    >select options from two different drop down boxes. Now i thought that
    >it would be better to use one Query for both dropdowns. How can i get
    >them together?
    >I now have something similair twice:
    >
    ><select name="series_id " class="dropdown box" id="series_id" >
    > <option value="0">- select -</option>
    >
    > <?php
    > $db = mysql_connect(" localhost", "name", "pass");
    > mysql_select_db ("db_name");
    > $result = mysql_query("SE LECT * FROM series ORDER BY name");
    >
    > if ($data = mysql_fetch_arr ay($result)){
    > do {
    > $current_id = $data['series_id'];
    > if($current_id == $id){$selected = "selected"; };
    > echo "<option value=\"".$data['series_id']."\"
    > ".$selected.">" .ucfirst($data['name'])."</option>\n";
    > } while ($data = mysql_fetch_arr ay($result));
    > } else {
    > echo "";
    > };
    > mysql_close($db );?>
    ></select>
    >
    >I hope it's clear what i mean. I do not make connection twice, but the
    >rest is similair except for the tablename of course.[/color]

    If it's a different table, it's a different query, so just run two queries.
    You should probably factor out the other code into a function though, so you're
    not repeating that - i.e. make it take parameters such as the SQL query (or an
    array of the data produced from the query), and the column names used for the
    data.

    --
    Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
    <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

    Comment

    Working...