1

I have an html select that is populated with DATA from a query using a foreach loop. It looks something like this

$client = $wpdb->get_results("SELECT string FROM `file` WHERE 
`code` = 002 AND `status` = 2");

echo 'Filter by client: ';
  echo '<select name="client_list">';
  foreach ($client as $key => $row) {
    $value = $row->string;
    echo 
  '<option value='.$value.'>'
  .$value. '</option>';
  }
  $client = $_GET['client_list'];
  echo '</select>';

It serves as a filter to display data based on the selected option value. The table that it filters looks somehting like this

   |client  | file              | 
   |------  |-------------------|
   |client1 | file00000         |
   |client2 | file00002         |

Now I want to make the first option value (the default value) of the html empty. How do I do this?

2 Answers 2

1

Add an empty option first:

echo '<select name="client_list"><option value=""></option>';

on submit if client_list is empty you know someone didn't choose anything.

Sign up to request clarification or add additional context in comments.

2 Comments

That works to create an empty value but when I hit submit, it doesn't give the desired results. When I submit with empty client list I want to display a clean unfiltered query but instead it doesn't give me any results.
that is another question than original. You should not use the $client = $_GET['client_list']; in your query if its empty
0

you can use this

      $client = $wpdb->get_results("SELECT string FROM `file` WHERE 
     `code` = 002 AND `status` = 2");

    echo 'Filter by client: ';
    echo '<select name="client_list"><option value=""></option>';
    foreach ($client as $key => $row) {
        $value = $row->string;
        echo 
       '<option value='.$value.'>'
        .$value. '</option>';
    }
    $client = $_GET['client_list'];
    echo '</select>';

3 Comments

That gives two empty values. You're missing / in option.
@R1ddler You wrote "That gives two empty values. You're missing / in option" - So I for one am confused by that comment and whether or not it does solve the question completely and doesn't have anything to do with your new question stackoverflow.com/q/46588904/1415724
If you ommit the closing / in option, that does give two empty values. This solved the question, I only needed to add / to option and it worked. My new question is different from this one and has nothing to do with this one.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.