Categories > TinyButStrong general >

Loop inside other loop

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Jeff Hardy
Date: 2005-02-11
Time: 21:44

Loop inside other loop

Hi,

I have something like this:

$sql = $db->sql_query("SELECT * FROM table WHERE id = $id");
while ($linha = $db->sql_fetchrow($sql))
{
  $var[] = array('camp1'=>$linha['camp1']);
  $sql2 = $db->sql_query("SELECT * FROM table2 WHERE sub_id = " . $linha['sub_id']");
  while ($linha2 = $db->sql_fetchrow($sql2))
  {
    $var2[] = array('y0zz'=>$linha2['y0zz']);
  }
}

In the template i tryed this:

<tag_loop>
blabla
<sub_tag_loop>
blabla
</sub_tag_loop>
</tag_loop>

but it didn't worked :(
Any idea how can i do this?
PS: sorry for my poor english
By: Skrol29
Date: 2005-02-11
Time: 22:59

Re: Loop inside other loop

Hello,

I'm sorry, I don't undesterand your problem.
By: Jeff Hardy
Date: 2005-02-12
Time: 00:18

Re: Loop inside other loop

I'm trying do get something like this:

Block 1
- sub block 1.1
- sub block 1.2
- sub block 1.3
Block2
- sub block 2.1
- sub block 2.2
Block 3
- sub block 3.1
Block 5
- sub block 5.1
- sub block 5.2
- sub block 5.3
- sub block 5.4

First i have to query the database do get the "blocks" and then in each block show the "sub blocks".

Hope you understood, tks for your help.
By: Skrol29
Date: 2005-02-12
Time: 13:02

Re: Loop inside other loop

This is easy to do with TBS sub-blocks. But you have to arrange your data in order to have a link between $var and $var2 items.
For example, you can change:
  $var[] = array('camp1'=>$linha['camp1'],'sub_id'=>$linha['sub_id']); // or $var[] = $linha;
  ...
  $var2[$linha['sub_id']][] = array('y0zz'=>$linha2['y0zz']);

Then your Html can be:
<table>
  <tr><td>[bm.camp1;block=tr;extend=1]</td></tr>
  <tr><td>[bs.y0zz;block=tr;p1=[bm.sub_id]]</td></tr>
</table>

Php:
$TBS->MergeBlock('bm',$var);
$TBS->MergeBlock('bs','array','var2[%p1%]'); // assume that $var2 is a global variable
By: Jeff Hardy
Date: 2005-02-12
Time: 14:31

Re: Loop inside other loop

Thank you!
It worked ;)