使用$ smarty从mysql数据库显示数据时出错

时间:2015-02-23 06:36:49

标签: php mysql smarty

有人可以指导我这部分有什么不对,有时它从数据库中获取所有数据,有时只有第一行,有时甚至没有。任何帮助或替代方法都将受到高度赞赏。谢谢提前

我模块/ 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);
	}
 
 

这是我的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>

这是我的表结构

表1

--------------------------------------
id   |  image   |  value1 |  value2
--------------------------------------
1    |  x.jpg   |  abc    |   xyz
--------------------------------------
2    |  x.jpg   |  abc    |   xyz
--------------------------------------

1 个答案:

答案 0 :(得分:0)

通过聪明的官方页面解决了这个问题: 修改如下

php文件:

&#13;
&#13;
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)));
		}
&#13;
&#13;
&#13;

到这个

&#13;
&#13;
	global $smarty;	
	
		$sql = 'SELECT * FROM `'._DB_PREFIX_.'blocktesti` ';

		$sql1= Db::getInstance()->executeS($sql);
		$smarty->assign('contacts', $sql1);
&#13;
&#13;
&#13;

来自此

的tmp文件

&#13;
&#13;
{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}
&#13;
&#13;
&#13;

到这个

&#13;
&#13;
{section name=co loop=$contacts}
    <li>
		<p>{$contacts[co].id}</p>
			<div class="cd-author">
				<img src='{$contacts[co].image}' alt="Author image">
				<ul class="cd-author-info">
					<li>{$contacts[co].value1}</li>
					<li>{$contacts[co].value2}</li>
				</ul>
			</div>
		</li>
{sectionelse}
  <h3>No Data found</h3>
{/section}
&#13;
&#13;
&#13;