|
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 |
meddle Smarty n00b
Joined: 11 Jun 2003 Posts: 2 Location: Barcelona
|
Posted: Wed Jun 11, 2003 3:04 pm Post subject: sorting template output from appended array |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Jun 11, 2003 3:21 pm Post subject: |
|
|
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 |
|
meddle Smarty n00b
Joined: 11 Jun 2003 Posts: 2 Location: Barcelona
|
Posted: Wed Jun 11, 2003 3:34 pm Post subject: |
|
|
thanx a lot for clarity and speed _________________ [meddle] |
|
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
|