doushang3352 2016-10-10 12:51
浏览 52
已采纳

根据发送到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-10 17: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部