【问题标题】:Codeigniter : How to change result array [duplicate]Codeigniter:如何更改结果数组
【发布时间】:2019-03-21 06:18:05
【问题描述】:

如何像这样改变结果数组

喜欢这个

这是我的控制器

public function index(){

    $response = Requests::get('https://xsintern-api.learnbalance.com/api/users/');
    $responseData = $response->body; 
    $arrData = json_decode($responseData,true); 

    echo "<pre>";
    print_r($arrData);
    echo "</pre>";
    $this->load->view('welcome_message');

}

【问题讨论】:

  • 现在数据如何?对象?

标签: php arrays api codeigniter


【解决方案1】:

控制器

public function index(){

    $response = Requests::get('https://xsintern-api.learnbalance.com/api/users/');
    $responseData = $response->body; 
    $arrData = json_decode($responseData,true);
    $data = array(); 
    $data['arrData'] = $arrData;

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

}

查看代码
首先你需要设置标题然后这个

<?php foreach($arrData as $data){ ?>
 <tr>
    <td><?php echo $data['user_firstname'] ?></td>
 </tr>
<?php } ?>

【讨论】:

    【解决方案2】:

    控制器:首先将数据从控制器传递到您的视图:

    public function index(){
    
        $response = Requests::get('https://xsintern-api.learnbalance.com/api/users/');
        $responseData = $response->body; 
        $arrData = json_decode($responseData,true); 
    
        // Create an Data Array to store all the data
        // that you need to pass to your view
        $data['user'] = $arrData; 
    
        $this->load->view('welcome_message', $data);
    
    }
    

    视图:然后在您的视图文件中创建一个表格并使其动态使用需要显示的数据:

    <?php foreach($user as $key => $value) : ?>
    <tr>
        <td><?= $value['user_firstname'] ?></td>
        <td><?= $value['user_lastname'] ?></td>
        ...
        ..
        .
    </tr>
    <?php endforeach; ?>
    

    【讨论】:

      【解决方案3】:

      在 HTML 表格中这样做

      $data['arrData'] = json_decode($responseData,true);
      

      在视图中传递数据$this-&gt;load-&gt;view('welcome_message', $data);

      <?php foreach($arrData as $data): ?>
       <tr>
          <td><?= $data['user_firstname'] ?></td>
       </tr>
      <?php endforeach; ?>
      

      【讨论】:

      • 它有这样的错误消息“消息:未定义的变量:arrData 和消息:为 foreach() 提供的参数无效”
      • @wazowski 我已经更新了答案
      【解决方案4】:

      更改控制器以传递$arrData 数据:

      public function index(){
      
          $response = Requests::get('https://xsintern-api.learnbalance.com/api/users/');
          $responseData = $response->body; 
          $data['arrData'] = json_decode($responseData,true); 
      
          $this->load->view('welcome_message', $data);
      
      }
      

      并在welcome_message.php 视图文件中尝试这样的操作:

      <table class="table table-hover">
          <thead>
              <tr>
                  <th>The Title</th>
              </tr>
          </thead>
          <tbody>
              <?php foreach($arrData as $data): ?>
              <tr>
                  <td><?= $data['user_firstname'] ?></td>
              </tr>
              <?php endforeach; ?>
          </tbody>
      </table>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-30
        • 1970-01-01
        • 2015-04-26
        相关资源
        最近更新 更多