View Single Post
  #11  
Old May 10th, 2013, 12:19 PM
Eric's Avatar
Eric Eric is offline
Datafeedr Team
 
Join Date: Feb 2008
Posts: 16,902
Default

Hi,

Here's some updated code.

Add the following code to your theme's functions.php file:

PHP Code:
function dfr_get_brands($category=false) {
    global 
$wpdb;
    
$cat_query "";
    if (@
$category->id) { 
        
$in = array();
        
$in[] = $category->id;
        if (!empty(
$category->children)) {
            foreach (
$category->children as $child => $id) {
                
$in[] = $id;
            }
        }
        
$category_ids implode(","$in);
        
$cat_query "WHERE p2c.cat_id IN (".$category_ids.") "
    }
    
$brands $wpdb->get_results("
        SELECT brand, p2c.cat_id 
        FROM "
.$wpdb->prefix."dfr_shop_products AS p 
        INNER JOIN "
.$wpdb->prefix."dfr_shop_p2c AS p2c ON p.id = p2c.id 
        
$cat_query
        GROUP BY brand
    "
);
    
$html '<option value="">Select a brand</option>';
    foreach (
$brands as $row) {
        
$brand trim($row->brand);
        if (
$brand != "") {
            
$selected = (strip_tags($_GET['brand'])==$brand) ? ' selected="selected"' '';
            
$html .= '<option value="'.$brand.'"'.$selected.'>'.$brand.'</option>';
        }
    }
    return 
$html;
}  

function 
dfr_get_merchants($category=false) {
    global 
$wpdb;
    
$cat_query "";
    if (@
$category->id) { 
        
$in = array();
        
$in[] = $category->id;
        if (!empty(
$category->children)) {
            foreach (
$category->children as $child => $id) {
                
$in[] = $id;
            }
        }
        
$category_ids implode(","$in);
        
$cat_query "WHERE p2c.cat_id IN (".$category_ids.") "
    }
    
$merchants $wpdb->get_results("
        SELECT merchant, p2c.cat_id 
        FROM "
.$wpdb->prefix."dfr_shop_products AS p 
        INNER JOIN "
.$wpdb->prefix."dfr_shop_p2c AS p2c ON p.id = p2c.id 
        
$cat_query
        GROUP BY merchant
    "
);
    
$html '<option value="">Select a merchant</option>';
       foreach (
$merchants as $row) {
        
$merchant trim($row->merchant);
        if (
$merchant != "") {
            
$selected = (strip_tags($_GET['merchant'])==$merchant) ? ' selected="selected"' '';
            
$html .= '<option value="'.$merchant.'"'.$selected.'>'.$merchant.'</option>';
        }
    }
    return 
$html;
}  


function 
dfr_get_networks($category=false) {
    global 
$wpdb;
    
$cat_query "";
    if (@
$category->id) { 
        
$in = array();
        
$in[] = $category->id;
        if (!empty(
$category->children)) {
            foreach (
$category->children as $child => $id) {
                
$in[] = $id;
            }
        }
        
$category_ids implode(","$in);
        
$cat_query "WHERE p2c.cat_id IN (".$category_ids.") "
    }
    
$networks $wpdb->get_results("
        SELECT network, p2c.cat_id 
        FROM "
.$wpdb->prefix."dfr_shop_products AS p 
        INNER JOIN "
.$wpdb->prefix."dfr_shop_p2c AS p2c ON p.id = p2c.id 
        
$cat_query
        GROUP BY network
    "
);
    
$html '<option value="">Select a network</option>';
       foreach (
$networks as $row) {
        
$network trim($row->network);
        if (
$network != "") {
            
$selected = (strip_tags($_GET['network'])==$network) ? ' selected="selected"' '';
            
$html .= '<option value="'.$network.'"'.$selected.'>'.$network.'</option>';
        }
    }
    return 
$html;

Add the following code to a Text module (in a Widget view):

HTML Code:
<form method="get">
  
  <!-- Brand drop down select menu -->
  <select name="brand">
    <?php echo dfr_get_brands($category); ?>
  </select>
  
  <!-- Merchant drop down select menu -->
  <select name="merchant">
    <?php echo dfr_get_merchants($category); ?>
  </select>
  
  <!-- Network drop down select menu -->  
  <select name="network">
    <?php echo dfr_get_networks($category); ?>
  </select>
  
  <!-- Price filter -->
  <select name="price">
    <option value="-100"<?php if ($_GET['price'] == '-100') { echo ' selected="selected"'; } ?>>Less than $100</option>
    <option value="100-500"<?php if ($_GET['price'] == '100-500') { echo ' selected="selected"'; } ?>>Between $100 and $500</option>
    <option value="500-"<?php if ($_GET['price'] == '500-') { echo ' selected="selected"'; } ?>>Over $500</option>
  </select>
  
  <input type="submit" value="Go" />
  
</form>
That should do the trick.

Eric