0

So i'm using latest Codeigniter and my problem is that i have this situation where i should use query inside foreach in controller. I have view that shows all production orders in one list, and i want all user made comments and notes underneath them from other table. Quite general situation in coding, but how i should do this with Codeigniter, cause you should keep your SQL clauses in Models.

Before i just fork raw SQL inside controller, i wanted to ask what is more gentle and proper way to achieve this. Thanks!

Controller

<?php
public function show()
{    
  $this->load->database();

  $this->load->model('report_model');
  $this->load->helper('url');

  $data['productionOrders'] = $this->report_model->getAllProductionOrders();
  $this->load->view('all_reports', $data);
}
?>

Model

<?php
function getAllProductionOrders()
{
  $this->db->select(*);
  $this->db->from('dbo.QualityControl_ProductionOrders');

  $query = $this->db->get();

  return $query->result();
}
?>

View

<!DOCTYPE html>
<html lang="en">

 <head>
   <meta charset="utf-8">
 </head>

 <body>

  <?php
    foreach ($productionOrdersas $row)
    {
  ?>

  <div> 
    ProdNo: <?php echo $row->Prodno; ?>
    Descriptions: 
    Hours:
    etc etc etc etc
  </div>

  [I want here comments and all user made notes]

  <?php
    }
  ?>
 </body>
</html>
4

2 回答 2

2

您应该努力避免使用类似这样的附加查询来翻转结果集。想象一下,如果您有 500 个生产订单。这是 501 个查询来制作一个简单的报告

相反,通过仅打印一次标题信息或使用 Underscore.php 生成嵌套结果来使用连接和解析结果,如下所示:

http://codebyjeff.com/blog/2012/08/no-more-machine-gunning-use-underscore-php

于 2013-05-28T06:31:09.260 回答
-1

不知道你在问什么,但试试这个 -

在控制器中,

    $data['productionOrders'] = $this->report_model->getAllProductionOrders();

    foreach($data['productionOrders']['id'] as $id)
    {
         $data['comment'][$id] = $this->report_model->getCommantsByOrderId($id);
    }
于 2013-05-28T06:25:10.597 回答