首页 文章

Codeigniter - 查询foreach内部

提问于
浏览
0

所以我使用最新的Codeigniter,我的问题是我有这种情况,我应该在控制器中的foreach内使用查询 . 我有一个视图,它在一个列表中显示所有 生产环境 订单,我希望所有用户在其他表中创建注释和注释 . 编码的一般情况,但我应该如何使用Codeigniter执行此操作,因为您应该在模型中保留SQL子句 .

在我只是将原始SQL分配到控制器内部之前,我想问一下什么是更温和和正确的方法来实现这一点 . 谢谢!

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>

2 回答

  • -1

    如果你有500个 生产环境 订单,你应该努力避免在结果集中使用其他查询来旋转结果集 . 那是501查询来做一个简单的报告

    相反,使用连接和解析结果,只需打印一次 Headers 信息或使用Underscore.php创建嵌套结果,如下所示:

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

  • 2

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

    在控制器中,

    $data['productionOrders'] = $this->report_model->getAllProductionOrders();
    
        foreach($data['productionOrders']['id'] as $id)
        {
             $data['comment'][$id] = $this->report_model->getCommantsByOrderId($id);
        }
    

相关问题