doushang3352 2016-10-10 20:51
浏览 51
已采纳

根据发送到codeigniter中的视图的数据在视图中执行模型函数

I'm having a controller that fetches an array from a model and sends it to the view. Sample table

user id    user name    user email
1          Mike         mike@yahoo.com
2          Tom          Tom@live.com

Now I've added another function to the model to fetch another array of data from another table for each user in that table to look like this

user id    user name        user email
1          Mike             mike@yahoo.com
order:10   order total:50   order date: 2016-09-12
order:12   order total:100  order date: 2016-09-14
2          Tom              Tom@live.com
order:15   order total:80   order date: 2016-09-13
order:16   order total:120  order date: 2016-09-14
order:17   order total:140  order date: 2016-10-10

Controller's index() function contains this code

$this->data['users'] = $users; //fetches data from a model function
$this->data['content'] = $this->load->view('users_view', $this->data);

So I have the function orders($user_id) that fetches orders from DB for a user. The view code:

<table>
        <tr>
            <th>
                 <?php echo 'user id';?>
            </th>
            <th>
                 <?php echo 'user name';?>
            </th>
            <th>
                <?php echo 'user email';?>
            </th>         
        </tr>
        <?php foreach($users as $val){
            ?>
            <tr>
                <td>
                     <?=$val->user_id?>
                </td>
                <td>
                    <?=$val->user_name;?>
                </td>
                <td>
                     <?=$val->user_email;?>
                </td>                   
            </tr>
        <?php       
        // how to call the model function here and loop through the orders array to 
display orders for each user?

        }?>

    </tbody>
</table>
  • 写回答

2条回答 默认 最新

  • doufang1954 2016-10-11 01:16
    关注

    You should index your orders table to have some common index key with the other table, such as username(order_username), user id(order_userid), user email(order_useremail), so you can fetch only orders made by username/user id/email.

    Say this is your controller :

    class MY_Controller extends CI_Controller 
    {
        public function __construct() 
        {
            parent::__construct();
            //Load My_Model
            $this->load->model("my_model");
        }
    
    
        public function index() 
        {
            $users = $this->my_model->get_users();
            foreach ($users->username as $user) {
                $user_orders = $this->my_model->get_orders_by_username($user);
            }
            $this->load->view('users_view', array("user_orders" => $user_orders, "users" => $users));
        }
    }
    

    And this is your model that fetches data from users table and orders table with the same username in common :

    class MY_Model extends CI_Model 
    {
        public function get_users() 
        {
            $results = $this->db->get('users');
            return $results;
        }
    
        public function get_orders_by_username($user)
        {
            $this->db->select('*');
            $this->db->where("order_username", $user);
            $results = $this->db->get('orders');
            return $results;
        }
    }
    

    Now you can view the data in the view file, and you can use them like this : $users->username - $users->email, or in a foreach loop like so :

    foreach ($users as $user) {
        foreach ($user_orders as $order) {
        echo "$user->user_name , $user->user_lastname - $user->user_email";
        echo "$order->orders_date - $order->orders_count";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路