douba8048 2017-08-01 10:44
浏览 50
已采纳

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

[![enter image description here][1]][1][enter image description here][2]I have two tables invoice and invoice_description, The column invoice_no is common for both tables, I want to fetch the data from invoice table name,address,invoice_no and next table invoice_description I want to fetch product description, quantity, price and more.
this is my table structure

                        invoice
         --------------------------------------
          name   bill_no   invoice_no   address    price
           adi     1        2           adasdasd     12
           abc     2        3           adasdas       13


                       invoice_description
        -----------------------------------------------------
               product   invoice_no  quantity 
                a          2          1
                b          2          2
                c          3          3

     i wana output like when i pass invoice_no 2
               invoice_no:-2     name- adi
                product          quantity
                 a                 1
                 b                 2

This is my controller:-

public function invoice($id)
{
            //echo $id;
        $this->load->database();  
             //load the model  
        $this->load->model('useradmin');  
            //$invoice=$this->useradmin->selectdealer($id);  
            //$invoicedes=$this->useradmin->selectdealer1($id);  
            //$data['invoice']=$invoice;
            //$data['invoicedes']=$invoicedes;
            //$this->load->view('envoice',$data);  
          //
        $query = $this->useradmin->selectdealer($id);
        $ids = $this->useradmin->selectdealer1($id);  
        $data['dealers'] =  $query;
        $data['id'] = $ids;                     
        $this->load->view('envoice',$data);
    }

}

My model is:-

public function selectdealer($id)  
{  
    //data is retrive from this query  
    //echo $id;           
    //$this->db->select('*');  
    //$this->db-from('invoice_description');
    $this->db->where('invoice_no', $id);
    $query = $this->db->get('invoice_description');
    return $query->result();
}   
public function selectdealer1($id)  
{  
    //data is retrive from this query  
    //echo $id;
    $query = $this->db->get('invoice');  
    $this->db->where('invoice_no', $id);
    return $query->result();    
}   

I want invoice no, name, and address from invoice table and more than one product description from invoice_description table

I m trying my view page is

invoice_no :-123 name:-xyzx

product des price quantity asdaf 12 10 asafsa 12 13

  • 写回答

5条回答 默认 最新

  • dopnpoh056622 2017-08-01 11:15
    关注

    In controller

    public function invoice($id)
    {
        $this->load->database();  
        $this->load->model('useradmin');  
        $data['dealer'] = $this->useradmin->selectdealer($id);
        $this->load->view('envoice',$data);
    }
    

    In model

    public function selectdealer($id)  
    {  
        $this->db->select("name, address, invoice_no");
        $this->db->from('invoice');
        $this->db->where('invoice_no', $id);
        $result = $this->db->get()->row_array();
        return $result;
    }   
    

    In view

    foreach($dealer as $row){
        echo $row['name'];
        echo $row['address'];
    
            $query = $this->db->get_where('invoice_description', array('invoice_no' => $row['invoice_no']));
    $results = $query->result_array();
    print_r($results);
        foreach($results as $desc){
            echo $desc['quantity'];
            echo $desc['price'];
            echo $desc['desc'];
        } 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改