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

nested foreach loops problem

 
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
anivix
Smarty n00b


Joined: 05 Jun 2009
Posts: 4

PostPosted: Sat Jun 06, 2009 1:21 pm    Post subject: nested foreach loops problem Reply with quote

i have been trying for 3 days to figure this one out but no luck.
hope someone can enlighten me.
so here goes.

i have 3 tables in my MySQL db:

Table1 : categories
id | name

Table2: projects
id | catId | name

Table3: images
id | projectId | image

the thing i cant manage at all is to display it in a 3 leveled nested foreach loop, like this:

Cat 1:
- project 1
--- image 1
--- image 2
--- image 3
- project 2
--- image 1
--- image 2
Cat 2:
- project 1
--- image 1
- project 2
--- image 1
--- image 2
Cat 3:
-- etc

how could i pull the data out and display it in that order?
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Sat Jun 06, 2009 2:40 pm    Post subject: Reply with quote

Use a SQL query to pull them out how you want, then just display them in Smarty with a foreach loop. Something like:

Code:
select * from categories c, projects p, images i
where c.id=p.catId and p.id=i.projectId
order by c.id,p.id,i.id
Back to top
View user's profile Send private message Visit poster's website
anivix
Smarty n00b


Joined: 05 Jun 2009
Posts: 4

PostPosted: Sat Jun 06, 2009 4:19 pm    Post subject: Reply with quote

i managed to make the first 2 loops work
i cant post code because "You must have 3 posts before you can post URL's/Links."

anyway the way i did it
in php there were 2 while loops

and in tpl file 2 foreach loops
everything is ok with that but i need the third one for the images and i dont really understand the query you gave me. dont know how to implement it in my code.

i will post php code as soon as i have 3 posts Neutral
im desperate for this T_T
Back to top
View user's profile Send private message
anivix
Smarty n00b


Joined: 05 Jun 2009
Posts: 4

PostPosted: Sat Jun 06, 2009 4:55 pm    Post subject: Reply with quote

i got it working finally.
phew.

ill post the code in the next post.
Back to top
View user's profile Send private message
anivix
Smarty n00b


Joined: 05 Jun 2009
Posts: 4

PostPosted: Sat Jun 06, 2009 4:59 pm    Post subject: Reply with quote

ok here is the code that works for my purpose.
feel free to comment and optimize it if u know how.
im open for better ways of doing it Smile

PHP:
Code:
$cats = array();
        $catSQL = mysql_query("SELECT * FROM adm_project_categories");
       
        while($rowCat = mysql_fetch_assoc($catSQL)) {
            $cat = array();
            $cat['id']   = $rowCat['id'];
            $cat['name'] = $rowCat['name'];
           
            $projects = array();
           
            $projectSQL = mysql_query('SELECT * FROM adm_projects WHERE catID = "'.$rowCat['id'].'"');
            while($projectsResult = mysql_fetch_assoc($projectSQL)) {
                $projectID = $projectsResult['id'];
                $project = $projectsResult;
               
                $images = array();
               
                $sqlIMG = mysql_query("SELECT * FROM adm_project_img WHERE projectID = '$projectID'");
                while($imgResult = mysql_fetch_assoc($sqlIMG)) {
                    $images[] = $imgResult;
                    $project['images'] = $images;
                }
                $projects[] = $project;
            }
            $cat['categories'] = $projects;
            $cats[] = $cat;
        }
        $style->assign('cats', $cats);
        $style->display('projects.tpl');


TPL file:
Code:
{foreach from=$cats item=cat}

<p>{$cat.name}</p>

  {foreach from=$cat.categories item=allprojects}

    <p>{$allprojects.name}</p>

    {foreach from=$allprojects.images item=allimages}

      <img src="./images/projects/{$allimages.projectID}/{$allimages.img}" />

    {/foreach}

  {/foreach}

{/foreach}
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