Datafeedr Support Forums

Datafeedr Support Forums (http://www.datafeedr.com/forums/index.php)
-   Questions (http://www.datafeedr.com/forums/forumdisplay.php?f=67)
-   -   Dynamic Generation of Brand Filter (http://www.datafeedr.com/forums/showthread.php?t=4411)

meperez May 1st, 2011 08:18 PM

Dynamic Generation of Brand Filter
 
www.thekitchentool.com
Store Id: 30751

Hi Eric,

I have some hard-coded brand filters on each category and I'm trying to generate those dynamically so that I don't have to worry about them getting out of sync with the products. Based on other posts I saw, I gave it an initial try... and clearly my syntax is erroneous somewhere as the Brand Filter no longer shows up. I'm trying it on the Woks category under Cookware. Can you please help?

I added the following function to my functions.php:

//Function to Get Brands of Woks Category
function dfr_get_brands() {
global $wpdb;
$merchants = $wpdb->get_results("SELECT DISTINCT p.brand FROM ".$wpdb->prefix."dfr_shop_products p, ".$wpdb->prefix."wp_dfr_shop_p2c pc, ".$wpdb->prefix."wp_dfr_shop_cats c AND pc.cat_id = c.id AND c.name = \'Woks\' ORDER BY c.name, p.brand");
$list = array();
foreach ($brands as $brands) {
$list[$brands->brand] = $brands->brand;
}
return $list;
}

Then to the filter Widget, I added:

<div>
<form method="get">
<span>
<?php echo datafeedr_tools()->form_select("brand",
array(
"" => "All Brands",
?>
<?php
$brands = dfr_get_brands();
foreach ($brands as $brandname) {
echo '\"'$brandname'\" => \"'$brandname'\",';
}
?>
));
<input type="submit" value="Go" />
</span>
</form>
</div>

My goal is to generate the following code, for example:

<div>
<form method="get">
<span>
<?php echo datafeedr_tools()->form_select("brand",
array(
"" => "All Brands",
"All-Clad" => "All-Clad",
"Anolon" => "Anolon",
"Calphalon" => "Calphalon",
"Circulon" => "Circulon",
"Cuisinart" => "Cuisinart",
"Le Creuset" => "Le Creuset",
"West Bend" => "West Bend"
)); ?>
<input type="submit" value="Go" />
</span>
</form>
</div>

Eric May 1st, 2011 08:21 PM

Hi

You can't perform a foreach() loop inside the form_select() function call. You'll just have to create the <select> drop down box using your own HTML code and not the form_select() function.

Eric

meperez May 1st, 2011 10:08 PM

Thanks, is there any other way you can think of to enable some sort of dynamic list?

Eric May 2nd, 2011 08:39 AM

Something like this should work:

HTML Code:

<?php
//Function to Get Brands
function dfr_get_brands() {
        global $wpdb;
        $merchants = $wpdb->
get_results("SELECT DISTINCT p.brand FROM ".$wpdb->prefix."dfr_shop_products p, ".$wpdb->prefix."wp_dfr_shop_p2c pc, ".$wpdb->prefix."wp_dfr_shop_cats c AND pc.cat_id = c.id AND c.name = \'Woks\' ORDER BY c.name, p.brand");
        $html = '';
        foreach ($brands as $brands) {
                $html .= '<option value="'.$brands->brand.'">'.$brands->brand.'</option>';
        }
        return $html;
}
?>





<!-- code for your vuew -->
<form method="get">
        <span>
                <?php echo dfr_get_brands(); ?>
                <input type="submit" value="Go" />
        </span>
</form>


meperez May 2nd, 2011 10:18 PM

Thanks Eric, I tried putting all of that code in the view and received the following error:

Warning: Invalid argument supplied for foreach() in /home/content/69/7529569/html/wp-content/plugins/datafeedr/datafeedr.php(1981) : eval()'d code on line 8

I also tried putting the function in functions.php after removing the php beginning and ending markers and then the bottom view code alone in the view but I then received a parse error.

Thanks

Eric May 3rd, 2011 08:08 AM

Quote:

Originally Posted by meperez (Post 20360)
Thanks Eric, I tried putting all of that code in the view and received the following error:

The function is supposed to go in your theme's functions.php file and the html code is supposed to go into your view.

meperez May 4th, 2011 11:50 PM

Yes, I also tried putting the function in functions.php after removing the php beginning and ending markers and then the bottom view code alone in the view but I then received a parse error.

Thanks,
Melissa

Eric May 5th, 2011 08:58 AM

Hi

I think your query is completely wrong. I did this and it worked:

Code for theme's functions.php 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 '';
    foreach (
$brands as $brands) {
        
$html .= '<option value="'.$brands->brand.'">'.$brands->brand.'</option>';
    }
    return 
$html;


Code for your view:
HTML Code:

<form method="get">
        <select name="brand">
                <?php echo dfr_get_brands(); ?>
                <input type="submit" value="Go" />
        </select>
</form>


fashion May 14th, 2011 08:05 AM

Im a bit confused, where did you put this code?

Eric May 14th, 2011 08:47 AM

Quote:

Originally Posted by fashion (Post 20689)
Im a bit confused, where did you put this code?

The function dfr_get_brands() { ... } goes in your theme's functions.php file.
The other HTML code goes in one of your Views.

digitalatom August 17th, 2011 10:29 PM

Help please - I have just pasted The function dfr_get_brands() { ... } goes in your theme's functions.php file. (I assume you meant the wordpress theme)

and... HTTP Error 500 (Internal Server Error) - really not sure what to do - HELPP

Eric August 18th, 2011 08:58 AM

Sounds like you either didn't paste the whole thing or you pasted outside of the opening/closing php tags (ie. <?php ... ?>)

digitalatom August 18th, 2011 10:16 AM

Hi Eric,

The code I pasted in was:
function dfr_get_brands() {
global $wpdb;
$brands = $wpdb->get_results("SELECT brand FROM ".$wpdb->prefix."dfr_shop_products GROUP BY brand");
$html = '';
foreach ($brands as $brands) {
$html .= '<option value="'.$brands->brand.'">'.$brands->brand.'</option>';
}
return $html;
}

How do I now get the site back live - as I cant login to my wordpress theme

www.designerlabeldeals.co.uk

Any help to get it back up and running would be really appreciated

Thanks

Eric August 18th, 2011 10:18 AM

The first step to getting your site back online is to remove all of the code your pasted into your functions.php file and save it again. Then you will be able to log back into your site.

digitalatom August 18th, 2011 10:26 AM

Hi Eric - fortunately my son has just arrived back and got the site live again - doing exactly what you just suggested.

Many thanks

meinehandtasche January 17th, 2012 03:01 PM

Store ID: 32722
Website URL: http://www.meinehandtasche.de/
URL to page in question: http://www.meinehandtasche.de/store/


Hi Eric,

I copied this code into the functions.php of my wordpress theme (lifestyle from studiopress):

Quote:

<?php
//Function to Get Brands
function dfr_get_brands() {
global $wpdb;
$merchants = $wpdb->get_results("SELECT DISTINCT p.brand FROM ".$wpdb->prefix."dfr_shop_products p, ".$wpdb->prefix."wp_dfr_shop_p2c pc, ".$wpdb->prefix."wp_dfr_shop_cats c AND pc.cat_id = c.id AND c.name = \'Woks\' ORDER BY c.name, p.brand");
$html = '';
foreach ($brands as $brands) {
$html .= '<option value="'.$brands->brand.'">'.$brands->brand.'</option>';
}
return $html;
}
?>
and I copied this code into a new view category widget within the datafeedr factory:

Quote:


<!-- code for your vuew -->
<form method="get">
<span>
<?php echo dfr_get_brands(); ?>
<input type="submit" value="Go" />
</span>
</form>
but it doesn't look like it should. Please scroll down on this page
http://www.meinehandtasche.de/store
and you can see the black list of words under the header "MARKEN".

I don't have a clue actually what this list is showing. The result should look like under the header "MARKEN FILTER".

Do you have any idea what went wrong?

Thanks,
Oliver

Eric January 17th, 2012 03:13 PM

Quote:

Originally Posted by meinehandtasche (Post 27933)
Do you have any idea what went wrong?

I don't think you copied the code correctly. The code you need in your functions.php file is in post #8.

Also, it looks like the correct code for your view is this:
HTML Code:

<form method="get">
        <select name="brand">
                <?php echo dfr_get_brands(); ?>
                <input type="submit" value="Go" />
        </select>
</form>

Eric

meinehandtasche January 17th, 2012 03:42 PM

Store ID: 32722
Website URL: http://www.meinehandtasche.de/
URL to page in question: http://www.meinehandtasche.de/store/


Thank you Eric! :-) Now the drop down shows all brands.

Is there a way to manage which brands should show up in the drop down?

Additionally the brands show up in different style (some capital letter some small etc.).

Thanks,
Oliver

Eric January 17th, 2012 03:55 PM

Hi
Quote:

Originally Posted by meinehandtasche (Post 27939)
Is there a way to manage which brands should show up in the drop down?

Not without modifying the query and adding WHERE NOT IN() exceptions.

Quote:

Originally Posted by meinehandtasche (Post 27939)
Additionally the brands show up in different style (some capital letter some small etc.).

That's how the merchant's have entered the brand names. You could control that with CSS or PHP.

Eric January 17th, 2012 04:29 PM

Here's an example of how to remove brands from the list. I've removed 'Adidas' and 'Atlas'. This example also shows how to capitalize the first letter in each word of the brand.
PHP Code:

function dfr_get_brands() {
    global 
$wpdb;
    
$brands $wpdb->get_results("SELECT brand FROM ".$wpdb->prefix."dfr_shop_products 
    WHERE brand NOT IN('Adidas','Atlas') 
    GROUP BY brand"
);
    
$html '';
    foreach (
$brands as $brands) {
        
$html .= '<option value="'.$brands->brand.'">'.ucwords($brands->brand).'</option>';
    }
    return 
$html;




All times are GMT -5. The time now is 11:59 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.