RE: [SMARTY] {section} with array that doesnt start with index 0

From: Date: Mon, 19 Aug 2002 15:05:56 +0000
Subject: RE: [SMARTY] {section} with array that doesnt start with index 0
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-5761@lists.php.net to get a copy of this message
Here's my approach to that (tried and tested in my current project): The solution to your problem a different table structure and combining your two queries into one with a LEFT JOIN. If you have a table of games, and a table a leagues, each with their own unique, auto-incrementing id fields, set up another table that sets the associations between the two. It would look like this: Table: leages_games +---------+-----------+ | game_id | league_id | +---------------------+ Then your query would look like this (not tested for this scenario but this is the basic idea): SELECT leagues.name AS leage_name, games.name FROM games LEFT JOIN leages_games ON leages_games.game_id = games.id LEFT JOIN leages ON leages_games.league_id = leages.id You could do some ordering in the query as well with an ORDER BY part, or just get the info and leagues for one game by adding a WHERE clause. Then once you get your query back, you as you loop over the result you can sort out a list of each league per game. Like so: ... database access code ... $result = array(); $leagues = array(); while($row = mysql_fetch_object($result_id)){ # you could use fetch_array $result[$row->id] = $row; #this will keep out redundancy if($row->league_name != NULL){ $leagues[$row->id][] = $row->league_name } } #I'm sure there's a way to do this in just one while loop, #but I had trouble with it over righting different items #so I separated it... no problems so far but we'll see what #happens as I start adding more and more information to the db #assign groups of leagues to each game: while(list($key, $val) = each($result)){ $result[$key]->leagues = $leagues[$key]; } then assign the $result array to a template variable, and it will be available in the template like this (assume we assigned it to $games): {foreach from=$games item=current} <b>{$current->name}</b><br> leagues: <ul> {foreach from=$current->leagues item=league} <li>{$league}</li> {/foreach} </ul> {/foreach} and that will display a list of games, and a list of leagues for each game. If there aren't any leagues for a game, the foreach won't display anything (I think, if not there's always {foreachelse}). And then you can do all sorts of neat stuff with the same basic code, like grab more information than just the name of the league in the query, or do another left join on a table that lists the users for each league, and display information about them as well. -- Josh -----Original Message----- From: Roman Neuhauser [mailto:neuhauser@bellavista.cz] Sent: Monday, August 19, 2002 10:05 AM To: 'Smarty General' Subject: Re: [SMARTY] {section} with array that doesnt start with index 0 > From: "Peter Billen" <peter@clueless.be> > To: "'Smarty General'" <smarty-general@lists.php.net> > Date: Mon, 19 Aug 2002 13:15:17 +0200 > Subject: RE: [SMARTY] {section} with array that doesnt start with index 0 > > I found another problem with not using primary keys as indices. > > Imagine you have 2 queries, 1 query to get the 'games' and another query > to get the 'leagues' from the database. In each game, they play an > undefined number of leagues. So a league is a sub-structure of a game. > > In the first query, you fill in the games into the array. Fe: > > $array[0]['game'] = "quake 1"; > $array[0]['game_id'] = 1; > $array[1]['game'] = "quake 2"; > $array[1]['game_id'] = 2; > .. > > In the second query, you have to fill in the leagues. However, you don't > know WHERE to fill it in, because you f.e. don't know where the game > "quake 1" is located (index 0? index 4? index ..?) > > So the Y in $array[Y]['leagues']['league_name'] = "Quake 1 International > Bla Bla League"; is unknown? > > If you had use primary keys as indices, you could have easily done: > > > $array[$row['game_id']]['leagues'][$row['league_id']]['league_name'] > = > "Quake 1 International Bla Bla League"; > > > Or am I wrong? no, you're perfectly right. i never said the array must be 0-based, non-associative during it's whole lifetime. just pull it through array_values() right before you pass it to Smarty::assign() -- FreeBSD 4.6-STABLE 4:03PM up 10 days, 3:58, 21 users, load averages: 0.01, 0.03, 0.00 -- Smarty General Mailing List (http://smarty.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.smarty.general (#5761) next »