|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
janmartin Smarty Rookie
Joined: 28 Mar 2004 Posts: 7 Location: nearby London, UK
|
Posted: Sun May 09, 2004 12:08 pm Post subject: MODIFIER: count serialized array |
|
|
Lets assume you stored some data (e.g. ticked checkboxes of a form) in a database using serialize.
When displaying an overview of the data you like to dislpay how many checkboxes has been ticked.
1. try:
{foreach key=key item=item from=$data}
{$item}
{/foreach}
displays:
a:5:{i:0;s:1:"3";i:1;s:2:"21";i:2;s:2:"22";i:3;s:2:"49";i:4;s:3:"101";}
2. try:
{$item|count}
always displays
1
Sloution:
{$item|countserializedarray}
outputs:
5
save this file as modifier.countserializedarray.php in smary plugin directory to make it work
Code: | <?php
/*
* Smarty plugin
* -------------------------------------------------------------
* File: modifier.countserializedarray.php
* Type: modifier
* Name: countserializedarray
* Purpose: counts number of values in the serialized string
* -------------------------------------------------------------
*/
function smarty_modifier_countserializedarray($string) {
if (empty($string)) {
return '0';
} else {
return count(unserialize($string));
}
}
?> |
Any comments? |
|
Back to top |
|
|
|
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
|