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.
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}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}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. 😊
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).
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.