Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I believe its quite a trivial task but I couldn't figure it out yet. I have a mysql table with multiple rows and columns. Each column should be used as a category for a dropdown menu. However some of those columns are shorter than the other ones. I have it currently implemented like this:

<select name="exhaust">
<option value="<? echo "$exhaust"; ?>" selected><? echo "$exhaust"; ?></option>
<?
//connect to mysql
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database"); 
$query = "SELECT * FROM tuning_parts";  
$query = mysql_query($query);
//ausgabe
while($db = mysql_fetch_array($query)){
$phrase = "<option value=\"".$db['exhaust']."\">".$db['exhaust']."</option>";

 echo($phrase);

  };
  ?>

</select> 

However, this give me sometimes very long dropdown lists with a lot of empty values. I've tried to play around with array_filter() but I always got empty results.

I would like to filter out the empty fields so the dropdown menu only shows actual values.

share|improve this question
add comment

3 Answers

up vote 0 down vote accepted

Well, you could do:


//inside your while loop
if(!empty($db['exhaust'])) {
  $phrase = "<option value=\"".$db['exhaust']."\">".$db['exhaust']."</option>";

}

share|improve this answer
add comment

The better way is don't select those records which are empty instead picking up them from database and preventing in code level.

Change your query to

$query = "SELECT * FROM tuning_parts 
          WHERE exhaust IS NOT NULL 
               AND exhaust !='' ";  
share|improve this answer
add comment

Just smple use 'if' condition

   if($db['exhaust']  != ''){
        $phrase = "<option value=\"".$db['exhaust']."\">".$db['exhaust']."</option>";
    }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.