Jump to content

Looping issue (repeating last item of the array)


gijew

Recommended Posts

I'm writing a small template class. Easy - works. I decided to get tricky and add a modifier to the template variables. The whole thing is too much to get in to so I'll try and keep it small - hopefully someone can understand my ramblings ;)

 

Example tpl var

::content->linkset::

 

Example tpl var w/modifier

::content->linkset[5]::

 

Using the modifier I would get (x) amount of records from an array. That code all works too but before I continue I should add that once all of the template variables have been replaced it writes everything to a file for caching purposes.

 

The problem is when the template contains more than one modifier it repeats the last one over and over again in the final output (and yes, it repeats the last one in the array). Experience has taught me that the solution would be to reset the counter before it loops again but for whatever reason it's not working.

 

Here's some code to see if someone can spot the problem. I took out a few things but this should be the gist. Thanks in advance.

 

<?php
// read from tpl
$file	= $var['path'] . $var['themes'] . $tpl . '/' . $var['file_index'];
$tplr 		= fopen($file, 'r');
$template	= fread($tplr, filesize($file));
// special output
if (preg_match_all('/(::content->linkset\\[([0-9]*)\\]::)/', $template, $results)) {
// open file for writing
$tplw	= fopen($file, 'w');
// loop through results
for ($i = 0; $i < count($results); $i++) {
	$output = fwrite($tplw, preg_replace('/(::content->links_body\\[([0-9]*)\\]::)/', $this->content_linkset('body', $results[2][$i]), $template));
}
}
?>

 

If for whatever reason you want to see the entire code I'll post it. It's not horribly long (48 lines) but I know how frustrating it is to sift through all that crap when you're trying to help ;)

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.