Re: [SMARTY] presentation logic for presenting categorized data nicely
| From: | Jason Hubble | Date: | Sun, 07 Dec 2003 21:47:00 +0000 |
| Subject: | Re: [SMARTY] presentation logic for presenting categorized data nicely | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-10646@lists.php.net to get a copy of this message | ||
Tommy,
>
> I have two database-tables containing elements (link categories) and
> subelements (actual links) -
> More precisely I have the following db-structure (with sample data):
>
> <sql>
> CREATE TABLE linkcategories (
> id int(3) unsigned NOT NULL auto_increment,
> category varchar(255) NOT NULL default '',
> PRIMARY KEY (id)
> ) TYPE=MyISAM;
>
> INSERT INTO linkscat VALUES (1, 'Link Category 1');
> INSERT INTO linkscat VALUES (2, 'Link Category 2');
>
> CREATE TABLE links (
> id tinyint(3) unsigned NOT NULL auto_increment,
> catid tinyint(3) unsigned NOT NULL default '0', //references id in
category
> table
> url varchar(255) NOT NULL default '',
> title varchar(255) NOT NULL default '',
> description text NOT NULL,
> PRIMARY KEY (id)
> ) TYPE=MyISAM;
>
> INSERT INTO links VALUES (1, 2, 'URL1', 'TITLE1',
> 'DESCRIPTION1');
> INSERT INTO links VALUES (2, 1, 'URL2', 'TITLE2',
> 'DESCRIPTION2');
> INSERT INTO links VALUES (3, 2, 'URL3', 'TITLE3',
> 'DESCRIPTION3');
> </sql>
>
> I need to create the following output from the above data:
>
> <output>
> Link Category 1:
> - title2, url2, description2
>
> Link Category 2:
> - title1, url1, description1
> - title3, url3, description3
> </output>
I would recommend having a nested loop in your php code. First you have one
sql statement that will return the IDs of your categories:
SELECT id,category FROM linkcategories;
This will return 2 rows because you have 2 categories. I use a custome made
SQL wrapper so I don't remember the exact built in php syntax for interacting
with mysql--i.e. this example doesn't attempt to be correct but it should
illustrate the point if you are familliar enough with using this stuff:
while ($row = mysql_fetch_assoc_array($something)) {
//now create a create a sql statement for each cat that will gather
//all the information from the links table for that category
SELECT url, title, description FROM links WHER catid = {$row['id']}
$inf[$row['id']]['category'] = $row['category'];
$i = 0;
while ($row2 = mysql_fetch_assoc_array($something_else)) {
$inf[$row['id']]['links'][$i++] = $row2;
}
}
So you end up with an array structure that looks like this:
$inf[0]['category'] = "Link Category 1";
$inf[0]['links'][0]['url'] = "URL2";
$inf[0]['links'][0]['title'] = "TITLE2";
$inf[0]['links'][0]['description'] = "DESCRIPTION2";
$inf[1]['category'] = "Link Category 2";
$inf[1]['links'][0]['url'] = "URL1";
$inf[1]['links'][0]['title'] = "TITLE1";
$inf[1]['links'][0]['description'] = "DESCRIPTION1";
$inf[1]['category'] = "Link Category 2";
$inf[1]['links'][1]['url'] = "URL3";
$inf[1]['links'][1]['title'] = "TITLE3";
$inf[1]['links'][1]['description'] = "DESCRIPTION3";
$smarty->assign('links', $inf);
Here is how you use that structure in smarty:
{section name=i loop=$links}
Category: {$links[i].category} <hr />
{section name=t loop=$links[i].links}
{$links[i].links[t].url} {$links[i].links[t].title}
{$links[i].links[t].description} <br />
{/section}
{/section}
Hope that helps and sorry for the pseudo-code in php.
jason