Note that there are some explanatory texts on larger screens.

plurals
  1. PORun query based on POST values with PHP
    primarykey
    data
    text
    <p>I have a form that allows a user to select a county from a dropdown menu, My form is then posted to my functions.php page where I've been using IF statements to run my queries. </p> <pre><code>if ($_POST['dropdown1'] == 'option 1' &amp;&amp; $_POST['dropdown 2'] == 'option 4' &amp;&amp; $_POST['county'] == 'cheshire' ) { // RUN QUERY } </code></pre> <p>My problem is however I can't realistically use IF statements for every county in every possible scenario as there will be thousands of options, has anybody got a better idea of how I can do this? </p> <hr> <pre><code>if ($_POST['vehicleType'] == 'hgv' &amp;&amp; $_POST['coverageRegion'] == 'national' ) { $customkey = 'vehicleType'; $customvalue = $_POST['vehicleType']; $customkey1 = 'coverageRegion'; $customvalue1 = $_POST['coverageRegion']; $customkey2 = 'locationType'; $customvalue2 = $_POST['locationType']; $args = array('orderby' =&gt; 'meta_value_num', 'meta_key' =&gt; 'order', 'order' =&gt; 'ASC', 'meta_query' =&gt; array( array( 'key' =&gt; $customkey, 'value' =&gt; $customvalue, 'compare' =&gt; '=' ), array( 'key' =&gt; $customkey1, 'value' =&gt; $customvalue1, 'compare' =&gt; '=' ), array( 'key' =&gt; $customkey2, 'value' =&gt; $customvalue2, 'compare' =&gt; '=' ) ) // end of array ); //end of if $query = new WP_Query( $args); // The Loop $i = 0; $i = -1; while ( $query-&gt;have_posts() ) { $i++; $query-&gt;the_post(); if ( $keys = get_post_custom_keys() ) { echo "&lt;div class='clearfix card-prod ".($i==0?'first':'')."'&gt;&lt;div class='top-dets'&gt;&lt;span class='card-title'&gt;"; echo the_title(); echo "&lt;/span&gt;"; // Network query $network_value = get_post_custom_values('srchnetwork'); foreach ( $network_value as $key =&gt; $value ) { echo '&lt;span class="srch-val-"&gt;'. $value . '&lt;/span&gt;'; }// Pricing Query $pricing_value = get_post_custom_values('srchpricing'); foreach ( $pricing_value as $key =&gt; $value ) { echo '&lt;span class="srch-val-1"&gt;'. $value . '&lt;/span&gt;'; } // Setup Query $setup_value = get_post_custom_values('srchsetupfee'); foreach ( $setup_value as $key =&gt; $value ) { echo '&lt;span class="srch-val-2"&gt;'. $value . '&lt;/span&gt;'; } // Services Query $services_value = get_post_custom_values('srchservices'); foreach ( $services_value as $key =&gt; $value ) { echo '&lt;span class="srch-val-3"&gt;'. $value . '&lt;/span&gt;&lt;/div&gt;'; } // Big Card Query $bigcard_value = get_post_custom_values('bigcard'); foreach ( $bigcard_value as $key =&gt; $value ) { echo '&lt;a href="/" class="cardclick"&gt;&lt;img src="/wp-content/themes/CAFC/images/cards/'. $value . '" alt="'; } echo the_title() . '" /&gt;&lt;/a&gt;'; echo '&lt;img src="wp-content/themes/CAFC/images/top-choice.jpg" alt="Top Choice" class="topchoice"&gt;'; echo the_excerpt()."&lt;/div&gt;"; } } } </code></pre>
    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.
 

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