【问题标题】:Array showing only one row from json file数组仅显示 json 文件中的一行
【发布时间】:2017-10-17 05:59:14
【问题描述】:

这是我的控制器

public function showCategoryJson(){
    $this->load->model('user/category_model');
    $data[] = $this->category_model->displayCategory();

    echo json_encode($data);
}

我的模型

function displayCategory(){
  $query = $this->db->get('categories');
  $res = $query->result_array();
  return $res;
}

和我的观点

<?php
  $url = site_url('user/category/showCategoryJson'); // path to your JSON file
  $data = file_get_contents($url); // put the contents of the file into a variable
  // echo $data;
  $results = json_decode($data); // decode the JSON feed

  foreach ($results as $key => $cat) { 
?>
<tr class="odd gradeX">
 <td><?php echo $cat[$key]->categoryname; ?></td>
 <td><?php echo $cat[$key]->username; ?></td>
 <td class="center"><a class="btn btn-success fa fa-edit"></a> <a class="btn btn-danger fa fa-trash delete" id="<?php echo $cat[$key]->categoryid; ?>"></a></td>
</tr>
<?php } ?>

我已经尝试了所有方法来获取该数组的所有值(来自 JSON)。但我只得到一排。这是屏幕截图-

这是json结果

【问题讨论】:

    标签: json codeigniter-3


    【解决方案1】:

    您可以开始“调试”的方法之一是实际打印出 $results 的内容。 在 foreach 循环之前尝试以下操作:

    print_r($results);
    

    结果将显示类似以下内容:

    Array ( [0] => Array ( [0] => stdClass Object ( [categoryid] => 10 [categoryname] => sdf [username] => icicle ) [1] => stdClass Object ( [categoryid] => 12 [categoryname] => ase [username] => icicle ) [2] => stdClass Object ([categoryid] => 23 [categoryname] => aser [username] => icicle ) ) ) sdf icicle

    这意味着json内容已经被解码成嵌套数组。

    您可以尝试为 json_decode 函数添加“true”参数,并将变量作为数组访问。

    $results = json_decode($data,true); // 解码 JSON 提要

    完整代码:

        <?php
            $url = site_url('user/category/showCategoryJson'); // path to your JSON file
            $data = file_get_contents($url); // put the contents of the file into a variable
            $results = json_decode($data,true); // decode the JSON feed
            foreach ($results[0] as $key => $cat) { 
        ?>
        <tr class="odd gradeX">
        <td><?php echo $cat['categoryname']; ?></td>
        <td><?php echo $cat['username']; ?></td>   
        <td class="center"><a class="btn btn-success fa fa-edit"></a> <a class="btn btn-danger fa fa-trash delete" id="<?php echo $cat['categoryid']; ?>"></a></td>
        </tr>
        <?php } ?>
    

    编辑:要将数据从控制器传递到视图,您可以将模型的结果分配给一个变量,然后指示控制器加载视图以及您存储数据的变量。

    例如,在您的控制器中,您可以更改以下内容:

    public function showCategoryJson(){
        $this->load->model('user/category_model');
        $data['category_data'] = $this->category_model->displayCategory();
        $url = site_url('user/category/showCategoryJson'); // path to your JSON file
        $json_data = file_get_contents($url); // put the contents of the file into a variable
        $data['results'] = json_decode($json_data,true); // decode the JSON feed
        $this->load->view('[YOUR_VIEW_NAME]',$data);
    }
    

    然后您可以在视图中使用 $category_data 和 $results。

    【讨论】:

    • 一个问题(只是想学习,如果你不介意的话)如果我在控制器中使用 $data['res'] = $this->category_model->displayCategory();那么我的观点需要改变什么?
    • 我已经编辑了答案,以展示如何将数据从控制器传递到视图。希望有帮助!
    • 谢谢兄弟...我会努力的:)
    【解决方案2】:

    你可以用这个:

    json_decode($data,true);
    

    TRUE时,返回的对象将被转换为关联数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-16
      • 1970-01-01
      • 1970-01-01
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多