RE: [SMARTY] Re: this mysql query with smarty
| From: | Isaac Vita Kohn | Date: | Wed, 17 Mar 2004 19:35:15 +0000 |
| Subject: | RE: [SMARTY] Re: this mysql query with smarty | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-11142@lists.php.net to get a copy of this message | ||
Hi,
> {section name=i loop=$menu_data}
> {if $menu_data[i][1] eq "1"}
> id: {$menu_data[i][0]}<br>
> {section name=ii loop=$menu_data}
> {if $menu_data[ii][3] eq $menu_data[i][2] and $menu_data[ii][1] eq "0"}
> id1: {$menu_data[ii][0]}<br>
> {/if}
> {/section}
> {/if}
> {/section}
>
> Is it OK to use this many conditions in templates?
If you really only need one level of depth -- i.e. categories and
subcategories -- this is absolutely fine.
For a more general solution for hierarchical trees, try something like
this. (But not on large tables with deep nesting; recursion will be slow
and you'll eat up as much memory as it takes to store two copies of the
table.)
<?php
// assuming your fields are 'name', 'id', 'subid'
// and all top-level rows have subid=0
function get_subs (&$menu_data, $parent_id, $parent_depth)
{
$output = array();
foreach($menu_data as $row)
{
if($row['subid'] == $parent_id)
{
$output[] = array('name' => $row['name'],
'depth' => $parent_depth+1);
$output += get_subs($menu_data, $row['id'], $parent_depth+1);
}
}
return $output;
}
$unraveled = get_subs($menu_data, 0, 0);
$smarty->assign("unraveled", $unraveled);
?>
{* Smarty *}
{strip}
{foreach from=$unraveled item=row}
{section name=whatever loop=$row.depth}
{/section}
{$row.name}
<BR />
{/foreach}
{/strip}
-----Original Message-----
From: Monte Ohrt [mailto:monte@ispi.net]
Sent: Wednesday, March 17, 2004 10:54 AM
To: dr. zoidberg
Cc: Smarty General
Subject: Re: [SMARTY] Re: this mysql query with smarty
It is not trivial to traverse and display a tree of data with just a
parent/child id. For one, there is no way to query your data in the
correct order from the database (unless the db specifically supports
this) in one query, it has to be unraveled. Then you have to figure out
the depth (indent level) for each element.
One way to overcome this (not perfect, but works) is to store the depth
and order in the database with each element. This way they can be pulled
with one select statement (order by) and the depth tells how to indent
them in the display. The only downside to this approach is that if you
insert a new element, the order must be updated (+1) for all elements
following the new one. That can also be done with one query, but could
feasibly have to update a lot of records in the case of a newsgroup
spool or something equally large.
Another method is using a serial id like so:
AAA
AAB
AAC
AACAAA
AACAAB
AACAACAAA
AAD
Each ID is three letters in my example, which limits you to 26^3
combinations for uppercase A-Z (if you need more, use more chars or
positions). For a child node you tack on three new characters to the
parent id. It is easy to see the parent/child relationship, and also
easy to get the depth (serial length / 3). Its also easy to select
immediate or all child elements for a given parent. The only problem
with this approach is getting the info from the database in the correct
order in one query. You can't just order by serial id, you get the short
ones first.
Maybe someone can give you a better idea, good luck.
On Wed, 2004-03-17 at 09:20, dr. zoidberg wrote:
> Dr. Zoidberg wrote:
> >
> > My database structure is the simpliest one:
> >
> > +-----+--------+-------+
> > | id | subid | name |
> > +-----+--------+-------+
> > | 1 | 0 | cat |
> > +-----+--------+-------+
> > | 2 | 0 | cat |
> > +-----+--------+-------+
> > | 3 | 1 | subcat|
> > +-----+--------+-------+
> > | 4 | 2 | subcat|
> > +-----+--------+-------+
> > | 5 | 2 | subcat|
> > +-----+--------+-------+
>
> I'm googling for days but no solution. I manage to display categosries
> and subcategories using simple mysql query and sort data in template.
> Here is how my template looks like:
>
> {section name=i loop=$menu_data}
> {if $menu_data[i][1] eq "1"}
> id: {$menu_data[i][0]}<br>
> {section name=ii loop=$menu_data}
> {if $menu_data[ii][3] eq $menu_data[i][2] and $menu_data[ii][1] eq "0"}
> id1: {$menu_data[ii][0]}<br>
> {/if}
> {/section}
> {/if}
> {/section}
>
> Is it OK to use this many conditions in templates?
>
> --
> Please do not CC me
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php