One result set makes two lists with same keys and values

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

    #1

    One result set makes two lists with same keys and values

    I have a page with two drop-downs, each listing the same employee names
    and their id's as the values. So they look like:
    <select name="selection _1">
    <option value="12">Curl y</option>
    <option value="8">Larry </option>
    <option value="36">Moe</option>
    </select>

    ....then further down the page:

    <select name="selection _2">
    <option value="12">Curl y</option>
    <option value="8">Larry </option>
    <option value="36">Moe</option>
    </select>

    Currently I am doing this, which works but seems sloppy:

    <select name="selection _1">
    <?
    while($employee s=mysql_fetch_a ssoc($qry_rslt) ) {
    /* ADD NAMES TO DROPDOWN WITH emp_id's AS THEIR VALUES */
    ?><option
    value="<?=$empl oyees['emp_id']?>"><?=$employe es['name']?></option><?

    /* CREATE ANOTHER ARRAY WITH THE emp_id AS THE KEY */
    $emp_list_2[$employees['emp_id']]=$employees['name'];
    }
    ?>
    </select>

    Then on the second drop-down:

    <select name="selection _2">
    <?
    foreach($emp_li st_2 as $key => $emps) {
    ?>
    <option value="<?=$key? >"><?=$emps?> </option>
    <?
    }
    ?>
    </select>


    Is there a better way to do this?
    Thanks.
    Steve.
  • Agelmar

    #2
    Re: One result set makes two lists with same keys and values

    Noyb wrote:[color=blue]
    > I have a page with two drop-downs, each listing the same employee
    > names and their id's as the values. So they look like:
    > <select name="selection _1">
    > <option value="12">Curl y</option>
    > <option value="8">Larry </option>
    > <option value="36">Moe</option>
    > </select>
    >
    > ...then further down the page:
    >
    > <select name="selection _2">
    > <option value="12">Curl y</option>
    > <option value="8">Larry </option>
    > <option value="36">Moe</option>
    > </select>
    >
    > Currently I am doing this, which works but seems sloppy:
    >
    > <select name="selection _1">
    > <?
    > while($employee s=mysql_fetch_a ssoc($qry_rslt) ) {
    > /* ADD NAMES TO DROPDOWN WITH emp_id's AS THEIR VALUES */[color=green]
    >> <option
    >> value="<?=$empl oyees['emp_id']?>"><?=$employe es['name']?></option><?[/color]
    >
    > /* CREATE ANOTHER ARRAY WITH THE emp_id AS THE KEY */
    > $emp_list_2[$employees['emp_id']]=$employees['name'];
    > }[color=green]
    >>[/color]
    > </select>
    >
    > Then on the second drop-down:
    >
    > <select name="selection _2">
    > <?
    > foreach($emp_li st_2 as $key => $emps) {[color=green]
    >>[/color]
    > <option value="<?=$key? >"><?=$emps?> </option>
    > <?
    > }[color=green]
    >>[/color]
    > </select>
    >
    >
    > Is there a better way to do this?
    > Thanks.
    > Steve.[/color]

    There is a slightly better way to do it...
    $dropdown = "";
    while($employee s=mysql_fetch_a ssoc($qry_rslt) ) {
    $dropdown .= '<option
    value="'.$emplo yees['emp_id'].'">'.$employee s['name']."<option>\n ";
    }

    and then you can just do...
    <select name ="select1">
    <?php echo $dropdown; ?>
    </select>

    <select name="select2">
    <?php echo $dropdown; ?>
    </select>

    -Ian


    Comment

    • Pedro Graca

      #3
      Re: One result set makes two lists with same keys and values

      ["Followup-To:" header set to comp.lang.php.]
      Noyb wrote:[color=blue]
      > Currently I am doing this, which works but seems sloppy:
      >
      ><select name="selection _1">
      ><?[/color]


      $options = '';

      [color=blue]
      > while($employee s=mysql_fetch_a ssoc($qry_rslt) ) {[/color]

      /* deleted[color=blue]
      > /* ADD NAMES TO DROPDOWN WITH emp_id's AS THEIR VALUES */
      > ?><option
      > value="<?=$empl oyees['emp_id']?>"><?=$employe es['name']?></option><?
      >
      > /* CREATE ANOTHER ARRAY WITH THE emp_id AS THE KEY */
      > $emp_list_2[$employees['emp_id']]=$employees['name'];[/color]
      deleted */

      $options .= "<option value='{$employ ess['emp_id']}'>{$employees['name']}</option>";
      [color=blue]
      > }
      > ?>[/color]


      <?=$options?>

      [color=blue]
      ></select>
      >
      > Then on the second drop-down:
      >
      ><select name="selection _2">[/color]

      /* deleted[color=blue]
      ><?
      > foreach($emp_li st_2 as $key => $emps) {
      > ?>
      ><option value="<?=$key? >"><?=$emps?> </option>
      ><?
      > }
      > ?>[/color]
      */ deleted

      <?=$options?>
      [color=blue]
      ></select>[/color]
      --
      --= my mail box only accepts =--
      --= Content-Type: text/plain =--
      --= Size below 10001 bytes =--

      Comment

      • Noyb

        #4
        Re: One result set makes two lists with same keys and values

        Agelmar wrote:
        [color=blue]
        > Noyb wrote:
        >[color=green]
        >>I have a page with two drop-downs, each listing the same employee
        >>names and their id's as the values. So they look like:
        >><select name="selection _1">
        >> <option value="12">Curl y</option>
        >> <option value="8">Larry </option>
        >> <option value="36">Moe</option>
        >></select>
        >>
        >>...then further down the page:
        >>
        >><select name="selection _2">
        >> <option value="12">Curl y</option>
        >> <option value="8">Larry </option>
        >> <option value="36">Moe</option>
        >></select>
        >>
        >>Currently I am doing this, which works but seems sloppy:
        >>
        >><select name="selection _1">
        >><?
        >>while($employ ees=mysql_fetch _assoc($qry_rsl t)) {
        >>/* ADD NAMES TO DROPDOWN WITH emp_id's AS THEIR VALUES */
        >>[color=darkred]
        >>><option
        >>>value="<?=$e mployees['emp_id']?>"><?=$employe es['name']?></option><?[/color]
        >>
        >>/* CREATE ANOTHER ARRAY WITH THE emp_id AS THE KEY */
        >>$emp_list_2[$employees['emp_id']]=$employees['name'];
        >>}
        >>
        >></select>
        >>
        >>Then on the second drop-down:
        >>
        >><select name="selection _2">
        >><?
        >>foreach($emp_ list_2 as $key => $emps) {
        >>
        >><option value="<?=$key? >"><?=$emps?> </option>
        >><?
        >>}
        >>
        >></select>
        >>
        >>
        >>Is there a better way to do this?
        >>Thanks.
        >>Steve.[/color]
        >
        >
        > There is a slightly better way to do it...
        > $dropdown = "";
        > while($employee s=mysql_fetch_a ssoc($qry_rslt) ) {
        > $dropdown .= '<option
        > value="'.$emplo yees['emp_id'].'">'.$employee s['name']."<option>\n ";
        > }
        >
        > and then you can just do...
        > <select name ="select1">
        > <?php echo $dropdown; ?>
        > </select>
        >
        > <select name="select2">
        > <?php echo $dropdown; ?>
        > </select>
        >
        > -Ian
        >
        >[/color]
        Silly me, that's a much easier solution. Thanks!

        Comment

        Working...