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

Generate menu from two tables

 
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
waro
Smarty Regular


Joined: 12 Jun 2008
Posts: 38

PostPosted: Fri Feb 24, 2017 9:17 am    Post subject: Generate menu from two tables Reply with quote

How do I gernerate menus from these two MySQL tables below:

Table `categories`
Code:
+-----+----------+
| cid |   name   |
+-----+----------+
|   1 | About    |
|   2 | Services |
|   3 | Books    |
|   4 | Authors  |
|   5 | Contact  |
+-----+----------+


Table `pages`
Code:
+---------+-----+-----------------------+
| page_id | cid |   title               |
+---------+-----+-----------------------+
|   1     |  1  | Who We Are            |
|   2     |  1  | Organization          |
|   3     |  2  | Web Development       |
|   4     |  2  | Consultation          |
|   5     |  2  | Manual Writing        |
|   6     |  3  | The Game of Thrones   |
|   7     |  3  | A Dance with Dragons  |
|   8     |  3  | The Name of the Wind  |
|   9     |  4  | JRR Tolkien           |
|   10    |  4  | George RR Martin      |
|   11    |  4  | Brandon Sanderson     |
|   12    |  5  | Our Office            |
+---------+-----+-----------------------+


where each page_id correlated to its cid:

Code:
About
    Who We Are
    Organization
Services
    Web Development
    Consultation
    Manual Writing
Books
    The Game of Thrones
    A Dance with Dragons
    The Name of the Wind
Authors
    JRR Tolkien
    George RR Martin
    Brandon Sanderson
Contact
    Our Office
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Feb 24, 2017 4:15 pm    Post subject: Reply with quote

This is the simplest but least efficient way:
Code:
$stmt = $pdo->prepare('SELECT * FROM `categories`');
$stmt->execute();
$smarty->assign('categories', $stmt->fetchAll());

$stmt = $pdo->prepare('SELECT * FROM `pages`');
$stmt->execute();
$smarty->assign('pages', $stmt->fetchAll());

$smarty->display('page.tpl');


Code:
{foreach $categories as $cat}
{$cat.name}
{foreach $pages as $page}
{if $page.cid === $cat.id}
    {$page.title}
{/if}
{/foreach}
{/foreach}
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