Page 1 of 1

Displaying templated search results

Posted: Mon Dec 26, 2005 6:07 pm
by evilmonkey
Hello,

I have a search system and I'm using smarty to template them and make them look all nice and idiot-proof. The search results have to come up as pictures with some text underneath. Here's the problem: I have three tables that I'm working with. One for the whole page (I don't care about that one), One for each row, and one for each result. Each row will have three results on it. The problem is that I have no idea how to do this in smarty because smarty has no humane loop function. In the past (when I didn't have to break results down in groups of 3), I'd just dump all the results into a 2D array, something like this:

Code: Select all

searchresults Array=>
[0]=>Array
     ['username']=>evilmonkey
     ['picture'] => evil.jpg
[1]=>Array
    ['username']=>joeblow
    ['picture']=>joeblow.jpg
I'd pass this array to smarty, and loop using the section command, something like this:

Code: Select all

{section name=results loop=$results}
{$results[results].username}//gives evilmonkey on first run, joeblow on the second
{$results[results].picture}
This is of course an oversimplified example, but it goes till all the results are taken care of. The question is, how do I split them into groups of three?

Thanks!

Posted: Tue Dec 27, 2005 9:29 am
by trukfixer

Code: Select all

<tr>
  {section name=categories loop=$s_cols step=$max}
  {strip}
    <td>
      {section name=innercat start=$smarty.section.categories.index loop=$smarty.section.categories.index+$max}
      {strip}
        {$s_cols[innercat]}<br>
        {$s_col[innercat]}<br>
      {/strip}
      {/section}
    </td>
  {/strip}
  {/section}
  </tr>
is how we did it - I built up the arrays in the code first s_cols was an array containing the main category, an dthen sub categories, and we structured it so that they would display in 3 columns - so the $s_cols first section contained an array of 3 item arrays and each array contained an array of the data for each row, but that's how we did em, basically - hopefully with that section info I posted you can figure it out :)

Posted: Tue Dec 27, 2005 11:11 am
by evilmonkey
Okay, so you're suggesting a 3D array?

Code: Select all

results [0] => array
               [0] => array
                  [username] => evilmonkey
                  [pic] => evil.jpg
                [1] => array
                   [username] => joeblow
                   [pic] => joeblow.jpg
                [2] => array
                   [username] => foobar
                   [pic] => foobar.jpg
//and that's just for the first row for three results.