还有其他解决方案可以在foreach表中循环执行我的功能吗?

时间:2019-04-04 08:19:10

标签: php html codeigniter codeigniter-3

我试图在表的foreach中循环我的变量函数。但是当我获取大量数据时,会导致生成缓慢

此代码是否有解决方案或想法?谢谢高级

服务器端(控制器)

$data['months'] = $this->db->get('nr_months')->result();
$data['data']   = $this->db->get('notes_receivable')->result();
$data['dataEachMos'] = function($d, $loan_ref){
   $result = $this->db->get_where('v_monthly_nr', array('due_date' => $d, 'ref_no' => $loan_ref))->row();
   return $result;
};
$this->load->view('tbl-view', $data);

客户端

-- in tbl-view page
<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Reference #</th>    
      <?php foreach($months as $row): ?>
      <th><?php echo date('F j, Y', strtotime($row->date)); ?></th>
      <?php endforeach;?>
    </tr>
  </thead>
  <tbody>
    <?php foreach($data as $row): ?>
      <tr>
        <td><?php echo strtoupper($row->name); ?></td>
        <td><?php echo $row->loan_ref; ?></td> 
        <?php foreach($months as $row): ?>
          <?php $nr_data = $dataEachMos($row->date, $row->loan_ref); ?>
          <th><?php echo number_format($nr_data->amount, 2); ?></th>
        <?php endforeach; ?>
      </tr>
    <?php endforeach;?>
  </tbody>
</table>

```


1 个答案:

答案 0 :(得分:1)

试试这个-

您必须更改第二个foreach循环。

-- in tbl-view page
<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Reference #</th>    
      <?php foreach($months as $row): ?>
      <th><?php echo date('F j, Y', strtotime($row->date)); ?></th>
      <?php endforeach;?>
    </tr>
  </thead>
  <tbody>
    <?php foreach($data as $roww): ?>
      <tr>
        <td><?php echo strtoupper($roww->name); ?></td>
        <td><?php echo $roww->loan_ref; ?></td> 
        <?php foreach($months as $row): ?>
          <?php $nr_data = $dataEachMos($row->date, $row->loan_ref); ?>
          <th><?php echo number_format($nr_data->amount, 2); ?></th>
        <?php endforeach; ?>
      </tr>
    <?php endforeach;?>
  </tbody>
</table>

```

在控制器中

$months = $this->db->get('nr_months')->result();
$data   = $this->db->get('notes_receivable')->result();
$dataEachMos = function($d, $loan_ref){
   $result = $this->db->get_where('v_monthly_nr', array('due_date' => $d, 'ref_no' => $loan_ref))->row();
   return $result;
};
$this->load->view('tbl-view', $data);