Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The query parameter "style" is an array in this case and must be identified by square brackets - if not, the last key=value pair will overwrite the others.</p> <pre><code>?style[]=ranch&amp;style[]=barn&amp;style[]=colonial </code></pre> <p><code>$_GET['style']</code> is an array then you can loop over by using <code>foreach</code>:</p> <pre><code>foreach ($_GET['style'] as $value) { // ... } </code></pre> <p>if 'style' is not the only parameter you want to add, you can use a <code>is_array()</code> check in the <code>foreach</code> loop:</p> <pre><code> foreach ($_GET as $key =&gt; $value) { if ($i == 1){ $sqlStyle .= "where "; }else{ $sqlStyle .= " and "; } if(is_array($value)) { $sec = array(); foreach($value as $second_level) { $sec[] = $key . " LIKE '%" . $second_level."%'"; } $sqlStyle .= implode(' AND ', $sec); } else { $sqlStyle .= $key . " LIKE '%" . $value ."%'"; } $i++; } echo $sqlStyle; </code></pre> <p>alternative without foreach:</p> <pre><code>&lt;?php $statement = "SELECT DISTINCT COUNT(*) as count FROM `houses_single`"; if(is_array($_GET)) { $statement .= ' WHERE'; // create copy to keep the $_GET array $add_where = $_GET; array_walk(function($elem,$key){ is_array($elem) { return implode(' AND ', array_map(function($sec) using ($key) { return "$key LIKE '%$sec%'"; }, $elem); } else { return "$key LIKE '%$elem%'"; } },$add_where); $statement .= implode(' AND ', $add_where); } </code></pre> <p><em>(codes are untested)</em></p> <p>Sidenode about safety: I hope you won't use this code snippet you provided in productive environment without any escaping of the parameters.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload