Jump to content

$smarty retrieving data from database and passing it to .tmp file


PraveenShahi

Recommended Posts

Hello everyone, can someone please guide me whats wrong in this part sometimes its fetching all data from database, sometime only first row and sometimes nothing..any help or alternate method will be highly appreciated.Thanks in advance

 

here is my-module/mymodule.php:

public function hookHome($params)
	{

		$this->context->controller->addCSS($this->_path.'css/style.css', 'all');
	
		$this->context->controller->addJS($this->_path.'js/main.js', 'all');
		
		if (!$this->isCached('my-module.tpl', $this->getCacheId()))
		{
			$infos = $this->getInfos($this->context->language->id, $this->context->shop->id);
			$this->context->smarty->assign(array('infos' => $infos, 'nbblocks' => count($infos)));
		}

		return $this->display(__FILE__, 'my-module.tpl', $this->getCacheId());
	}

	public function getInfos($id_lang, $id_shop)
	{
		$sql = 'SELECT * FROM `'._DB_PREFIX_.'table1` ';

		return Db::getInstance()->executeS($sql);
	}


and here is my my-module/mymodule.tmp:

<div class="my-class">
	<ul class="my-class-child">
	
{if $infos|@count > 0}	

    {foreach from=$infos item=info}

        <li>
		<p>{$info.id}</p>
			<div class="cd-author">
				<img src='{$info.image}' alt="my-img">
				<ul class="csd">
					<li>{$info.value1}</li>
					<li>{$info.value2}</li>
				</ul>
			</div>
	</li>
     {/foreach}
{/if}
   </ul>

</div>

here is my table structure

 

table 1:

-----------------------------------------------

id      |  image   |   value1    |  value2

-----------------------------------------------

1      |  x.jpg      |  abc         |   xyz

----------------------------------------------

2      |  x.jpg      |  abc         |   xyz

----------------------------------------------

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

found this one working, thanks everyone

 

 

php code:



global $smarty; 
$sql = 'SELECT * FROM `'._DB_PREFIX_.'table` ';
$sql1= Db::getInstance()->executeS($sql);
$smarty->assign('contacts', $sql1);

tmp file

{section name=co loop=$contacts}
    <li>
		<p>{$contacts[co].value1}</p>
			<div class="cd-author">
				<img src='{$contacts[co].value2}' alt="Author image">
				<ul class="cd-author-info">
					<li>{$contacts[co].name}</li>
					<li>{$contacts[co].rank}</li>
				</ul>
			</div>
		</li>
{sectionelse}
  <h3>No Data found</h3>
{/section}
Edited by PraveenShahi (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...