如何从另一个表获取数据并在 PHP Codeigniter 中的单个表上显示?

我需要帮助,我有 2 个表“客户”和“产品”。我想根据客户表从产品表中获取数据并将其整体显示。


productTable

productId     productName      productPrice

1             Abc              100

2             Bcd              200

3             Cde              300


clientTable

clientId        productNameId     clientName

4               2                 A

5               3                 B

6               1                 C

我希望表格将记录显示为:


Client Name             Product Name

A                       Bcd

B                       Cde

C                       Abc

我如何使用 MVC CI 展示它们。


模型


class Client_model extends CI_model

{

    function All()

    {

        return $client = $this->db->get('client')->result_array();

    }

}

控制器


class Client extends CI_Controller

    public function index()

    {

        $this->load->model('Client_model');

        

        $invoice = $this->Client_model->All();

        $data = array();

        $data['client'] = $client;

        $this->load->view('admin/ViewClient', $data);

    }

}


holdtom
浏览 93回答 1
1回答

繁星coding

客户端控制器代码:-class Client extends CI_Controller&nbsp; &nbsp; public function index()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->load->model('Client_model');&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $data['clientData']&nbsp; = $this->Client_model->All();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('admin/ViewClient', $data);&nbsp; &nbsp; }}Client_model 型号代码:-class Client_model extends CI_model{&nbsp; &nbsp; function All()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $client = $this->db->get('client')->result_array();&nbsp; &nbsp; }}ViewClient 查看页面:-<table><thead>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <th>S.NO</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>client Name</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>Product Name</th>&nbsp; &nbsp;</tr></thead><tbody><?php if(!empty($clientData)) {&nbsp;&nbsp; &nbsp; $count=1;foreach($clientData as $client){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;?><tr>&nbsp; &nbsp; <td><?php echo $count; ?></td>&nbsp; &nbsp; <td><?php echo $client['clientName']; ?></td>&nbsp; &nbsp; <td><?php&nbsp;&nbsp;$pid =&nbsp; $client['productNameId'];$pdata = $this->db->get_where('productTable',array('productId '=>$pid))->row();echo&nbsp; $pdata->productName; ?>&nbsp; &nbsp; </td>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp;<?php $count++;&nbsp; &nbsp; } }&nbsp;&nbsp; &nbsp; ?>&nbsp; &nbsp; </tbody>&nbsp; &nbsp; </table>
打开App,查看更多内容
随时随地看视频慕课网APP