【问题标题】:Is there any other solution to loop my function within the foreach table?是否有任何其他解决方案可以在 foreach 表中循环我的函数?
【发布时间】:2019-04-04 08:19:10
【问题描述】:

我试图在表中的 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>

```


【问题讨论】:

    标签: php html codeigniter codeigniter-3


    【解决方案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);
    

    【讨论】:

    • 对不起,我忘了更改对象.. 是的,它是一个不同但同样慢.. :(
    • @donpentavia:立即尝试
    猜你喜欢
    • 2015-10-25
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 2019-03-26
    • 2021-08-10
    • 2014-03-22
    • 2019-02-24
    • 1970-01-01
    相关资源
    最近更新 更多