Jump to content

Recommended Posts

I'm using php dreamweaver 8 and mySQL 4.1.21

 

I'm teaching myself php and need some help with arrays and drop down menus

I've built and array containing state ids and state names.

  Example:

[1]Alabama [2]Alaska [3]Arizona [4]Arkansas [5]California and so on.

 

 

 

I have this section of code to build a dropdown menu from a record set, works fine.

 

<select name="listStates" id="listStates">

  <option value="">Select a state</option>

  <?php

    do { ?>

        <option value="<?php echo $row_getStates['state_id']?>"><?php echo $row_getStates['state_name']?></option>

        <?php

    } while ($row_getStates = mysql_fetch_assoc($getStates));

        $rows = mysql_num_rows($getStates);

        if($rows > 0) {

          mysql_data_seek($getStates, 0);

  $row_getStates = mysql_fetch_assoc($getStates);

        }

  ?>

</select

 

 

I now want to build the same menu with the array

 

I get this far and I'm stumped.

 

How do I define my array elements in the foreach loop?

 

 

<select name="listStatesArray" id="listStatesArray">

  <option value="">Select a state</option>

  <?php

    foreach ($arrayStates as $statesMenu) {

 

    ?>                                       

      <option value="<?php echo $statesMenu  (need ID here)?>"><?php echo $statesMenu  (need name here)?></option>

    <?php

    }

  ?>

</select>

 

Any help will be greatly appreciated

<select name="listStatesArray" id="listStatesArray">
   <option value="">Select a state</option>
   <?php
     foreach ($arrayStates as $stateID => $stateValue) {
        echo "<option value=\"$stateID\">$stateValue</option>";
     }
   ?>
</select>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.