Re: Printing selective elements of an array
| From: | Marko Kind | Date: | Mon, 21 Jan 2002 23:01:28 +0000 |
| Subject: | Re: Printing selective elements of an array | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-2557@lists.php.net to get a copy of this message | ||
Hi Neil,
I've had a similar problem and I solved it by using two nested sections. This looks like:
{section name=artist_name loop=$artist_names}
<BR><BR><B> {$artist_names[artist_name]}</B><BR>
{section name=list loop=$search_artist}
<ahref="/product/catalogue_no/{$search_artist[artist_name][list].catalogue_no}">{$search_ artist[artist_name][list].product_title}</A><BR> {/section} {/section} where $artist_names is an array containing just the artist names and $search_artist is an array of arrays with the artist name as the first key and a unique product key as the second key. I didn't test the example above, I hope I didn't make any mistakes adapting my code to your problem. I'm sure there is a better solution than mine (I need two arrays where one might be sufficient) but it works for me... Does this help? Regards Marko Neil Galvin wrote:
Hi. I'm having a problem looping through an array and only printing out certain elements. My existing php code is as follows: for($i = 0; $i < count($search_artist); $i++)-- Marko Kind Alte Mühle 20a \ 22395 Hamburg \ Germany Phone/Fax: +49-40-6047779 mailto:marko.kind@web.de{ if($search_artist[$i][artist_name] == $search_artist[$i-1][artist_name]) { echo "<a href=\"/product/catalogue_no/".$search_artist[$i][catalogue_no]."\">".$search_artist[$i][product_title]."</A><BR> ";} else { echo "<BR><BR><B>".$search_artist[$i][artist_name]."</B><BR> "; echo "<ahref=\"/product/catalogue_no/".$search_artist[$i][catalogue_no]."\">".$searc h_artist[$i][product_title]."</A><BR> ";} }My non-working smarty code is: {section name=list loop=$search_artist} {if $search_artist[list].artist_name == $search_artist[list-1].artist_name} {* I have tried using index & index_prev to no avail. I have also tried creating an array and offseting it by one to use instead of [list] *} <a href="/product/catalogue_no/{$search_artist[list-1].catalogue_no}">{$search_ artist[list].product_title}</A><BR> {else}<BR><BR><B> {$search_artist[list].artist_name}</B><BR> <a href="/product/catalogue_no/$search_artist[list].catalogue_no">{$search_artist[list].product_title}</A><BR> {/if}{/section}Is there a way to offset the section name by one? Any help would be appreciated. Regards Neil --- You are currently subscribed to smarty as: marko.kind@web.de To unsubscribe send a blank email to leave-smarty-935846B@lists.ispi.net Visit Smarty's homepage at http://www.phpinsider.com/php/code/Smarty/