Jump to content

Concatenation in Variable Names in .tpl files


Keval Bhogayata

Recommended Posts

I am passing some of the variables to the .tpl file from php file using concatenation as given below:

while($row=mysqli_fetch_assoc($run_field))
	{		
		foreach($row as $rows)
		{
			$response_field = array(
			'extraVariable1'.$i => $row['id'],
			'extraVariable2'.$i => $row['name'],
			);
		}
		$finalarray_field=$finalarray_field+$response_field;
		$i++;
	} 

    return array(
        'tabOverviewReplacementTemplate' => $templateFile,
	    'templateVariables' => $finalarray_field,
	);

Now, I want to show this values in .tpl file. Is there any way so that I can print all the variables using loops (so that I should not have to write repeatedly as  {extraVariable11}, {extraVariable12},....  and so on).

Link to comment
Share on other sites

this code is illogical, invalid in few ways, try to apply the following:

$finalarray_field = array();
while($row = mysqli_fetch_assoc($run_field))
	{		
		
		$finalarray_field[] = array(
			'extraVariable1' => $row['id'],
			'extraVariable2' => $row['name'],
			);
	} 

    return array(
        'tabOverviewReplacementTemplate' => $templateFile,
	    'templateVariables' => array("finalarray" => $finalarray_field),
	);

then in your template file you display it using the following code:

{foreach item=row from=$finalarray}
<div>Variable #1: {$row.extraVariable1}</div>
<div>Variable #2: {$row.extraVariable2}</div>
<div>----------------------------------</div>
{/foreach}

 

Link to comment
Share on other sites

13 hours ago, sentq said:

this code is illogical, invalid in few ways, try to apply the following:


$finalarray_field = array();
while($row = mysqli_fetch_assoc($run_field))
	{		
		
		$finalarray_field[] = array(
			'extraVariable1' => $row['id'],
			'extraVariable2' => $row['name'],
			);
	} 

    return array(
        'tabOverviewReplacementTemplate' => $templateFile,
	    'templateVariables' => array("finalarray" => $finalarray_field),
	);

then in your template file you display it using the following code:


{foreach item=row from=$finalarray}
<div>Variable #1: {$row.extraVariable1}</div>
<div>Variable #2: {$row.extraVariable2}</div>
<div>----------------------------------</div>
{/foreach}

 

I came across some of the  mistakes I have made in syntax. Thank you. 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated