duaiwo9093 2016-01-28 06: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 13: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
            ));
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度