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条)

报告相同问题?

悬赏问题

  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,