Displaying templated search results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Displaying templated search results

Post 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!
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post 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 :)
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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.
Post Reply