Hello
I am trying to run a query that produces a drop down list from an array. The array has values stored like this:

pencil, ruler
ruler, protractor
pencil, ruler, compass
etc

I want to split the array then only return distinct values. So the above would be a drop down containing pencil, ruler, protractor and compass. Can anyone help me out with this please? Here's my code at the moment, but I'm not exploding the array and I'm not sure how to in this context.

<?php
$query = "SELECT DISTINCT category FROM table";
$result = @mysql_query ($query);
$pulldown = '<option value="Any">Any</option>';
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
$pulldown .= "<option value=\"{$row['category']}\">{$row['category']}</option>\n";
}
?>

<select class="formbit" name="category">
<?php echo $pulldown; ?> 
</select>

Many thanks!

    you store more than one entry in one field ?

    if yes, lets create a big $string , and separate the values with , or # ,

    $sql="SELECT field FROM table GROUP BY field";
    $res=mysql_query("$sql");
    while( $egy_sor = mysql_fetch_object( $eredmeny))
    {
    $string.=$egy_sor->field; //link into one string the entries.
    }

    and then use the $exploded_string=explode(",", $string);

    $unique_list=array_unique($exploded_string);

    then lets create a dropdown with foreach.

    etoast;10891601 wrote:

    Hello
    I am trying to run a query that produces a drop down list from an array. The array has values stored like this:

    pencil, ruler
    ruler, protractor
    pencil, ruler, compass
    etc

    I want to split the array then only return distinct values. So the above would be a drop down containing pencil, ruler, protractor and compass. Can anyone help me out with this please? Here's my code at the moment, but I'm not exploding the array and I'm not sure how to in this context.

    <?php
    $query = "SELECT DISTINCT category FROM table";
    $result = @mysql_query ($query);
    $pulldown = '<option value="Any">Any</option>';
    while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
    $pulldown .= "<option value=\"{$row['category']}\">{$row['category']}</option>\n";
    }
    ?>
    
    <select class="formbit" name="category">
    <?php echo $pulldown; ?> 
    </select>

    Many thanks!

      Thanks for the suggestion. $eredmeny gave an error so I changed it to $res but I still couldn't get it to work. Going back to my original I have managed to split the arrays, but I cannot get it to stop showing duplicate values.

      I tried to turn $category2 into a new array and then unique_array to get rid of the duplicates, but I cannot figure out the syntax as I just get "Array" showing in my dropdown.

      Here's my revised code:

      <?php
      $query = "SELECT DISTINCT category FROM table";
      $result = @mysql_query ($query) or die(mysql_error());
      while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
      $category2=explode(", ",$row["category"]);
      
      foreach ($category2 as $value) {
      echo "<option value=" .$value. ">$value</option><br />";
      }
      }
      ?>

        After the explode() each element of category2 is itself an array, so now you have to loop through that as well.
        $distinct_values = array();

        while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
            foreach(explode(", ",$row["category"]) as $value) {
                $distinct_values[] = $value;
            }
        }
        $distinct_values = array_unique($distinct_values);
        

        But I think you're finding out one of the reasons why what you've done in the database is considered poor design. Fix it while you still can 🙂

          Perfect. Thanks so much weedpacket!

            Write a Reply...