【问题标题】:Can't read data that send from controller to view by foreach无法读取从控制器发送到 foreach 视图的数据
【发布时间】:2020-02-18 22:09:34
【问题描述】:

发送到视图的 $results 是未定义的

public function action(){
        // if($this->input->post('data_action'))
        // {
        //  $data_action = $this->input->post('data_action');
        //  if($data_action=="fetch_all")
        //  {
                $api_url = "https://jsonplaceholder.typicode.com/users";

                $process = curl_init($api_url); //your API url
                curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
                $return = curl_exec($process);
                curl_close($process);
                $results=json_decode($return);
                //finally print your API response

                $this->load->view('masterPartner',$results);
                //print_r($result);
            //  }
            // }

这就是风景

<?php foreach($results as $result)
                      { ?>
                        <tr>
                        <th scope="row"><?php echo $result->id ?></th>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td><a class="btn btn-success " style="margin-right:5px"href="/Crudview/index.php/edit-partner" role="button">Edit</a><a class="btn btn-danger" href="/Crudview/index.php/viewApi" role="button">Delete</a>
                        </tr>
                        <?php } ?>

第一个错误

遇到 PHP 错误 严重性:通知

消息:未定义变量:结果

文件名:views/masterPartner.php

行号:115

第二个错误

遇到 PHP 错误 严重性:警告

消息:为 foreach() 提供的参数无效

文件名:views/masterPartner.php

行号:115

【问题讨论】:

    标签: php codeigniter foreach


    【解决方案1】:

    这是因为视图中不存在 $results。它期望通过数组传入 $results。

    要传递它,您需要更改当前代码

    来自

    $results=json_decode($return);
    //finally print your API response
    $this->load->view('masterPartner',$results);
    

    $data['results']=json_decode($return);
    //finally print your API response
    $this->load->view('masterPartner',$data);
    

    所以现在您可以在视图中访问 $results。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      • 2018-08-06
      相关资源
      最近更新 更多