Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

MODIFIER: count serialized array

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
janmartin
Smarty Rookie


Joined: 28 Mar 2004
Posts: 7
Location: nearby London, UK

PostPosted: Sun May 09, 2004 12:08 pm    Post subject: MODIFIER: count serialized array Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins 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