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

MySQL Nested For Forum

 
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
rutherfurdsolutions
Smarty n00b


Joined: 24 Sep 2013
Posts: 1

PostPosted: Tue Sep 24, 2013 2:05 am    Post subject: MySQL Nested For Forum Reply with quote

We have a forum we are building into our company website currently but are having some issues when working out how to build the main forum page. We have a forum structure with categories and forums.

Data Dictionary
forum_categories: cat_id, cat_name
forum_forums: forum_id, forum_name, description, topics, posts

We are wanting to have the end result laid out in the following style...

Code:

<table cellspacing="0" cellpadding="0" id="forum_name" width="100%">
      <tr>
         <td colspan="2"><strong>Category Name</strong></td>
         <td align="center"><strong>Topics</strong></td>
         <td align="center"><strong>Posts</strong></td>
      </tr>
      <tr>
         <td width="50px" align="center">&nbsp;</td>
         <td>
            <strong><a href="#">Forum name</a></strong><br />
            Forum description details go here.<br />
            <strong>Moderators: </strong>
            <em>
               <a href="#">Moderator 1</a>,
               <a href="#">Moderator 2</a>
            </em>
         </td>
         <td width="50px" align="center">1</td>
         <td width="50px" align="center">1</td>
      </tr>
   </table>
   <table cellspacing="0" cellpadding="0" id="forum_name" width="100%">
      <tr>
         <td colspan="2"><strong>Category Name</strong></td>
         <td align="center"><strong>Topics</strong></td>
         <td align="center"><strong>Posts</strong></td>
      </tr>
      <tr>
         <td width="50px" align="center">&nbsp;</td>
         <td>
            <strong><a href="#">Forum name</a></strong><br />
            Forum description details go here.<br />
            <strong>Moderators: </strong>
            <em>
               <a href="#">Moderator 1</a>,
               <a href="#">Moderator 2</a>
            </em>
         </td>
         <td width="50px" align="center">1</td>
         <td width="50px" align="center">1</td>
      </tr>
      <tr>
         <td width="50px" align="center">&nbsp;</td>
         <td>
            <strong><a href="#">Forum name</a></strong><br />
            Forum description details go here.<br />
            <strong>Moderators: </strong>
            <em>
               <a href="#">Moderator 1</a>,
               <a href="#">Moderator 2</a>
            </em>
         </td>
         <td width="50px" align="center">1</td>
         <td width="50px" align="center">1</td>
      </tr>
      <tr>
         <td width="50px" align="center">&nbsp;</td>
         <td>
            <strong><a href="#">Forum name</a></strong><br />
            Forum description details go here.<br />
            <strong>Moderators: </strong>
            <em>
               <a href="#">Moderator 1</a>,
               <a href="#">Moderator 2</a>
            </em>
         </td>
         <td width="50px" align="center">1</td>
         <td width="50px" align="center">1</td>
      </tr>
   </table>


I have tried many different techniques which I have found on these forums as well as others including JOINS, multi-dimensional arrays, etc. I have no problem using multi-dimensional arrays when I am generating a flat table of data but as soon as I need to produce a table with related sub data such as the above example we are currently working on I find it all comes crashing down.

Any assistance would be appreciated.
Back to top
View user's profile Send private message
IT_Services
Smarty Rookie


Joined: 30 Sep 2013
Posts: 9

PostPosted: Mon Sep 30, 2013 10:35 am    Post subject: Reply with quote

In my optinion you have two possibilities:

    Prepare all data and provide it as nested array
    Provide a flat table


If you prepare the data and assign one array to the template you only need to loop through the array with foreach (and also all nested arrays):

PHP:
Code:
$example_structure = array(
    'Category 1' => array(
        'Forum 1' => array(
            'Description' => 'All information and much more',
            'Moderators' => array(),
        ),
        'Forum 2' => array(
            'Description' => 'No information',
            'Moderators' => array(),
        ),
    ),
     'Category 2' => array(
    ...
    )
);


Template:
Code:
{foreach ...}


Using a flat table needs the use of ugly if-conditions:

Mysql-query: SELECT cat_id, cat_name, forum_id, forum_name, description, topics, posts, moderator_name FROM categories JOIN ...

PHP
Code:
$smarty->assign('data', $mysql_result);


Template:
Code:
{$cur_cat_id=''}
{$cur_forum_id=''}
{foreach $data as $cat}
{if $cur_id != $cat.cat_id}
Kategorie: {$cat.cat_name}
{$cur_cat_id=$cat.cat_id}
{/if}
{if $cur_forum!= $cat.forum_id}
Forum: {$cat.forum_name}
{$cur_forum_id=$cat.forum_id}
...
{/foreach}


I would prefer the first solution Wink
Back to top
View user's profile Send private message
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