od_
od_

Reputation: 183

Smarty, a total value not possible

with Smarty variable [{$ getCnt | var_dump}] I have the following values:

int (3) int (1) int (1)

But now I want to have the total value. That is, in this example: 5

PHP:

How can I objects in a variable counting?

I get that at in PHP with var_dump ($ getCnt).

object (selections) # 214 (4) {values​​} object (selections) # 215 (4) {values​​} object (selections) # 216 (4) {values​​} object (selections) # 217 (4) {values​​} object (selections) # 218 (4) {values​​}

There are total of 5 objects. How can I using PHP Find out how many objects has a variable?

Advance thank you!

Upvotes: 3

Views: 494

Answers (2)

Karthick Kumar
Karthick Kumar

Reputation: 2361

use {$getCnt|array_sum} using php function in smarty like this and get the total of array where $getCnt need to be array

check my code here

<?php
  $array=array(3,2,1);
  $smarty->assign('test',$array);
  $smarty->display('test.tpl');
?>

and in test.tpl

{$test|array_sum}

am using Smarty-3.1.12

Upvotes: 1

James Donnelly
James Donnelly

Reputation: 128791

I haven't used Smarty before so I'm not familiar with its syntax, but PHP has a built in array_sum() method which calculates the sum of the values stored within an array. Assuming $ getCnt is a variable:

$total = array_sum($getCnt);

See: http://www.php.net/manual/en/function.array-sum.php

Upvotes: 0

Related Questions