从 json 解码计数总是显示不同数量的总和

我是使用 API 的新手。我在计算数据并将其显示到 html 视图时遇到问题。我正在使用 count($data) 在 html 视图中对其进行计数,但如果将其与数据进行比较,结果会有所不同。这是我从 API 获得的数据。我这里只显示 9 个样本数据。这是我从 API 获得的数据。


   {"rows":[{"ID":26,"Name":"Design Request (Draft)"}, 

   {"ID":25,"Name":"Kirim Produk Iklan"},

   {"ID":27,"Name":"Kirim Produk Iklan Reguler"},

   {"ID":18,"Name":"KMP - Advertorial"},

   {"ID":34,"Name":"KMP - Advertorial Daerah"},

   {"ID":5,"Name":"KMP - Artikel"},

   {"ID":9,"Name":"KMP - Artikel Editor"},

   {"ID":28,"Name":"KMP - Artikel PB"},

   {"ID":29,"Name":"KMP - Desain Grafis"},

 "status":0,"message":"","messagedescription":"","rowcount":9,"columns":["ID","Name"]}

这是我的控制器


    function index(){


    $output = $this->http_request('http://api.com');

    $profile = json_decode($output, TRUE);

    $config['total_rows'] = count($profile);

    var_dump($config);

    $data['data']=$profile;

    $data['sidebar']='sidebar';

    $data['content']='apinew';

    $this->load->view('main',$data);


}


function http_request($url){



    // persiapkan curl

    $ch = curl_init(); 


    // set url 

    curl_setopt($ch, CURLOPT_URL, $url);


    // set user agent    

    curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');


    // return the transfer as a string 

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 


    // $output contains the output string 

    $output = curl_exec($ch); 


    // tutup curl 

    curl_close($ch);      


    // mengembalikan hasil curl

    return $output;

}

这是我的观点


      <table class="table table-bordered">

        <thead>

         <tr>

          <th class="text-center" scope="col"> ID </th>

          <th class="text-center" scope="col">Job</th>

          <th class="text-center" scope="col">Action</th><!-- 

          <th class="text-center">Actions</th> -->

         </tr>

       </thead>

        <tbody>

           <?php for($i =0 ; $i < count($data); $i++){ ?>

           <tr>


谢谢你 :)


慕斯王
浏览 151回答 1
1回答

LEATH

问题是,你计算的data变量,它包含键rows,status,message等。您实际寻找的是 的计数$data['rows'],因此您可以通过将其切换为计算正确的字段来修复您的代码。$i =0 ; $i < count($data); $i++)// change to$i =0 ; $i < count($data['rows']); $i++)但是,您应该使用 aforeach而不是 for 循环来迭代数组,如下所示。这使您不必每次迭代都确定索引,并让您可以直接访问每一行。<?php foreach ($data['rows'] as $row) { ?>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td class="align-middle text-center"><?php echo $row['ID']; ?></td>&nbsp; &nbsp; &nbsp; &nbsp; <td class="align-middle text-center"><?php echo $row['Name'] ?></td>&nbsp; &nbsp; &nbsp; &nbsp; <td class="text-center align-middle">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="btn-group align-top">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a onclick="window.location.href='<?php echo base_url('apinew/edit/' . $row['Name']); ?>'"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;class="btn btn-sm btn-primary badge">Edit</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; </tr><?php } ?>
打开App,查看更多内容
随时随地看视频慕课网APP