Jump to content


Photo

best way to loop within loops?


  • Please log in to reply
2 replies to this topic

#1 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 17 October 2006 - 06:58 PM

I created this template system I use so I can set loops like {loop} info {/loop} and i can run 'info' as many times as I want.
The problem I came up with recently was looping within loops. I created some functions for 'child_loops' but I'm not sure this is the best way to go.

I was thinking maybe just using a series of arrays could do the trick but I tend to lose my mind quickly with arrays. What I was thinking I could do is save all the info like this: (array print)
Array:
0 => array (0 => "TEST", 1 => "info {CHILD} {info} {/CHILD}");
1 => array ("TEST" => "CHILD", "info" => "new info");

then run through the arrays. I think I'll need it to run the array backwards so I can find child loops first right? So it can find "TEST" in the array before it and replace "CHILD" loops with "info". .. .. hope I didn't confuse anyone. This is the best way I can think to do it but it's just driving me crazy.
And then that will only hold the loops drawn from the original code and I'll have to change that so I'll have to create a temporary array to hold the new info and replace the original info in the first array.
This is 2 much for me to comprehend (I have a headache). I'm trying real hard to figure out where to start lol. If anyone wants to toss me some code to run with then I'll be greatful.

#2 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 17 October 2006 - 09:44 PM

If it's the best way or not... who knows - but it's a suggestion.
I point out that i don't know what your template looks like, but i guess you can understand my idea here:


<!-- template.html -->

1: {info}
2: {info}
3: {info}
4: {info}
5: {info}

<!-- end template.html -->


<?php

$lines = file('template.html');
$num = 0;

$query = mysql_query("select * from info");
while(row=mysql_fetch_array($query))
{
$lines[$num] = str_replace("{info}", $row["info"], $lines[$num]);
$num++;
}

foreach($lines as $line => $value)
{
  if(!stristr($value, '{info}'))
  {
   $html .= $value;
  }
}

echo $html;

?>



#3 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 17 October 2006 - 11:26 PM

you do it by line? .. hmm.

See the idea i'm thinking is like with forums, we have categories and forums. We'll we need to loop through the categories and then through the forums in the current category.

I have found a way to do it without using any more arrays then I already had. I messed with it for a few hours after I posted this. Now it runs like a charm (so far).

I'm sure there is a way to run the loops like I mentioned though. I'm not sure it's the best way now (now that I have this way working) but i'm still interested about how much slower or faster it will run through.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users