duaiwo9093 2016-01-27 22:52
浏览 66
已采纳

View无法调用Controller Function Zend Framework 2

I can't call my controller in view page. even if i use print_r in controller but it didn't show. I have body_product.phtml view code:

<table class="table table-hover table-condensed">
                <tbody>
                    <?php
                        $i = 1;
                        //print_r("list produk:".$this->productList);
                         foreach ($this->productList as $data) {
                            $desc=explode(",",$data['descriptions']);
                            ?>
                            <tr>
                                <th colspan="3">
                                    <input type="hidden" id="id_pack" name="id_pack" value="<?php echo $data['package_id']; ?>">
                                    <input type="hidden" id="nama_pack" name="nama_pack" value="<?php echo $data['package_name']; ?>">
                                    <h4 align="center" class="title-pack"><?php echo $data['package_name']; ?></h4>
                                </th>
                            </tr>
                            <tr id="dashe">
                                <td>
                                    <ul class="myul">
                                    <?php foreach($desc as $descriptions) { ?>
                                        <li class="myli"> <?php echo $descriptions; ?></li>
                                    <?php } ?>
                                    </ul>
                                </td>
                                <td>
                                    <h4 class="prize">
                                        <?php setlocale(LC_MONETARY, 'id_ID');
                                        echo money_format('%.2n', $data['package_price']); ?>
                                        / month
                                    </h4>
                                </td>
                                <td>
                                    <p id="btn-hrm" class="mybutton" data-toggle="modal" data-target=".mymodal">Order</p>
                                </td>
                            </tr>
                            <?php
                            $i++;
                        }
                    ?>
                </tbody>
            </table>

and in the indexController:

 public function loadProductAction() {
        $viewModel = new ViewModel();
        $storage = Product\Storage::factory($this->getDb());
        $productList = new Product($storage);
        $data = $productList->loadProduct();
        $arr = array();
        if ($data) {
            foreach ($data as $val) {
                array_push($arr, $val);
            }
        }
        print_r('teaaat'.$arr);
        $viewModel->setVariables(array('productList' => $arr))
                ->setTerminal(true);
        return $viewModel;
    }

If I open print_r in the view,it show error Warning: Invalid argument supplied for foreach() in.... I think it cause of view can't call the controller. Help me please,thanks.

展开全部

  • 写回答

1条回答 默认 最新

  • douhu2898 2016-01-28 05:03
    关注

    Firstly when you try to use print_r it is expecting an array. So it should be something like print_r($arr). Also give this a try and see if it helps.

    public function loadProductAction() {
            $storage = Product\Storage::factory($this->getDb());
            $productList = new Product($storage);
            $data = $productList->loadProduct();
            $arr = array();
            if (is_array($data) && !empty($data)) {
                foreach ($data as $val) {
                    array_push($arr, $val);
                }
            } else {
                echo '$data is not an array or it is empty';
            }
            print_r($arr);
            return new ViewModel(array(
                'productList' => $arr
            ));
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 KeiI中头文件找不到怎么解决
  • ¥15 QT6将音频采样数据转PCM
  • ¥15 本地安装org.Hs.eg.dby一直这样的图片报错如何解决?
  • ¥15 下面三个文件分别是OFDM波形的数据,我的思路公式和我写的成像算法代码,有没有人能帮我改一改,如何解决?
  • ¥15 Ubuntu打开gazebo模型调不出来,如何解决?
  • ¥100 有chang请一位会arm和dsp的朋友解读一个工程
  • ¥50 求代做一个阿里云百炼的小实验
  • ¥15 查询优化:A表100000行,B表2000 行,内存页大小只有20页,运行时3页,设计两个表等值连接的最简单的算法
  • ¥15 led数码显示控制(标签-流程图)
  • ¥20 为什么在复位后出现错误帧
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部