根据发送到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>

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";
    }
}

You definitely do not do this. This is entirely wrong. This is how it should be done:

  1. Request to controller
  2. Controller calls relevant libraries and models to get data together
  3. Controller sends data to views

So in your controller you need to call your data for users from one model, then foreach user get orders from another model. How you do this in your models or libraries is up to you of course, but the controller needs to collect all the required data up front.

In constructing the views, your controller or your template library or your page model might call other sub-views or partials to create strings for your page chunks, modules or set sections of your page. Then use those strings as part of the main view. OR your main view might call other views directly. Either way, the data required for the view should be sent to the view. In the case of views calling other partials (which I personally abhor but some people seem to like it) all the data is sent to the original view and the partial is sent only the data it requires as a subset of the original dataset.

Views should never call models, other controllers, or libraries. Think of them as static elements of the page design, that simply receive raw data and dictate how to display that data. That is it.

In the same way you should never have HTML in your controllers or models, you should never have PHP processing in your views (except layout constructs like foreach loops, if statements etc).