Jump to content

Recommended Posts

Right so ive been developing a template engine for awhile now and its turned into one massive class and ive hit a snag on it.

 

This snippet of code is supposed to compile a nested loop block, its working but theres a small snag...

 

Its supposed to output:

General
new.gif General Chat 1 0
new.gif Introductions 1 0

Tutorials
new.gif PHP Tutorials 1 0

 

But insted it is outputting:

General
new.gif General Chat 1 0
new.gif Introductions 1 0

Tutorials
new.gif General Chat 1 0
new.gif Introductions 1 0
new.gif PHP Tutorials 1 0

 

Its running off a multi-dim array that currently outputs to:

Array ( 
[1] => Array ( 
	[icon] => new.gif 
	[title] => General Chat 
) 

[2] => Array ( 
	[icon] => new.gif 
	[title] => Introductions 
) 
) 

Array ( 
[1] => Array ( 
	[icon] => new.gif 
	[title] => PHP Tutorials 
) 
)

 

As you probably noticed it looks all correct so im guessing it is a problem within the code itself and here is a snippet of it:

$this->current_nested_loop_block = $this->generate_block_ref_data( $variable_name_2, array( " BEGIN ", " END " ), "loop", $rough_loop );

foreach ( $variable_content_2 as $variable_name_nested => $variable_content_nested )
{									
$rough_nested_loop = $this->current_nested_loop_block[2];									

foreach ( $variable_content_nested as $variable_name_nested_2 => $variable_content_nested_2 )
{
	$rough_nested_loop = str_replace( $this->replacement_variable_prefix . $variable_name_2 . "." . $variable_name_nested_2 . $this->replacement_variable_suffix, $variable_content_nested_2, $rough_nested_loop );
}	

$parsed_nested_loop .= rtrim( $rough_nested_loop );
}

$this->current_nested_loop_block[3] = $parsed_nested_loop;
$rough_loop = str_replace( $this->replacement_loop_prefix . " BEGIN " . $variable_name_2 . $this->replacement_loop_suffix . substr( $rough_loop, $this->current_nested_loop_block[0], $this->current_nested_loop_block[1] ) . $this->replacement_loop_prefix . " END " . $variable_name_2 . $this->replacement_loop_suffix, $this->current_nested_loop_block[3], $rough_loop ); 	
$this->current_nested_loop_block = array( );

 

Basically it first captures the cat_row looping tags: <!-- BEGIN cat_row --> and <!-- END cat_row --> then writes the contents of whats between the tags to a variable named current_nested_loop_block, then it is supposed to loop each row array ( [1] => Array  ) etc, then its supposed to loop through each tag ( [icon] and [title] ) etc, and replace each {cat_row.tag} with the appropriate data.

 

So for each of the rows it searches for:

	<!-- BEGIN cat_row -->
<tr>
	<td><img src="{img_base_dir}icon_{cat_row.icon}" alt="{cat_row.icon}" /></td>
	<td>{cat_row.title}</td>
	<td>{cat_row.topic_count}</td>
	<td>{cat_row.reply_count}</td>
</tr>
<!-- END cat_row -->

 

And replaces it with the correct data and its supposed to do that for each of the rows.

 

Sooo anyone see the problem?!

Link to comment
https://forums.phpfreaks.com/topic/74205-solved-nested-looping/
Share on other sites

Here is the looping function:

	
<?php
function loop_replace( $loop, $vars = array( ), $error_line, $error_file )
{
	if ( $this->has_error == true )
	{
		return;
	}
	else if ( $this->has_error == false )
	{    
		$this->current_loop_block = $this->generate_block_ref_data( $loop, array( " BEGIN ", " END " ), "loop" );

		foreach ( $vars as $variable_name => $variable_content )
		{
			if ( is_numeric( $variable_name ) && is_array( $variable_content ) )
			{
				$rough_loop = $this->current_loop_block[2];

				if ( is_array( $variable_content ) )
				{
					foreach( $variable_content as $variable_name_2 => $variable_content_2 )
					{
						if ( !is_array( $variable_content_2 ) )
						{
							/**
								Associative Array Loops

								array(
									"title" => "Board Name"
								)
							**/

							$rough_loop = str_replace( $this->replacement_variable_prefix . $loop . "." . $variable_name_2 . $this->replacement_variable_suffix, $variable_content_2, $rough_loop );
						}
						else if ( is_array( $variable_content_2 ) )
						{					
							/**
								Nested Array Loops

								array(
									"title" => "Name",
									"nested_loop" => array(
										"title" => "Sub Name"
									)
								)
							**/			

							$this->current_nested_loop_block = $this->generate_block_ref_data( $variable_name_2, array( " BEGIN ", " END " ), "loop", $rough_loop );

							foreach ( $variable_content_2 as $variable_name_nested => $variable_content_nested )
							{									
								$rough_nested_loop = $this->current_nested_loop_block[2];									

								foreach ( $variable_content_nested as $variable_name_nested_2 => $variable_content_nested_2 )
								{
									$rough_nested_loop = str_replace( $this->replacement_variable_prefix . $variable_name_2 . "." . $variable_name_nested_2 . $this->replacement_variable_suffix, $variable_content_nested_2, $rough_nested_loop );
								}

								$parsed_nested_loop .= rtrim( $rough_nested_loop );									
							}

							$this->current_nested_loop_block[3] = $parsed_nested_loop;
							$rough_loop = str_replace( $this->replacement_loop_prefix . " BEGIN " . $variable_name_2 . $this->replacement_loop_suffix . substr( $rough_loop, $this->current_nested_loop_block[0], $this->current_nested_loop_block[1] ) . $this->replacement_loop_prefix . " END " . $variable_name_2 . $this->replacement_loop_suffix, $this->current_nested_loop_block[3], $rough_loop ); 	
							$this->current_nested_loop_block = array( );								
						}	
					}
				}					
			}

			$parsed_loop .= rtrim( $rough_loop );
		}

		$this->current_loop_block[3] = $parsed_loop;
		$this->current_template_html = str_replace( $this->replacement_loop_prefix . " BEGIN " . $loop . $this->replacement_loop_suffix . substr( $this->current_template_html, $this->current_loop_block[0], $this->current_loop_block[1] ) . $this->replacement_loop_prefix . " END " . $loop . $this->replacement_loop_suffix, $this->current_loop_block[3], $this->current_template_html ); 
		$this->current_loop_block = array( );
	}
} 
?>

 

And here is the generate_block_ref_data function:

<?php
function generate_block_ref_data( $name, $tag, $type, $content = "" )
{
	$tag_start = ( $type == "loop" ) ? $this->replacement_loop_prefix . $tag[0] . $name . $this->replacement_loop_suffix : $this->replacement_variable_prefix . $tag[0] . $name . $this->replacement_variable_suffix;
	$tag_end = ( $type == "loop" ) ? $this->replacement_loop_prefix . $tag[1] . $name . $this->replacement_loop_suffix : $this->replacement_variable_prefix . $tag[1] . $name . $this->replacement_variable_suffix;

	$tag_start_pos = ( $content != null ) ? strpos( $content, $tag_start ) + strlen( $tag_start ) : strpos( $this->current_template_html, $tag_start ) + strlen( $tag_start );
	$tag_end_pos = ( $content != null ) ? strpos( $content, $tag_end ) : strpos( $this->current_template_html, $tag_end );			
	$tag_length = $tag_end_pos - $tag_start_pos;	

	return array(
		0 => $tag_start_pos, 
		1 => $tag_length, 
		2 => ( $content != null ) ? substr( $content, $tag_start_pos, $tag_length ) : substr( $this->current_template_html, $tag_start_pos, $tag_length ),	
		3 => ""
	);	
}
?>

Link to comment
https://forums.phpfreaks.com/topic/74205-solved-nested-looping/#findComment-374814
Share on other sites

Alright I have made a few new changes to the function:

 

<?php
function loop_replace( $loop, $vars = array( ), $error_line, $error_file )
{
	if ( $this->has_error == true )
	{
		return;
	}
	else if ( $this->has_error == false )
	{    
		$this->current_loop_block = $this->generate_block_ref_data( $loop, array( " BEGIN ", " END " ), "loop" );

		foreach ( $vars as $variable_name => $variable_content )
		{
			if ( is_numeric( $variable_name ) && is_array( $variable_content ) )
			{
				$rough_loop = $this->current_loop_block[2];

				if ( is_array( $variable_content ) )
				{
					foreach( $variable_content as $variable_name_2 => $variable_content_2 )
					{
						if ( !is_array( $variable_content_2 ) )
						{
							/**
								Associative Array Loops

								array(
									"title" => "Board Name"
								)
							**/

							$rough_loop = str_replace( $this->replacement_variable_prefix . $loop . "." . $variable_name_2 . $this->replacement_variable_suffix, $variable_content_2, $rough_loop );
						}
						else if ( is_array( $variable_content_2 ) )
						{					
							/**
								Nested Array Loops

								array(
									"title" => "Name",
									"nested_loop" => array(
										"title" => "Sub Name"
									)
								)

							$this->current_nested_loop_block = $this->generate_block_ref_data( $variable_name_2, array( " BEGIN ", " END " ), "loop", $rough_loop );

							foreach ( $variable_content_2 as $variable_name_nested => $variable_content_nested )
							{									
								$rough_nested_loop = $this->current_nested_loop_block[2];									

								foreach ( $variable_content_nested as $variable_name_nested_2 => $variable_content_nested_2 )
								{
									$rough_nested_loop = str_replace( $this->replacement_variable_prefix . $variable_name_2 . "." . $variable_name_nested_2 . $this->replacement_variable_suffix, $variable_content_nested_2, $rough_nested_loop );
								}

								$parsed_nested_loop .= @rtrim( $rough_nested_loop );	
								$rough_nested_loop = "";																
							}

							$this->current_nested_loop_block[3] = $parsed_nested_loop;
							$rough_loop = str_replace( $this->replacement_loop_prefix . " BEGIN " . $variable_name_2 . $this->replacement_loop_suffix . substr( $rough_loop, $this->current_nested_loop_block[0], $this->current_nested_loop_block[1] ) . $this->replacement_loop_prefix . " END " . $variable_name_2 . $this->replacement_loop_suffix, $this->current_nested_loop_block[3], $rough_loop ); 	
							$this->current_nested_loop_block = array( );
							*/

							$this->current_nested_loop_block = $this->generate_block_ref_data( $variable_name_2, array( " BEGIN ", " END " ), "loop", $rough_loop );

							foreach ( $variable_content_2 as $variable_name_nested => $variable_content_nested )
							{
								if ( is_numeric( $variable_name_nested ) && is_array( $variable_content_nested ) )
								{
									$rough_nested_loop = $this->current_nested_loop_block[2];

									if ( is_array( $variable_content_nested ) )
									{
										foreach( $variable_content_nested as $variable_name_nested_2 => $variable_content_nested_2 )
										{
											$rough_nested_loop = str_replace( $this->replacement_variable_prefix . $variable_name_2 . "." . $variable_name_nested_2 . $this->replacement_variable_suffix, $variable_content_nested_2, $rough_nested_loop );
										}
									}

									$parsed_nested_loop .= @rtrim( $rough_nested_loop );
									$rough_nested_loop = "";
								}

								$this->current_nested_loop_block[3] = $parsed_nested_loop;
								$rough_loop = str_replace( $this->replacement_loop_prefix . " BEGIN " . $variable_name_2 . $this->replacement_loop_suffix . substr( $rough_loop, $this->current_nested_loop_block[0], $this->current_nested_loop_block[1] ) . $this->replacement_loop_prefix . " END " . $variable_name_2 . $this->replacement_loop_suffix, $this->current_nested_loop_block[3], $rough_loop ); 	
								$this->current_nested_loop_block = array( );
							}								
							// End Nested Looping....
						}	
					}
				}					
			}

			$parsed_loop .= rtrim( $rough_loop );
		}

		$this->current_loop_block[3] = $parsed_loop;
		$this->current_template_html = str_replace( $this->replacement_loop_prefix . " BEGIN " . $loop . $this->replacement_loop_suffix . substr( $this->current_template_html, $this->current_loop_block[0], $this->current_loop_block[1] ) . $this->replacement_loop_prefix . " END " . $loop . $this->replacement_loop_suffix, $this->current_loop_block[3], $this->current_template_html ); 
		$this->current_loop_block = array( );
	}
} 
?>

 

Now its outputting:

General
new.gif 	General Chat 	1 	0
Tutorials
new.gif 	General Chat 	1 	0
new.gif 	PHP Tutorials 	1 	0

Link to comment
https://forums.phpfreaks.com/topic/74205-solved-nested-looping/#findComment-374861
Share on other sites

Argh im getting so close to getting this damn thing working, there is where i stand right now. This is the current output:

General
new.gif 	General Chat 	1 	0 	Kurt

Tutorials
nonew.gif 	PHP Tutorials 	1 	0 	Kurt

Help
nonew.gif 	No Subboards Defined! 	0 	0 	N/A

Show Off
nonew.gif 	No Subboards Defined! 	0 	0 	N/A

 

Seems alright... only thing is that General should have a 2nd subboard under it called Introductions....

General
new.gif 	General Chat 	1 	0 	Kurt
nonew.gif 	Intros 	1 	0 	Kurt

Tutorials
nonew.gif 	PHP Tutorials 	1 	0 	Kurt

Help
nonew.gif 	No Subboards Defined! 	0 	0 	N/A

Show Off
nonew.gif 	No Subboards Defined! 	0 	0 	N/A

 

Thats what it should be displaying but its not...

I am creating a multi-dim array based off of database output and this is the array that is being created:

Array
(
[1] => Array
	(
		[title] => General
		[cat_row] => Array
			(
				[1] => Array
					(
						[icon] => new.gif
						[title] => General Chat
						[topic_count] => 1
						[reply_count] => 0
						[last_poster] => Kurt
					)

				[2] => Array
					(
						[icon] => nonew.gif
						[title] => Introductions
						[topic_count] => 1
						[reply_count] => 0
						[last_poster] => Kurt
					)

			)

	)

[2] => Array
	(
		[title] => Tutorials
		[cat_row] => Array
			(
				[1] => Array
					(
						[icon] => nonew.gif
						[title] => PHP Tutorials
						[topic_count] => 1
						[reply_count] => 0
						[last_poster] => Kurt
					)

			)

	)

[3] => Array
	(
		[title] => Help
		[cat_row] => Array
			(
				[2] => Array
					(
						[icon] => nonew.gif
						[title] => No Subboards Defined!
						[topic_count] => 0
						[reply_count] => 0
						[last_poster] => N/A
					)

			)

	)

[4] => Array
	(
		[title] => Show Off
		[cat_row] => Array
			(
				[2] => Array
					(
						[icon] => nonew.gif
						[title] => No Subboards Defined!
						[topic_count] => 0
						[reply_count] => 0
						[last_poster] => N/A
					)

			)

	)

)

 

And once again here is the looping function:

<?php
function loop_replace( $loop, $vars = array( ), $error_line, $error_file )
{
	if ( $this->has_error == true )
	{
		return;
	}
	else if ( $this->has_error == false )
	{    
		$this->current_loop_block = $this->generate_block_ref_data( $loop, array( " BEGIN ", " END " ), "loop" );

		foreach ( $vars as $variable_name => $variable_content )
		{
			if ( is_numeric( $variable_name ) && is_array( $variable_content ) )
			{
				$rough_loop = $this->current_loop_block[2];

				if ( is_array( $variable_content ) )
				{
					foreach( $variable_content as $variable_name_2 => $variable_content_2 )
					{
						if ( !is_array( $variable_content_2 ) )
						{
							/**
								Associative Array Loops

								array(
									"title" => "Board Name"
								)
							**/

							$rough_loop = str_replace( $this->replacement_variable_prefix . $loop . "." . $variable_name_2 . $this->replacement_variable_suffix, $variable_content_2, $rough_loop );
						}
						else if ( is_array( $variable_content_2 ) )
						{					
							/**
								Nested Array Loops

								array(
									"title" => "Name",
									"nested_loop" => array(
										"title" => "Sub Name"
									)
								)
							*/

							$this->current_nested_loop_block = $this->generate_block_ref_data( $variable_name_2, array( " BEGIN ", " END " ), "loop", $rough_loop );
							$rough_nested_loop = $this->current_nested_loop_block[2];

							foreach ( $variable_content_2 as $variable_name_nested => $variable_content_nested )
							{								
								if ( is_numeric( $variable_name_nested ) && is_array( $variable_content_nested ) )
								{
									if ( is_array( $variable_content_nested ) )
									{
										foreach( $variable_content_nested as $variable_name_nested_2 => $variable_content_nested_2 )
										{
											$rough_nested_loop = str_replace( $this->replacement_variable_prefix . $variable_name_2 . "." . $variable_name_nested_2 . $this->replacement_variable_suffix, $variable_content_nested_2, $rough_nested_loop );
										}
									}
								}

								$parsed_nested_loop = @rtrim( $rough_nested_loop );									
							}	

							$this->current_nested_loop_block[3] = $parsed_nested_loop;
							$rough_loop = str_replace( $this->replacement_loop_prefix . " BEGIN " . $variable_name_2 . $this->replacement_loop_suffix . substr( $rough_loop, $this->current_nested_loop_block[0], $this->current_nested_loop_block[1] ) . $this->replacement_loop_prefix . " END " . $variable_name_2 . $this->replacement_loop_suffix, $this->current_nested_loop_block[3], $rough_loop ); 	
							$this->current_nested_loop_block = array( );							
						}	
					}
				}					
			}

			$parsed_loop .= rtrim( $rough_loop );
		}

		$this->current_loop_block[3] = $parsed_loop;
		$this->current_template_html = str_replace( $this->replacement_loop_prefix . " BEGIN " . $loop . $this->replacement_loop_suffix . substr( $this->current_template_html, $this->current_loop_block[0], $this->current_loop_block[1] ) . $this->replacement_loop_prefix . " END " . $loop . $this->replacement_loop_suffix, $this->current_loop_block[3], $this->current_template_html ); 
		$this->current_loop_block = array( );
	}
}
?>

 

and the generate_block_ref_data function:

<?php
function generate_block_ref_data( $name, $tag, $type, $content = "" )
{
	$tag_start = ( $type == "loop" ) ? $this->replacement_loop_prefix . $tag[0] . $name . $this->replacement_loop_suffix : $this->replacement_variable_prefix . $tag[0] . $name . $this->replacement_variable_suffix;
	$tag_end = ( $type == "loop" ) ? $this->replacement_loop_prefix . $tag[1] . $name . $this->replacement_loop_suffix : $this->replacement_variable_prefix . $tag[1] . $name . $this->replacement_variable_suffix;

	$tag_start_pos = ( $content != null ) ? strpos( $content, $tag_start ) + strlen( $tag_start ) : strpos( $this->current_template_html, $tag_start ) + strlen( $tag_start );
	$tag_end_pos = ( $content != null ) ? strpos( $content, $tag_end ) : strpos( $this->current_template_html, $tag_end );			
	$tag_length = $tag_end_pos - $tag_start_pos;	

	return array(
		0 => $tag_start_pos, 
		1 => $tag_length, 
		2 => ( $content != null ) ? substr( $content, $tag_start_pos, $tag_length ) : substr( $this->current_template_html, $tag_start_pos, $tag_length ),	
		3 => ""
	);	
}
?>

 

It seems like it is not adding the correct rows together as it should be.

Link to comment
https://forums.phpfreaks.com/topic/74205-solved-nested-looping/#findComment-374893
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.