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

Sorting categories/forums problem

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
ramH
Smarty n00b


Joined: 04 May 2003
Posts: 2

PostPosted: Sun May 04, 2003 3:01 pm    Post subject: Sorting categories/forums problem Reply with quote

I have been playing and using smarty for a while. I built a kind of url database and used smarty as template. Bu now I want to go further and create a forum. I know it has been before but i consider it a nice challenge. Smile

But I bumped against a problem and thatswhy I am here. In a forum you have forumcategories and subforums. But how do I display that using smarty. I use this sql-query to get the data form my database:
Code:
SELECT
   c.id AS category_id, c.category AS category_category, l.url AS link_url, l.description AS link_description, l.category AS link_category
FROM
   category AS c, links AS l
WHERE
   c.id = l.category
ORDER BY
   c.category

In PHP I used to use this while-for-loop to display it:
Code:
$lastcat = "";
while($row = mysql_fetch_array($result_links))
{
   if($lastcat != $row['category_id'])
      {
         print "<br /><b>" . $row['category_category'] . "</b><br />\n";
         $lastcat = $row['category_id'];
      }
   print "<a href=\"" . $row['link_url'] . "\" target=\"_blank\">" . $row['link_description'] . "</a><br />\n";
}

And that gives this result in my browser:
Code:
Category 1
Forum A

Category 2
Forum B
Forum C

But how do I do this using smarty? Because I can not find a function that can do the same as while as I would like. I tried using foreach but I actually do not understand the syntax of that function correctly because it does not work. Can you please help me? Smile
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun May 04, 2003 3:30 pm    Post subject: Reply with quote

Code:
while ( $row = mysql_fetch_array($result_links) ) {
    $rows[] = $row;
}

$smarty->assign('rows', $rows);

---

{section name=row loop=$rows}
    {if $rows[row].category_id neq $rows[row.index_prev].category_id}
        <br/><b>{$rows[row].category_id}</b><br/>
    {/if}
    <a href="{$row$[row].link_url}" target="_blank">{$rows[row].link_description}</a><br />
{/section}


Should get you started Smile

See: http://smarty.php.net/manual/en/language.function.section.php
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Sun May 04, 2003 3:32 pm    Post subject: Reply with quote

build up an array-structure that is easy to handle for smarty. example:
Code:

$lastcat = "";
while($row = mysql_fetch_array($result_links))
{
   if($lastcat != $row['category_id'])
      {
         $cats[$row['category_id']] = $row['category_category'];
         $forums[$row['category_id']] = array();
         $lastcat = $row['category_id'];
      }
      $forums[$lastcat][] = array('url'=>$row['link_url'],'description'=>$row['link_description']);
}

$smarty->assign('cats', $cats);
$smarty->assign('forums', $forums);


(don't try the if-last-cat-stuff inside the template, do it before in php, that's easier==better)

nest two loops inside your template, to dump the structure:
Code:

{foreach from=$cats key=cat_id item=categorie}
  {$categorie}
  {foreach from=$forums[$cat_id] item=forum}
  <a href="{$forum.link}">{$forum.description}</a>
  {/foreach}
{/foreach}


the above is not tested, but it should make clear what i mean.

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


Joined: 04 May 2003
Posts: 2

PostPosted: Mon May 05, 2003 2:02 pm    Post subject: Reply with quote

I tried boots' solution and that worked. Great! Smile
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon May 05, 2003 2:22 pm    Post subject: Reply with quote

There is a subtle difference with messju's solution -- all of the logic for processing the logical data is handled in the PHP code. Notice how his template has no {if} statement and merely deals with the presentation logic of formatting/outputing the already processed data.

This is a strategy that should be emulated as often as possible when writing your application and template logic.

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