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

Multiple Arrays (Is this Smarty or PHP?)

 
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
spib
Smarty Rookie


Joined: 06 Jul 2003
Posts: 7

PostPosted: Sun Jul 06, 2003 7:46 am    Post subject: Multiple Arrays (Is this Smarty or PHP?) Reply with quote

Hey Guys,

I have two db tables. The first is a list of catagories, the second is a list of items each of which has a catagory id. What is the best way to store this for it to be displayed in my template? What I'm looking for is to display:-

Cat 1
Item 1
Item 2

Cat 2
Item 1
Item 2

My initial thought was to grab a list of the catagories from the db and assign them as an array, then for each catagory select the relevant Items and assign these as an array with the name of the catagory:-

[php:1:baf97c0ee2]
<?php

function GetItemList()
{
$this->assign('Catagories', $this->GetCatagories());

foreach($this->get_template_vars('Catagories') as $Cat)
{
$this->assign('Cat_' . $Cat.Name,$this->GetCatItems($Cat.seq_no));
}
}
?>[/php:1:baf97c0ee2]

Is this the correct way to do it?
Back to top
View user's profile Send private message
spib
Smarty Rookie


Joined: 06 Jul 2003
Posts: 7

PostPosted: Tue Jul 08, 2003 10:20 am    Post subject: Reply with quote

Can anyone help with this? This is the only problem I have at the moment and I'm desperate to fix it. If my question might be better posted elsewhere then please let me know. Very Happy
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Tue Jul 08, 2003 7:46 pm    Post subject: Reply with quote

IMHO, this is not a Smarty question. The "best-way" to store anything is extremely dependant on its typical access/usage patterns meaning that there is no single answer to your question.

Never-the-less, you have options. For example, instead of making multiple, iterative calls to your db for each category, why not just join the tables in the query itself and have the db do the work--unless you REALLY know what you are doing, it is very difficult to do better than a SQL optimizer at retrieving related data from multiple tables. Having said that, you do have to make sure your db is properly built and maintained. IE. proper indexes and suitable de-normalizations (a full academic form 5 normlaization of your db is typically a bad choice for most production/on-line systems).

In this case, you'd get back a flat array which is much easier to process especially since it only requires a single loop. If the data in the array is ordered by categoryID then as you process the array you need only to determine if the current categoryID is different from the last categoryID to determine if you have to print a new title, etc.

I suggest you reconsider the order of assignment operations. In particular, try to avoid using Smarty as part of your application control logic (ie. the smarty assign followed by the get_tamplate_vars). In general, it is preferable to only assign the final product of your application processing to Smarty.

Good Luck
Back to top
View user's profile Send private message
spib
Smarty Rookie


Joined: 06 Jul 2003
Posts: 7

PostPosted: Tue Jul 08, 2003 10:31 pm    Post subject: Reply with quote

Thanks boots. I'm not particularly au fait with Databases but I think I follow what you're suggesting. Thanks for your help.
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