Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script type="text/javascript">
- jQuery(document).ready(function($) {
- dealerResults = function () {
- var county = $('#dealer-county').val(),
- town = $('#dealer-town').val(),
- wp_nonce = '<?php echo wp_create_nonce("dealer_search");?>';
- if(county != 0 && town != 0){
- $.ajax({
- type: 'POST',
- url: '<?php echo admin_url('admin-ajax.php'); ?>',
- data: { varCounty : county, varTown : town, action: 'dealer_search', nonce: wp_nonce },
- success: function(data){
- $(".dealer-wrapper").remove();
- $("#dealer-results").append(data);
- }
- });
- }
- }
- var dealer_town = $('#dealer-town').clone(true);
- $('#dealer-county').change(function(){
- var selected_county = $('option:selected',this).val().replace(/ /g,"-");
- if(selected_county != 0){
- var selectedTown = dealer_town.clone(true).find('.default,.'+selected_county);
- $('#dealer-town').removeAttr('disabled');
- $('#dealer-town').empty().append(selectedTown);
- } else {
- $('#dealer-town').attr('disabled','disabled');
- }
- });
- $('#dealer-search').submit(function(e){ // Fire this function some how with the form submission.
- e.preventDefault();
- dealerResults();
- });
- });
- </script>
- <?php
- global $wpdb;
- $all_dealer_region = $dealer_county = $dealer_town = Array();
- $dealer_data = $wpdb->get_results( "SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta WHERE meta_key = 'dealer_county' OR meta_key = 'dealer_town'", ARRAY_A);
- // get all county base on meta_key and associate it with town
- foreach($dealer_data as $d){
- if($d['meta_key'] == 'dealer_county'){
- if(!is_array($all_dealer_region[$d['meta_value']]))
- $all_dealer_region[$d['meta_value']] = array();
- $dealer_county[$d['post_id']] = $d['meta_value'];
- }
- if($d['meta_key'] == 'dealer_town'){
- $dealer_town[$d['post_id']] = $d['meta_value'];
- }
- }
- foreach($dealer_town as $key => $value){
- if( !in_array($value, $all_dealer_region[$dealer_county[$key]]) ){
- $all_dealer_region[$dealer_county[$key]][] = $value;
- }
- }
- ?>
- <div id="dealer-filter">
- <form id="dealer-search" name="dealer-search">
- <select name="dealer-county" id="dealer-county">
- <option class="default" value="0">Choose County</option>
- <?php foreach($all_dealer_region as $key => $value){
- echo "<option value=\"$key\">$key</option>";
- } ?>
- </select>
- <select name="dealer-town" id="dealer-town" class="input-xlarge" disabled="disabled">
- <option class="default" value="0">Choose Town</option>
- <?php foreach($all_dealer_region as $key => $value){
- usort($value, 'dealer_sort');
- $county_class = str_replace(' ','-',$key);
- foreach($value as $town){
- echo "<option class=\"$county_class\" value=\"$town\">$town</option>";
- }
- } ?>
- </select>
- <button type="submit" class="btn"><i class="icon-search"></i> Find dealers</button>
- <form>
- </div>
- <div id="dealer-results">
- <!-- MY DEALER RESULTS WILL APPEAR HERE! -->
- </div>
Advertisement
Add Comment
Please, Sign In to add comment