dpkk8687 2017-08-02 05:05
浏览 25

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

I wanna fetch and show data from 2 tables on basis of invoice_id for one customer at one time. shown

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

I wanna output like this:-

If I click on 789 Invoice no.

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

but my output is like this:-

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

how to resolve it

controller:-

   //$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;

       }
    }

its returns all the data invoice_no and Name which present on my table second foreach loop works correctly but first foreach loop returns all the data which present in table

  • 写回答

2条回答 默认 最新

  • dtdb99743 2017-08-02 05:18
    关注

    Use join query

    $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();
    

    For the second function

    $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();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错