Zen Cart Logo
Forums / General Questions / Get max value from attributes values

Get max value from attributes values

Views: 748

Results 1 to 7 of 7
14 Oct 2012, 10:52 AM
#1
tips007 avatar

tips007

New Zenner

Join Date:
Oct 2012
Posts:
52
Plugin Contributions:
0

Get max value from attributes values

hello
in checkout_confirmation file, i find the code nl2br(zen_output_string_protected($order->products[$i]['attributes'][$j]['value']))
i want to use max() for my attributes values (all my attributes avlues are numbers)
i tried echo max(zen_output_string_protected($order->products[$i]['attributes'][$j]['value']))
and echo max(array(zen_output_string_protected($order->products[$i]['attributes'][$j]['value'])))
they don't work
any ideas?

15 Oct 2012, 5:22 AM
#2
tips007 avatar

tips007

New Zenner

Join Date:
Oct 2012
Posts:
52
Plugin Contributions:
0

Re: Get max value from attributes values

each product has only one attribute, all the attribute values are numbers

ex.
product A with attribute value 6
product B with attribute value 3
product C with attribute value 8

if i add <? php echo zen_output_string_protected($order->products[$i]['attributes'][$j]['value']); ?> to tpl_checkout_confirmation_default
it displays
638

if i add <? php echo zen_output_string_protected($order->products[$i]['attributes'][$j]['value']) . '<p>'; ?>
it displays
6
3
8

now i want to get the max number from 6, 3, 8

the code below doesn't work
max(array(zen_output_string_protected($order->products[$i]['attributes'][$j]['value'])))
max(zen_output_string_protected($order->products[$i]['attributes'][$j]['value']))

is there anyone could help? thanks

16 Oct 2012, 3:34 PM
#3
tips007 avatar

tips007

New Zenner

Join Date:
Oct 2012
Posts:
52
Plugin Contributions:
0

Re: Get max value from attributes values

use the code below

<? php 
for ($i=0, $n=sizeof($order->products); $i<$n; $i++) { 
   for ($j=0, $n2=sizeof($order->products[$i]['attributes']); $j<$n2; $j++) {
      echo zen_output_string_protected($order->products[$i]['attributes'][$j]['value']);
   }
}
?>

you'll get the result
638

if you add the <p> to it, such as echo zen_output_string_protected($order->products[$i]['attributes'][$j]['value']) . '<p>';
you'll get
6
3
8

now i want to get the max number from 6, 3, 8
any one could help?

17 Oct 2012, 5:47 AM
#4
tips007 avatar

tips007

New Zenner

Join Date:
Oct 2012
Posts:
52
Plugin Contributions:
0

Re: Get max value from attributes values

anyone?

17 Oct 2012, 6:22 AM
#5
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Get max value from attributes values

$attrib_max = 0;
for ($i=0, $n=sizeof($order->products); $i<$n; $i++) { 
   for ($j=0, $n2=sizeof($order->products[$i]['attributes']); $j<$n2; $j++) {
      $attrib_max = ($attrib_max > $order->products[$i]['attributes'][$j]['value'])? $attrib_max: $order->products[$i]['attributes'][$j]['value'];
   }
}
``` $attrib_max will end up as the max of the attribute values.
17 Oct 2012, 8:09 AM
#6
tips007 avatar

tips007

New Zenner

Join Date:
Oct 2012
Posts:
52
Plugin Contributions:
0

Re: Get max value from attributes values

you are a genius!
thank you

17 Oct 2012, 3:01 PM
#7
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Get max value from attributes values

A simpler version of the comparison uses the PHP max() function:```php
$attrib_max = max($attrib_max, $order->products[$i]['attributes'][$j]['value']);