0

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.

3 Answers 3

2

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 !='' ";  
Sign up to request clarification or add additional context in comments.

Comments

0

Well, you could do:


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

}

Comments

0

Just smple use 'if' condition

   if($db['exhaust']  != ''){
        $phrase = "<option value=\"".$db['exhaust']."\">".$db['exhaust']."</option>";
    }

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.