View Single Post
  #1  
Old April 18th, 2012, 09:11 AM
Eric's Avatar
Eric Eric is offline
Datafeedr Team
 
Join Date: Feb 2008
Posts: 16,902
Default Drop down menus for Brands, Merchants and Networks

Here's how to add a <select> dropdown menu for brands, merchants and networks. If you are not comfortable editing PHP or HTML code, this tutorial might not be for you.

You don't have to use them all but you can if you want. Here's what it will look like:



1. Go here: The Factory > Your Store > VIEWS > WIDGETS > Add a new view

2. Name the view "Drop Down Filters"

3. Add a Text module to the view

4. Add the following code to the Text module:

HTML Code:
<form method="get">
  
	<!-- Brand drop down select menu -->
	<select name="brand">
		<?php echo dfr_get_brands(); ?>
	</select>
	
	<!-- Merchant drop down select menu -->
	<select name="merchant_id">
		<?php echo dfr_get_merchants(); ?>
	</select>
	
	<!-- Network drop down select menu -->  
	<select name="net_id">
		<?php echo dfr_get_networks(); ?>
	</select>
	
	<input type="submit" value="Go" />
  
</form>
5. Save the view.

6. Open your theme's functions.php file (usually found here: /wp-content/themes/YOUR-THEME/functions.php)

7. Add the following PHP code to the bottom of the file:

PHP Code:
function dfr_get_brands() {
    global 
$wpdb;
    
$brands $wpdb->get_results("SELECT brand FROM ".$wpdb->prefix."dfr_shop_products GROUP BY brand");
    
$html '<option value="">Select a brand</option>';
    foreach (
$brands as $brands) {
        if (
trim($brands->brand) != "") {
            
$selected = (strip_tags(stripslashes($_GET['brand']))==$brands->brand) ? ' selected="selected"' '';
            
$html .= '<option value="'.$brands->brand.'"'.$selected.'>'.$brands->brand.'</option>';
        }
    }
    return 
$html;


function 
dfr_get_merchants() {
    global 
$wpdb;
    
$merchants $wpdb->get_results("SELECT merchant, merchant_id FROM ".$wpdb->prefix."dfr_shop_products GROUP BY merchant");
    
$html '<option value="">Select a merchant</option>';
    foreach (
$merchants as $k => $v) {
        if (
trim($v->merchant) != "") {
            
$selected = (strip_tags($_GET['merchant_id'])==$v->merchant_id) ? ' selected="selected"' '';
            
$html .= '<option value="'.$v->merchant_id.'"'.$selected.'>'.$v->merchant.'</option>';
        }
    }
    return 
$html;


function 
dfr_get_networks() {
    global 
$wpdb;
    
$networks $wpdb->get_results("SELECT network, net_id FROM ".$wpdb->prefix."dfr_shop_products GROUP BY net_id");
    
$html '<option value="">Select a network</option>';
    foreach (
$networks as $k => $v) {
        if (
$v->net_id 0) {
            
$selected = (strip_tags($_GET['net_id'])==$v->net_id) ? ' selected="selected"' '';
            
$html .= '<option value="'.$v->net_id.'"'.$selected.'>'.$v->network.'</option>';
        }
    }
    return 
$html;

8. Save the functions.php file.

9. Add the new "Drop Down Filters" widget to your sidebar here: WordPress Admin Area > Appearance > Widgets

That's it.

Eric