1

The following worked on a local environment. Now that it's pushed live everything but these seems to work. Displays completely empty select boxes now no checks or blank labels just empty space in the "select options" drop down. select display

<?php
  $selected = array();
  $selected = explode(",",$fill['markets']);

  $condb = mysql_query("SELECT * FROM `countries`");
  $count = mysql_num_rows($condb);
  $countries = array();
  $str;

  while ($countries = mysql_fetch_array($condb))
  {
    $str = "option{$countries['id']}";
    echo "<option value='{$str}' ";
    if(in_array($str,$selected)) {
      echo "selected>";
      echo $countries['country'];
      echo "</option>";
    } else {
      echo ">";
      echo $countries['country'];
      echo "</option>";
    }
  }      
  ?>
  • try $countries = mysql_fetch_array($condb); or you can use just a while with: `while ($row = mysql_fetch_array($result)) {`, – cmnardi Apr 12 '16 at 14:42
  • that's the second variable declared, and it contains the values. $i references the row by id / array element. Since it's an array of arrays it's "arraykey->arrayvalue" in this case row->column. I know it contains the values because if I echo them specifically/individually it prints them out. – Robert Wood Apr 12 '16 at 14:45
  • 1
    `mysql_fetch_array` only returns *one* row at a time, not *all* rows of the result set. Thats why you only have one country listed. – bspellmeyer Apr 12 '16 at 14:47

1 Answers1

0

You should use while loop instead. You can declare $i outside the loop if you need it, Try the following code:

  $condb = mysql_query("SELECT * FROM `countries`");

  $i = 0;
   while($countries = mysql_fetch_array($condb)) {
    $str = 'option' . $i;
    echo "<option value='{$str}' ";
    if(in_array($str,$selected)) {
      echo "selected>";
      echo $countries['country'];
      echo "</option>";
    } else {
      echo ">";
      echo $countries['country'];
      echo "</option>";
    }
 $i++;
  }
  ?>
</select>

Note:

mysql_* is deprecated as of . So instead use mysqli_* or PDO.
Why shouldn't I use mysql_* functions in PHP?

Community
  • 1
  • 1
mega6382
  • 9,211
  • 17
  • 48
  • 69