如何使用codeigniter在单个页面中维护两个表数据

时间:2017-08-02 05:05:10

标签: php codeigniter

我想一次为一个客户提供基于invoice_id的2个表格中的数据。            示

   invoice                                     invoice_description
 -------------                                ----------------------
invoice_no  name   address                   product  invoice_no  qantity
  789       adi    ghfgfgfgh                   a       789         1
  786       abc    sdsddasd                    b       789         2

我想这样输出: -

如果我点击789发票编号

                    invoice no-789               name-adi
                             product    quantity
                               a           1
                               b           2

但我的输出是这样的: -

             invoice no-789   name-adi
                  product     quantity
                   a             1
                   b             2
         ------------------------------
              invoice no-786   name-abc
                  product     quantity
                    a           1
                    b           2

如何解决它

控制器: -

   //$id is current invoice_no;
 public function invoice($id)
    {

    $this->load->database();  

    $this->load->model('useradmin');  
    $query = $this->useradmin->selectdealer($id);
  $ids = $this->useradmin->selectdealer1($id);
  $data['dealers'] =  $query;
                    $data['id'] = $ids;

        $this->load->view('envoice',$data);
}
 model:-
    public function selectdealer($id)  
 {  

 $this->db->where('invoice_no', $id);
 $query = $this->db->get('invoice_description');
 return $query->result();  

}   
public function selectdealer1($id)  
{  

  $query = $this->db->get('invoice');  
  $this->db->where('invoice_no', $id);
  return $query->result();  

}   
 view:-
   foreach($id as $ids)
   {
     echo $ids->invoice_no;
     echo $ids->name;
      foreach($dealers as $dealer)
      {
       echo $dealer->product;

       }
    }

它返回我桌子上的所有数据invoice_no和Name  第二个foreach循环正常工作,但第一个foreach循环返回所有  表中的数据

2 个答案:

答案 0 :(得分:1)

使用联接查询

$this->db->select('*');
$this->db->from('invoice');
$this->db->join('invoice_description', 'invoice_description.invoice_no= invoice.invoice_no');
$this->db->where('invoice_no', $id);
$query = $this->db->get();
 return $query->result();

对于第二个功能

$this->db->select('*');
$this->db->from('invoice_description');
$this->db->join('invoice', 'invoice_description.invoice_no= invoice.invoice_no');

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

答案 1 :(得分:0)

我对你的代码有一些建议。 如果您自动加载数据库,那么您不需要在控制器中加载它。 使用构造函数在控制器中加载公共模型。

控制器代码:

public function __construct() {
parent::__construct ();
    $this->load->model ('useradmin');
}

public function invoice($id)
{
    $this->load->database();  // if you autoloding the database then you don't need to load it here. if not then add this in __construct     
    $data = $this->useradmin->selectdealer($id);
    $this->load->view('envoice',['data'=>$data]);

}

型号代码:

你想要一个与id和name相关的数据,所以我添加了$ name变量。您必须自己将其传递给模型,或者如果不是,则删除该行代码。

public function selectdealer($id)  
{ 

    $this->db->select('id.product, id.quantity');
    $this->db->from('invoice as i');
    $this->db->join('invoice_description as id', 'id.invoice_no= i.invoice_no');
    $this->db->where('i.invoice_no', $id);             
    $this->db->where('i.name', $name); // I have assumed this variable here so you have to take care of it or remove it if you don't need it.
    $query = $this->db->get();
    return $query->result();

    //You should use $this->db->last_query(); to get last executed query to understand the query properly. 
    // I am alos suggestion to use codeigniter log. it would be very helpfull. 

}  

查看代码:

<?php
    foreach($data as $single_data)
    {
        // Your expected result i have printed it just for knowledge. remove it and do you further code.
        echo "<pre>";
        print_r($single_data);
        echo "<//pre>";
    } 
?>

希望它对你有所帮助。