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

sorting template output from appended array

 
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 -> General
View previous topic :: View next topic  
Author Message
meddle
Smarty n00b


Joined: 11 Jun 2003
Posts: 2
Location: Barcelona

PostPosted: Wed Jun 11, 2003 3:04 pm    Post subject: sorting template output from appended array Reply with quote

Hi all, soorry if this has been answered before, been searching through the forums and didnt find it.

Smarty 2.4.2
PHP 4.2.3

I need to sort an array generated in a loop like this:

Code:
   foreach($TITOLS as $tocompare) {
      $first = $daExplodedArray[2];
      $second = $tocompare;
      $i = similar_text($first, $second, &$p);
         if ($p > 35) { // add to smarty template output
              $p = round($p,2);
               $smarty->append('SIMILARS', array(
                       'blogSubject' => $second,
                       'perc' =>  $p
                     ));
         }
         else {
         }
   }


So basically I have an array with two dimensions, blogSubject and perc. If in the html I want to sort the array by perc, how would I use the section? I currently use:

Code:
{section name=nr loop=$SIMILARS}
{$SIMILARS[nr].perc}% - {$SIMILARS[nr].blogSubject}<br />
{/section}


Or is there any PHP builtin function to do that? I suppose I could do a foreach first then pass the values to a temp array, sort that array in php and then do a new foreach to enter the if that stores via append, right? But maybe I can save one foreach. TIA for any help.
_________________
[meddle]
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Jun 11, 2003 3:21 pm    Post subject: Reply with quote

the sorting should be done in php before and not in the template. see http://php.net/usort for an example how to sort multidimensional arrays. after sorting assign the sorted array as a template-var and do one foreach over that.

HTH
messju
Back to top
View user's profile Send private message Send e-mail Visit poster's website
meddle
Smarty n00b


Joined: 11 Jun 2003
Posts: 2
Location: Barcelona

PostPosted: Wed Jun 11, 2003 3:34 pm    Post subject: Reply with quote

thanx a lot for clarity and speed Wink
_________________
[meddle]
Back to top
View user's profile Send private message Visit poster's website
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 -> General 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