Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Want to check if the values are duplicate.

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
imdeveloper
Smarty n00b


Joined: 08 May 2019
Posts: 2

PostPosted: Wed May 08, 2019 8:15 am    Post subject: Want to check if the values are duplicate. Reply with quote

I want to check if the values coming in the array are duplicate , if its a same then increment the counter .
want to check if values returns is repeated for $iop->getPortName(). if its same then increment the counter .

Example of output which i want:
ABCD-Count-1
ABCD-Count-2
ABCD-Count-3
ABCD-Count-4
GEFH-Count-1
GEFH-Count-2
LMNO-Count-1
OKKH-Count-1

Below are the code :

{foreach from=$iops item=iop}
{assign var=wwpn value=$iop->getWWPN()}
{if $wwpn->_saved}
{assign var=switch value=$wwpn->getSwitches()}
{assign var=port value=$wwpn->getSwitchPorts()}
<tr class="{cycle values="odd,even"}">
<td>{$iop->getPortName()|escape}</td>
</tr>
{/if}
{/foreach}
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu May 09, 2019 7:41 am    Post subject: Reply with quote

I don't quite understand, why are you doing it in Smarty.
Other than that, your code does not do anything with counting and not representing the source data either.
Impossible to help.
Back to top
View user's profile Send private message
imdeveloper
Smarty n00b


Joined: 08 May 2019
Posts: 2

PostPosted: Thu May 09, 2019 11:31 am    Post subject: Reply with quote

I want the functionality to be implemented using smarty template only.
You can check the output example to see how i want the response .

if input array has data as given below
array= [ ABCD,
ABCD,
ABCD,
ABCD,
GEFH,
GEFH,
LMNO,
OKKH ]

Then output should return the count of occurrences as given below using smarty.
ABCD-1
ABCD-2
ABCD-3
ABCD-4
GEFH-1
GEFH-2
LMNO-1
OKKH-1
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu May 09, 2019 12:06 pm    Post subject: Reply with quote

Well, solve it in PHP first, then you can port the approach to Smarty.
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Thu May 09, 2019 6:22 pm    Post subject: Reply with quote

The following should work (but I haven't tried it):
Code:
PHP Code:
$array = ["red","blue","green","green"];
$arrValueCounts = array_count_values($array);
foreach($arrValueCounts as $k=>$v){
   for($i=1;$i>$v;$i++){
      echo $k."-".$i."\n";
   }
}

Smarty Code:
{$array=["red","blue","green","green"]}
{$arrValueCounts=$array|array_count_values}
{foreach $arrValueCounts as $k=>$v}
  {for $i=1 to $v}
{$k}-{$i}
  {/for}
{/foreach}
This assumes you have managed to get the $array populated with string values.

PHP has a function to count distinct values and is coded here as a variable modifier. (Smarty can use PHP functions as modifiers unless a Smarty security setting prohibits that capability.)

Then we create a simple loop for the number of times each distinct value was found.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP