|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
lsantos2000 Smarty n00b
Joined: 10 Jun 2004 Posts: 3
|
Posted: Thu Jun 10, 2004 10:03 pm Post subject: using foreach without repeating fields |
|
|
I have a variable $itemList wihic is an array of 3 elements each row
i.e.
category, role , name
cat1, role1, name1
cat1, role1, name2
cat1, role2, name3
cat2, role1, name1
cat3, role1, name2
and I want to print
category[1]
role[1] (name[1], name[2],... name[i])
role[2] (name[1], name[2],... name[i])
category[2]
role[1] (name[1], name[2],... name[i])
role[2] (name[1], name[2],... name[i])
.
.
.
category[i]
role[1] (name[1], name[2],... name[i])
role[2] (name[1], name[2],... name[i])
how to do this using foreach without repeating the categories ??
how could I concatenate the names? |
|
Back to top |
|
lsantos2000 Smarty n00b
Joined: 10 Jun 2004 Posts: 3
|
Posted: Fri Jun 11, 2004 7:40 pm Post subject: More info about my request |
|
|
>
> Hi,
>
> I have this array $itemList with 3 elements
>
> category, role, name
>
> ordered by category, role, name
>
> I want to display by the folllowing groups
>
> category
> rolesUnderCategory namesUnderRole
>
> So only one category at a time, list of roles under that category and concatenate names on that role
>
> This is all I am getting:
> ____________________________________
> Photography
>
> Invited to Quote John Dean
>
> Photography
>
> Invited to Quote Leonardo Santos-Macias (Purchaser)
>
> Photography
>
> Invited to Quote Peter MacCain
>
> Print
>
> Invited to Quote John Dean
>
> Print
>
> Invited to Quote Leonardo Santos-Macias (Purchaser)
>
> Print
>
> Invited to Quote Peter MacCain
> _______________________________
>
> To get the above, I am using:
>
> {foreach name=itemList item=element from=$itemList}
> <tr width="100%" class="td_header">
> <td colspan="5">{$element.itemCategory}</td>
> </tr>
> <tr width="100%">
> <td colspan="2">{$element.itemRole}</td>
> <td colspan="3">{$element.itemName}</td>
> </td>
> <tr>
> {/foreach}
>
> BUT I don;t get any groupings since my foreach doesn't know about groupings
>
> I would like to get:
> ___________________
>
> Photography
>
> Invited to Quote John Dean, Leonardo Santos-Macias (Purchaser),Peter MacCain
> Other role Name1, Name2
>
> Print
>
> Invited to Quote John Dean, Leonardo Santos-Macias (Purchaser),Peter MacCain
> Other role Name1, Name2
>
> Other category
> A role Name1, Name2
> ____________________________
>
> Is there a way to do a foreach DISTINCT element?
> or how would you do this?
> I appreciate your help in advance
>
> Thanks
>
> Leonardo |
|
Back to top |
|
lsantos2000 Smarty n00b
Joined: 10 Jun 2004 Posts: 3
|
Posted: Fri Jun 11, 2004 7:48 pm Post subject: posting reply from messju (thanks to him, got email) |
|
|
> Is there a way to do a foreach DISTINCT element?
not really.
> or how would you do this?
i wouldn't use a two-dimensional array (each row has 3 columns) as you do, but
i would setup a 3-dimensional array like:
for each categorie,role,name you have do:
$list[$categorie][$role][$i] = $name (with $i going from 0..n-1)
then you can nest your loops:
{foreach from=$list key=categorie item=roles}
{$categorie}
{foreach from=$roles key=role item=names}
{$role}: {$names|@implode:", "} {* or with a third foreach *}
{/foreach}
{/foreach}
it's much easier in php to prepare the array that way for smarty, than
trying to implement a "distinc-logic" inside the template.
HTH
messju |
|
Back to top |
|
colynb Smarty n00b
Joined: 12 Aug 2004 Posts: 4
|
Posted: Thu Aug 12, 2004 7:14 pm Post subject: Re: Is there a way to do a foreach DISTINCT element? |
|
|
I created a function that will do EXACTLY what you're looking for. I had a similar development problem a few months ago.
Code: |
// returns a true or false
function isFirst($field)
{
global $prev;
if ($field !== $prev) {
$GLOBALS['prev'] = $field;
return true;
} else {
return false;
}
}
// Implement it like this:
{if isFirst($myCategory)}
<h2>{$myCategory}</h2>
{/if}
.... rest of your code
|
It works for me, so try it out. |
|
Back to top |
|
|
|
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
|
|