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

Problem with foreach and section both nested

 
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
DJ-Smooth
Smarty Rookie


Joined: 03 Apr 2004
Posts: 9

PostPosted: Fri Apr 23, 2004 12:22 pm    Post subject: Problem with foreach and section both nested Reply with quote

Hello.

I have this php code:
Code:


$query = "SELECT * FROM article_cat";
      $query = mysql_query($query);
      echo mysql_error();
      while ($row = mysql_fetch_array($query))
      {
         $article_cat[] = $row;
         
         $query1 = "SELECT * FROM article WHERE cat='".$row['id']."'";
         $query1 = mysql_query($query1);
         echo mysql_error();
         while ($row1 = mysql_fetch_array($query1))
         {
            $article[] = $row1;
                     
         }
      }
      
      $main->assign('article', $article);
      $main->assign('article_cat', $article_cat); 



and the template:

Code:


{foreach item=article_cat from=$article_cat name=article_cat}
{$article_cat.name}<br>
{$article_cat.description}<br><br>

{foreach item=article from=$article name=article}
{$article.id}<br>
{$article.title}<br>

{/foreach}
{/foreach}



it should display somthing like that:

cat1
- article
- article
cat2
- article.

this is result :

Verein
Vereinsartikel

1
Ein Test Artikel
Presse
Presseartikel über den Verein oder LAN-Partys

1
1
1
1
E
E
E
E
t
t
t
t
0
0
0
0
1
1
1
1


i've tried so many things but i don't get the result i've mentioned above.anybody can help?
thanks in advance
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Apr 23, 2004 12:39 pm    Post subject: Reply with quote

you should give your arrays and you loop-items different names. this:
{foreach item=article from=$article}
can cause problems, maybe you better choose
{foreach item=article from=$articles}

another point: you have to give your $articles-array another structure. if $article_cats is one array and $articles is another array, smarty cannot know easily, which categorie an article belongs to. consider something like this (untested):
[php:1:f47435c774]$query = "SELECT * FROM article_cat";
$query = mysql_query($query);
echo mysql_error();

$article_cats = array();
$articles = array();
while ($row = mysql_fetch_array($query))
{
$article_cats[] = $row;
$cat_id = $row['id'];
$query1 = "SELECT * FROM article WHERE cat='".$row['id']."'";
$query1 = mysql_query($query1);
echo mysql_error();
while ($row1 = mysql_fetch_array($query1))
{
$articles[$cat_id][] = $row1;
}
}

$main->assign('articles', $articles);
$main->assign('article_cats', $article_cats);
[/php:1:f47435c774]

this makes $articles an 2-dimensional array. each array key is an article_cat's id and each array value is itself an array of articles that belong in this article_cat.

in the template you can dump it like this then:
Code:
 {foreach item=article_cat from=$article_cats}
 {$article_cat.name}<br>
 {$article_cat.description}<br><br>
 
 {foreach item=article from=$articles[$article_cat.id]}
 {$article.id}<br>
 {$article.title}<br>
 
 {/foreach}
 {/foreach}


HTH
messju
Back to top
View user's profile Send private message Send e-mail Visit poster's website
DJ-Smooth
Smarty Rookie


Joined: 03 Apr 2004
Posts: 9

PostPosted: Fri Apr 23, 2004 9:33 pm    Post subject: Reply with quote

thanks a lot, i hope you have a nice weekend. cya!
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