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

foreach of section, that is the question

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


Joined: 24 Sep 2003
Posts: 8

PostPosted: Wed Sep 24, 2003 2:20 pm    Post subject: foreach of section, that is the question Reply with quote

HI

I'm a noob, but what from what i've seen so far of smarty is whey hey hey.

Very good bit of coding people.

I'm in a sticky situation.

i have an mysql table bob say i retrieve records to a variable and assign it in smarty say table.

Quote:
ID / subjectID / text
1 / 1 / hello 1
2 / 1 / hello 2
3 / 1 / hello 3
4 / 2 / hello world
5 / 10 / hello dummy thats me
6 / 10 / hello i'm hungry


what i cant get my head around, is to loop through all records and grouping by subjectID therefore

subject 1
hello 1
hello 2
hello 3

subject 2
hello world

subject 10
hello dummy thats me
hello i'm hungry

this should be possible
Back to top
View user's profile Send private message Send e-mail
inerte
Smarty Rookie


Joined: 24 Sep 2003
Posts: 9

PostPosted: Wed Sep 24, 2003 3:38 pm    Post subject: Reply with quote

Don't group on Smarty, group it on your code:

Code:

$results_grouped = array();
while (list($id, subject_id, $text) = mysql_fetch_array($res)) {
    $results_grouped[$subject_id][$id] = $text;
}


Now just {section} loop $results_grouped, and {section} again the $subject_id key (which is an array)
Back to top
View user's profile Send private message Visit poster's website
triggsley
Smarty Rookie


Joined: 24 Sep 2003
Posts: 8

PostPosted: Thu Sep 25, 2003 8:29 am    Post subject: Sorted it Reply with quote

Thanks for your help, i eventually resolved the problem
Code:

[b]php code:[/b]
$results_grouped = array();

while (list($id, $subject_id, $msg, $link) = mysql_fetch_array($result)) {

    $results_grouped[$subject_id][$id] = array (
             'id'=>$id,
              'msg'=>$msg,
              'link'=>$link);
}

$smarty->assign('group', $results_grouped);



[b]tpl code:[/b]
{foreach from=$group item=subject key=key}
<b>{$key}</b><br>

   {foreach from=$subject item=news_article key=key}

      {foreach from=$news_article item=detail key=key}
      {$detail.msg}<br>
      <b>{$detail.link}</b><br>
      {/foreach}

   {/foreach}

{/foreach}


in the third foreach i cant seem to get the indvidual array fields that i created, but i tried it with {key} = {detail} and i got the results, how can i get the results as shown in my tpl script?
Back to top
View user's profile Send private message Send e-mail
triggsley
Smarty Rookie


Joined: 24 Sep 2003
Posts: 8

PostPosted: Thu Sep 25, 2003 9:38 am    Post subject: not working Reply with quote

i cant seem to get this bit of code to work
Code:

{foreach from=$group item=subject key=key}
<tr>
   <td>
   <b>{$key}</b><br>
   </td>
</tr>
<tr>
   <td>
   {foreach from=$subject item=news_article}

      {foreach from=$news_article item=detail}
      <b>{$detail.title}</b><br>
      {$detail.brief}<br>
      {/foreach}

   {/foreach}
   </td>
</tr>
{/foreach}


this works
Code:

{foreach from=$group item=subject key=key}
<b>{$key}</b><br>

 {foreach from=$subject item=news_article key=key}

     {foreach from=$news_article item=detail key=key}
          {$key} = {$detail}<br>
     {/foreach}

 {/foreach}

{/foreach}


but i want to format the 3rd foreach, all i get is 1 character.
my array is:

Code:

$results_grouped = array();
while (list($id, $subject_id, $title, $brief) = mysql_fetch_array($region)) {
// echo "id = ".$id." / subject_ID = ".$subject_id." / title = ".$title." / brief = ".$brief."<br>";

$results_grouped[$subject_id][$id] = array (
            'id'   =>$id,
            'title'   =>$title,
            'brief'   =>$brief);
}


somethings not quite right, i just cant put my head around it.

many thanks

trigg
Back to top
View user's profile Send private message Send e-mail
triggsley
Smarty Rookie


Joined: 24 Sep 2003
Posts: 8

PostPosted: Thu Sep 25, 2003 9:54 am    Post subject: sorted it this time Reply with quote

yippie

Code:

{foreach from=$group item=subject key=key}
<b>{$key}</b><br>

   {foreach from=$subject item=news_article}

                <b>{$news_article.title}</b><br>
   {$news_article.brief}<br>

   {/foreach}

{/foreach}


i'm so tired, zzzzZZZzzzzz

kept me till 2 this morning, had a fresh go at it, now it works
Back to top
View user's profile Send private message Send e-mail
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