We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Display cheapest price

Development and Programming

IC360 (Oliver Cannell)'s avatar
IC360 (Oliver Cannell)
248 posts
15 years ago
IC360 (Oliver Cannell)'s avatar IC360 (Oliver Cannell)

I have 3 price custom fields and would like to display the cheapest price on the page.

Would it be better to use {if}{if:elseif}{/if} commands, or use PHP?

       
Ingmar's avatar
Ingmar
29,245 posts
15 years ago
Ingmar's avatar Ingmar

Either way should work although PHP might be a bit easier, certainly if you should ever decide to move beyond three fields in the future.

       
IC360 (Oliver Cannell)'s avatar
IC360 (Oliver Cannell)
248 posts
15 years ago
IC360 (Oliver Cannell)'s avatar IC360 (Oliver Cannell)

OK that sounds good. So here is my first attempt to read the “offer_price_inside” variable. This is an embedded template by the way, as it’s used a number of times on one page. I have used the {entry_id} tag to make each PHP variable unique.

I’m checking if the price variable exists using the {if} statement, otherwise applying a value of ‘0’.

{exp:channel:entries channel="offers" entry_id="{embed:entry_id}" dynamic="no"}

<?php

$offer_price_inside{entry_id} = {if offer_price_inside}{offer_price_inside}{if:else}0{/if};

echo $offer_price_inside{entry_id};

?>

{/exp:channel:entries}

But I get the following error on the page:

Parse error: syntax error, unexpected ‘{’ in /home/sites/domainname.co.uk/public_html/brain/expressionengine/libraries/Functions.php(640) : eval()’d code on line 5

PHP is switched on for the template, and set to Output.

       
hd 's avatar
hd 
156 posts
15 years ago
hd 's avatar hd 

If you are wanting to output the lowest value between three custom fields, try something like:

{exp:channel:entries channel="offers" entry_id="{embed:entry_id}" dynamic="no"}

<?php
    echo min(array("{custom_field_price_1}","{custom_field_price_2}","{custom_field_price_3}"));
?>

{/exp:channel:entries}
       
IC360 (Oliver Cannell)'s avatar
IC360 (Oliver Cannell)
248 posts
15 years ago
IC360 (Oliver Cannell)'s avatar IC360 (Oliver Cannell)

Wow that works a treat. Thanks a lot.

However, if there is no value in one of the fields, it’s outputting no value (as it thinks this is the lowest value, I assume).

Is there a way to not include fields with no values?

       
hd 's avatar
hd 
156 posts
15 years ago
hd 's avatar hd 

In that case try this… You’ll need an if here since there would be an error if a user left 0 or empty values for all three fields.

{exp:channel:entries channel="offers" entry_id="{embed:entry_id}" dynamic="no"}

<?php
    $arr = array_filter(array("{custom_field_price_1}","{custom_field_price_2}","{custom_field_price_3}"));
    if (count($arr)) echo min($arr);
?>

{/exp:channel:entries}
       
IC360 (Oliver Cannell)'s avatar
IC360 (Oliver Cannell)
248 posts
15 years ago
IC360 (Oliver Cannell)'s avatar IC360 (Oliver Cannell)

Awesome. Worked like a charm. Thanks a lot. 😊

       
IC360 (Oliver Cannell)'s avatar
IC360 (Oliver Cannell)
248 posts
15 years ago
IC360 (Oliver Cannell)'s avatar IC360 (Oliver Cannell)

OK, here’s a further development on this one. A bit tougher this time.

So… we can now display the Cheapest price for a given Entry, using the above code. But is it possible to output a list of Entries in Cheapest/Dearest price order?

I know it’s kind of adding another level to the PHP processing, but I’m sure it must be possible.

Thanks a lot to anyone who might know the answer. 😊

       
hd 's avatar
hd 
156 posts
15 years ago
hd 's avatar hd 

It’s possible with a little more PHP, You can store all values for each entry in an array, then sort by lowest price of the lowest prices.

I wouldn’t do this on anything that has a very large amount of entries (where only a subset is shown), because it would require processing on every entry (even if they are not shown).

Something like this:

<?php entries = array(); ?>
{exp:channel:entries channel="offers" entry_id="{embed:entry_id}" dynamic="no" limit="1000"}

<?php
    $arr = array_filter(array("{custom_field_price_1}","{custom_field_price_2}","{custom_field_price_3}"));
    if (count($arr)) {
        $lowest_price = min($arr);
    } else {
        $lowest_price = ''; 
    }
    
    $minval = $lowest_price;
    // add leading zeros
    while(strlen($minval) < 10)){
        $minval = '0'.$minval;
    }

// get variables into php
$title = <<<EOT;
{title}
EOT;
$url_title = <<<EOT;
{url_title}
EOT;
$body = <<<EOT;
{body}
EOT;

    // store the custom variables in an array
    $entries[$minval.'|'.$url_title] = array(
        'title'        =>    $title,
        'url_title'    =>    $url_title,
        'body'         =>    $body
        'lowest_price' =>    $lowest_price);
?>
{/exp:channel:entries} 

<?php
     
    // sort the array by the key (lowest price, then url_title)
    $entries = ksort($entries); 

    // output the results  
    foreach ($entries as $entry){
        echo "lowest price: $lowest_price <a href="/$url_title">$title</a> body: $body";
    }
?>

This could be done more efficiently with an upper-level sql query (or stored procedure), which I would suggest if you intend on having a large quantity of entries here (more than 1k).

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.