0

im new to PHP and im trying this stuff for hours, I wanted to show the Array items to the Select Option. Here's the code:

<select name="state">
         <?php
          $arrstate=array
          (
             array("AK","Alaska"),
             array("AL","Alabama"),
             array("AR","Arkansas"),
             array("AZ","Arizona"),
             array("CA","California"),
             array("CO","Colorado"),
             array("CT","Connecticut"),
             array("DC","District of Columbia"),
             array("DE","Delaware"),
             array("FL","Florida"),
             array("GA","Georgia"),
             array("HI","Hawaii"),
             array("IA","Iowa"),
             array("ID","Idaho"),
             array("IL","Illinois"),
             array("IN","Indiana"),
             array("KS","Kansas"),
             array("KY","Kentucky"),       
          );
          for($lop=0;$lop<=49;$lop++)
              {
                if (strtoupper($lead_info['state'])==$arrstate[$lop][0])
              {
                echo "<option selected=\"selected\" value=\"".$arrstate[$lop][0]."\">".$arrstate[$lop][1]."</option>\n";           
              }else{
              echo "<option value=\"".$arrstate[$lop][0]."\">".$arrstate[$lop][1]."</option>\n";           
              }
              }
          ?>
        </select>

But it seems it only shows ".$arrstate[$lop][1]." What seems to be the problem?

3
  • 1
    why not just use associative array? Commented Apr 7, 2016 at 2:31
  • I wanted to know what state the user was. Before this page. There was a pre-reg that only gets his/her ZIP. and I wanted to show what state he/she currently on based on the ZIP he entered. Commented Apr 7, 2016 at 2:37
  • you can do a condition on option.. for example <option <?=($value == $current_zip_entered ? "selected" : "")?> > Commented Apr 7, 2016 at 2:40

3 Answers 3

3

try it

<?php
  $arrstate = array
  (
     'AK' => 'Alaska',
     'AL' => 'Alabama',
     'AR' => 'Arkansas',
     'AZ' => 'Arizona'   
  ); ?>
<select>
     <?php foreach($arrstate as $key => $value) { ?>
      <option value="<?php echo $key; ?>"  <?=($value == $current_zip_entered ? "selected" : "" )?>><?php echo $value; ?></option>
     <?php } ?></select>
Sign up to request clarification or add additional context in comments.

Comments

0
  <?php
      $arrstate=array
      (
         "AK","Alaska",
         "AL","Alabama",
         "AR","Arkansas",
         "AZ","Arizona"   
      );
   ?>

this is the easiest way...

   <select>
         <?php
           foreach($arrstate as $key => $value) {
         ?>
          <option value="<?php echo $key; ?>"  <?=($value == $current_zip_entered ? "selected" : "" )?>><?php echo $value; ?></option>
         <?php
           }
          ?>
   </select>

Comments

0

You can retrieve from database like this example:

      <select>
         <?php $result = mysqli_query($conn, "SELECT * FROM categories");
              while ($row = mysqli_fetch_array($result)) {                                
                    $categories = array($row["category"]);
                    $arrlength = count($categories);

                    for($x = 0; $x < $arrlength; $x++) {
                       echo  "<option>";
                       echo  $categories[$x]; 
                       echo  "</option>";
                                         }}?>
               </select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.