phpmodel-view-controllercodeigniter-3grocery-crud

How to send variable to view in Grocery Crud?


I am using Grocery Crud for my web application, I want to send some data from user model to the view, however, since grocery uses $output variable which is initialized as $output = $crud->render(); and always send it to the view; therefore, I cannot send other data to the view.

Here is my code in controller and what I tried but it does not success.

<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class User1 extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->library('grocery_CRUD');
        $this->load->model('M_User');
    }
            
    public function index()
    {
        $this->user();
    }
            
    public function user($id)
    {
        // get user information
        $user = $this->M_User->get_user_info($id);
    
        // array variable that I want to send to view a long with variable $output
        $u = array(
            'owner_name' => $user->owner_name,
            'owner_phone' => $user->owner_phone,
            'owner_email' => $user->owner_email,
            'owner_facebook' => $user->owner_facebook,
            'owner_picture' => $user->owner_picture,
            'owner_gender' => $user->owner_gender
        );
    
        // Start Grocery Crud
        $crud = new grocery_CRUD();

        $crud->set_table('room');
        $crud->set_subject('subject');

        $crud->where('owner_id', $id);
        $output = $crud->render();
    
        // $output = array_merge($output,$u); This is what I tried to do but it did not work.
    
        $this->load->view('user1', $output);
    }
}

Is there any way that I could send my variable $u to view along with variable $output?


Solution

  • Controller

    //...
    //do all your Grocery Crud staff
    //...
    $output = $crud->render();
    
    //rending extra value to $output
    $data['variable1']='variable_value';
    $data['variable2']=12;
    
    $output->data = $data;
    
    $this->load->view('...your page...',$output);
    

    View

    echo $data['variable1'];
    echo $data['variable2'];