Jump to content

best way to loop within loops?


Jocka

Recommended Posts

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.
Link to comment
Share on other sites

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:
[code]


<!-- 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;

?>

[/code]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.