dongwei1954 2016-06-04 13:15
浏览 63
已采纳

Smarty从控制器传递数据以查看和显示数据

I'm getting data from database with this function which is working fine.

static function dohvatiSveOlijeku($id){
    $baza = new Baza();

    $upit = "SELECT * FROM lijek WHERE status = 1 and lijek.id = " . $id;

    $rez = $baza->selectDB($upit);
    if($rez->num_rows == 0){
        return null;
    }else{
        $red = $rez->fetch_assoc();
        return $red;
    }
}

and this is my controller which is using parameter id to get data from model...

if (isset($_GET['id'])) {
$LijekSve = Lijek::dohvatiSveOlijeku($_GET['id']);
}


$Smarty->assign('lijek', $LijekSve);
$Smarty->display('view/lijek.tpl');

and this is a view which is presenting given data

{foreach from=$lijek item =lijekpodaci}
    <div>
        <p>{$lijekpodaci.cijena} kuna</p>
    </div>
{/foreach}

For each item in table I get this error

 Illegal string offset 'cijena' in C:\xampp\htdocs\ljekarna\smarty\libs\sysplugins\smarty_internal_templatebase.php(171) : eval()'d code on line 69 1 kuna

I also tried this, but the same error shows

{$lijekpodaci['cijena']}
  • 写回答

1条回答 默认 最新

  • drq22639 2016-06-04 13:43
    关注

    Why not try using a conditional statement to check if the $lijek Array is empty before you loop? Here's how:

        <?php
            // INITIALIZE $LijekSve TO AN EMPTY ARRAY JUST IN CASE $_GET['id'] ISN'T SET
            $LijekSve       = array();      
            if (isset($_GET['id'])) {
                $LijekSve   = Lijek::dohvatiSveOlijeku($_GET['id']);
            }
    
            $Smarty->assign('lijek', $LijekSve);
            $Smarty->display('view/lijek.tpl');
        ?>
    

    TEMPLATE

            {if !empty($lijek) }
                {foreach from=$lijek item =lijekpodaci}
                    <div>
                        <p>{$lijekpodaci.cijena} kuna</p>
                    </div>
                {/foreach}
            {/if}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?