Multiple 'similar' dropdowns -> 1 query

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

    Multiple 'similar' dropdowns -> 1 query

    Hi there

    I'm building this CMS, and at a point i have
    3 similar drop downs. The values of the drop
    downs are called from a MysqlDB.

    The first one is just fine:
    do{

    $selected = ($_GET['id_1'] == $result['id']) ? ' selected' : '';

    echo '<option'.$sele cted.'>'.$resul t['id'].'</option>';

    }while($result = mysql_fetch_arr ay($query));


    If the value of $_GET['id_1'] matches a value from the DB,
    that value is selected in the final dropdown.

    But when i try to do it again with $_GET[id_2] or $_GET[id_3], it
    fails.
    How can i 'recall' the mysql info?

    Thanks.

    Frizzle.

  • Geoff Berrow

    #2
    Re: Multiple 'similar' dropdowns -&gt; 1 query

    I noticed that Message-ID:
    <1122475554.151 561.37260@g49g2 000cwa.googlegr oups.com> from frizzle
    contained the following:
    [color=blue]
    >But when i try to do it again with $_GET[id_2] or $_GET[id_3], it
    >fails.
    >How can i 'recall' the mysql info?[/color]

    I usually create an array of the db info then create the from that. All
    you need to do then is check if $_GET[id_n] is in the array.

    --
    Geoff Berrow (put thecat out to email)
    It's only Usenet, no one dies.
    My opinions, not the committee's, mine.
    Simple RFDs http://www.ckdog.co.uk/rfdmaker/

    Comment

    • frizzle

      #3
      Re: Multiple 'similar' dropdowns -&gt; 1 query

      Hmm, could you maybe explain a little bit more?
      I'm not trying to find a better way for the '$selected'-part,
      but i'm not able (yet) to create the same dropdown three
      times from 1 query ...

      Thanks.

      Frizzle.

      Comment

      • Geoff Berrow

        #4
        Re: Multiple 'similar' dropdowns -&gt; 1 query

        I noticed that Message-ID:
        <1122490398.862 551.269070@g43g 2000cwa.googleg roups.com> from frizzle
        contained the following:
        [color=blue]
        >Hmm, could you maybe explain a little bit more?
        >I'm not trying to find a better way for the '$selected'-part,
        >but i'm not able (yet) to create the same dropdown three
        >times from 1 query ...[/color]

        If you are doing something more than once you need to be thinking
        'function'
        Let is say you query the database and do a loop to put the values in an
        array. In the following function, $name is the name of the select box,
        $array is the array from the database and $sel is the selection you are
        testing.

        function mk_select($name ,$array,$sel){
        $string = "<select name=\"$name\"c lass=\"input\"> ";
        foreach ($array AS $key=>$value) {
        if($key==$sel){ $selected=" selected";}
        else{$selected= "";}
        $string .= "<option
        value=\"$key\"$ selected>$value </option>\n";
        }
        $string.="</select>";
        return $string;
        }
        --
        Geoff Berrow (put thecat out to email)
        It's only Usenet, no one dies.
        My opinions, not the committee's, mine.
        Simple RFDs http://www.ckdog.co.uk/rfdmaker/

        Comment

        • frizzle

          #5
          Re: Multiple 'similar' dropdowns -&gt; 1 query

          Hmm, being the newbie i am, i still can't get it to work:
          i have $result = mysql_fetch_arr ay($get_result) ;

          Works fine with a

          do{
          foo
          }while($result = mysql_fetch_arr ay($get_result) );

          It returns the following from the DB:

          id code
          ____________
          27 ABC becomes: <option value="27">ABC</option>
          35 KNO becomes: <option value="35">KNO</option>
          57 TRE etc.
          81 FLO etc.
          ____________



          But when i call your function like this:
          echo mk_select('name _1', $result, 'find_this');
          it returns 4 items wich is correct, but the first two
          are one id, and the third and fourth are a code.

          Something like this:
          id code
          ____________
          <option value="">27</option>
          <option value="">27</option>
          <option value="">KNO</option>
          <option value="">KNO</option>
          ____________

          i just can't figure it out ...

          Greetings Frizzle.

          Comment

          • Geoff Berrow

            #6
            Re: Multiple 'similar' dropdowns -&gt; 1 query

            I noticed that Message-ID:
            <1122495115.266 022.14850@g43g2 000cwa.googlegr oups.com> from frizzle
            contained the following:
            [color=blue]
            >Hmm, being the newbie i am, i still can't get it to work:
            >i have $result = mysql_fetch_arr ay($get_result) ;
            >
            >Works fine with a
            >
            >do{
            > foo
            >}while($resu lt = mysql_fetch_arr ay($get_result) );
            >
            >It returns the following from the DB:
            >
            >id code
            >____________
            >27 ABC becomes: <option value="27">ABC</option>
            >35 KNO becomes: <option value="35">KNO</option>
            >57 TRE etc.
            >81 FLO etc.
            >____________
            >[/color]
            So $result[id] ==27 $result[code]==ABC

            You have to combine those into a new array
            $select_array=a rray();
            while($result = mysql_fetch_arr ay($get_result) ){
            $select_array[$result[id]]=$result[code];
            }

            Then your new array will contain the database rows as key/value pairs
            and you can use it in the function.

            echo mk_select('name _1', $select_array, 'find_this');
            --
            Geoff Berrow (put thecat out to email)
            It's only Usenet, no one dies.
            My opinions, not the committee's, mine.
            Simple RFDs http://www.ckdog.co.uk/rfdmaker/

            Comment

            • frizzle

              #7
              Re: Multiple 'similar' dropdowns -&gt; 1 query

              Great!
              As you've probably read, i didn't put the query in an array first.
              Now everything works fine!
              Thanks a lot!

              Frizzle.

              Comment

              Working...