View无法调用Controller Function Zend Framework 2

时间:2016-01-28 06:52:31

标签: php zend-framework

我无法在视图页面中调用我的控制器。即使我在控制器中使用print_r但它没有显示。我有body_product.phtml查看代码:

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

并在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;
    }

如果我在视图中打开print_r,则会显示错误Warning: Invalid argument supplied for foreach() in...。我认为它的原因不能调用控制器。 请帮帮我,谢谢。

1 个答案:

答案 0 :(得分:0)

首先,当您尝试使用print_r时,它需要一个数组。所以它应该像print_r($arr)。还试一试,看看它是否有帮助。

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