Jump to content

Problem displaying arrays on .tpl


Recommended Posts

HI evrybody!!. Using PS 1.6.0.5. I´m having some trouble passing a query result from my hand-made module, using th .tpl... I´ll put the example and you´ll understand better.

This is the PHP on the "mymodule.php"

public function hookProductFooter($params)
	{

		global $smarty;
		
		$libroID = Tools::getvalue('id_product');		
		
		switch ($libroID) {
			case 476:		
			$libroarray = array(307,303,302,306);
			break;
			
			// MY INTENTION IS TO KEEP ADDING CASES
			}
			
			
			foreach ($libroarray as $librorow) 
			{
				$sql = 'SELECT * FROM '._DB_PREFIX_.'image WHERE `id_product` = '.$librorow;
				$results = Db::getInstance()->ExecuteS($sql)	;
				
					foreach ($results as $row) 
						{
						$var = $row['id_image'].' :: '.$row['id_product'].'<br />';						
						}
			}	
				
			$this->smarty->assign('libros', $var);
			return $this->display(__FILE__, 'mymodule.tpl');
		
		}

And this is the code on "mymodule.tpl"

{foreach $libros as $item}
  {$item}
{/foreach}

This displays on the products page at the bottom:

 

322 :: 306

 


This are the last result of the array. where '306' is the last number of the first array($libroarray),
and '322' is the result of the query that should run for eah of the numbes of that first array ($row['id_image'])...

I need to display all of the combination of that first array... here is the result I´m looking for:

 

389 :: 307

255 :: 303

560 :: 302

322 :: 306

 

Do I explain myself?  I know it has to be something really simple but I cant figure it out...

Please a little help?

Edited by TinoCas (see edit history)
Link to comment
Share on other sites

the problem is in this foreach loop:
 

foreach ($results as $row){
  $var = $row['id_image'].' :: '.$row['id_product'].'<br />';					
}

note that each loop of this foreach function changes $var variable value.

so you see last one only.

 

instead of $var =  try to use $var['element_nb']
 

where element_nb is an iteration value.

 

then you will be able to pass array, not single variable.

  • Like 1
Link to comment
Share on other sites

Hi Vekia... thanks for your help, appreciate it... Unfortuantely, that doesnt do it. I have this now:
 

foreach ($libroarray as $librorow) 
			{
				$sql = 'SELECT * FROM '._DB_PREFIX_.'image WHERE `id_product` = '.$librorow;
				$results = Db::getInstance()->ExecuteS($sql)	;
				
					foreach ($results as $row) 
						{
						$var['element_nb'] = $row['id_image'].' :: '.$row['id_product'].'<br />';						
						}
			}	
				
			$this->smarty->assign('libros', $var);
			return $this->display(__FILE__, 'modulomarban.tpl');

and this is the .tpl

{foreach $libros as $item}
	{$item}
{/foreach}

And still bringing the last result only.

I´m trying to build my own module where I can insert (using SWITCH/CASE) some Reletad products to each product. I´m doing it this way because, those realated products can be on different category than the parent and I want them to be display on a particular order... I was thinking of using an XML to load them, but still working on it, so the switch/case must do it...

I dont really know how to do what you say. I understand but I can´t figure out how it is done. Arrays arent my thing, actually I simply never knew how to work with them, allways drive me mental.

what do I have to do in this line after I change "$var= "  to  "$var['element_nb']" , what in my code is the 'element_nb'?

And I know this line would change also: "$this->smarty->assign('libros', $var);"  but , for what?

I am really sorry... but I know the key is on those 2 lines, but I cant get it to work... I tried all combinations I could think of...

Thanx for your help buddy!!

 

EDITED

 

Guys, I finally worked it out... on the code above, just change "$var['element_nb']= " for just "$var[] = "... as simple as that!! Thanks Vekia for you help!!

I mark this as "Solved"

Edited by TinoCas (see edit history)
Link to comment
Share on other sites

×
×
  • Create New...