【问题标题】:Count from json decode always show different number of sum从 json 解码计数总是显示不同的总和数
【发布时间】:2019-12-18 16:39:42
【问题描述】:

我是 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>
            <td class="align-middle text-center"><?php echo $data['rows'][($i)]['ID'];?></td>
            <td class="align-middle text-center"><?php echo $data['rows'][($i)]['Name'] ?></td>
            <td class="text-center align-middle">
            <div class="btn-group align-top">
           <a onclick="window.location.href='<?php echo base_url('apinew/edit/'.$data['rows'][$i]['Name']);?>'" class="btn btn-sm btn-primary badge">Edit</a>
            </div></td>
           </tr>
           <?php } ?>
          </tbody>
  </table>

谢谢你:)

【问题讨论】:

    标签: php html json codeigniter


    【解决方案1】:

    问题是您正在计算 data 变量,其中包含键 rowsstatusmessage 等。

    您实际上要查找的是$data['rows'] 的计数,因此您可以通过将代码切换为正确的字段来修复您的代码。

    $i =0 ; $i < count($data); $i++)
    // change to
    $i =0 ; $i < count($data['rows']); $i++)
    

    但是,您应该使用 foreach 而不是 for 循环来遍历数组,如下所示。这使您不必每次迭代都确定索引,并让您可以直接访问每一行。

    <?php foreach ($data['rows'] as $row) { ?>
        <tr>
            <td class="align-middle text-center"><?php echo $row['ID']; ?></td>
            <td class="align-middle text-center"><?php echo $row['Name'] ?></td>
            <td class="text-center align-middle">
                <div class="btn-group align-top">
                    <a onclick="window.location.href='<?php echo base_url('apinew/edit/' . $row['Name']); ?>'"
                       class="btn btn-sm btn-primary badge">Edit</a>
                </div>
            </td>
        </tr>
    <?php } ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多