【问题标题】:Error displaying data from mysql database using $smarty使用 $smarty 显示 mysql 数据库中的数据时出错
【发布时间】:2015-02-23 06:36:49
【问题描述】:

有人可以指导我这部分有什么问题,有时它会从数据库中获取所有数据,有时只是第一行,有时什么都没有。任何帮助或替代方法将不胜感激。提前致谢

我的模块/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
--------------------------------------

【问题讨论】:

    标签: php mysql smarty


    【解决方案1】:

    感谢 smarty 官方页面解决了这个问题: 修改如下

    php 文件:

    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)));
    		}

    到这里

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

    来自这个的 tmp 文件

    {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}

    到这里

    {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}

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多