Skip to content

Instantly share code, notes, and snippets.

@joshmoto
Created February 1, 2013 12:02
Show Gist options
  • Save joshmoto/4690927 to your computer and use it in GitHub Desktop.
Save joshmoto/4690927 to your computer and use it in GitHub Desktop.
help.php
<?php
define('WP_USE_THEMES', false);
require('/home/sites/mission-rossi.com/www/wp/wp-load.php');
?>
<!DOCTYPE html>
<html>
<head>
<!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<?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_country' OR meta_key = 'dealer_location'", ARRAY_A);
// get all county base on meta_key and associate it with town
foreach($dealer_data as $d){
if($d['meta_key'] == 'dealer_country'){
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_location'){
$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;
}
}
?>
<form id="dealer-search" name="dealer-search">
<select name="dealer-county" id="dealer-county">
<option class="default" value="0"><?php _e('Choose country...','theme') ?></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"><?php _e('...') ?></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" disabled="disabled"><i class="icon-search"></i> <?php _e('Find dealers','theme') ?></button>
</form>
</div>
<div id="dealer-results">
</div>
<script type="text/javascript">
$(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);
$('#dealer-town option.default').html('<?php _e('Choose town or city...','theme'); ?>');
} else {
$('#dealer-town').attr('disabled','disabled');
$('#dealer-town option.default').html('...');
}
});
$('#dealer-town').change(function(){
var selected_town = $('option:selected',this).val().replace(/ /g,"-");
if(selected_town != 0){
$('#dealer-search button').removeAttr('disabled');
} else {
$('#dealer-search button').attr('disabled','disabled');
}
});
$('#dealer-search').submit(function(e){ // Fire this function some how with the form submission.
e.preventDefault();
dealerResults();
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment