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 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题