⚠ In case you've missed it we are migrating to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This forum is read-only and soon will be archived. ⚠


How to pass more data to the view?


  • Please log in to reply
4 replies to this topic

#1 geekygirl

geekygirl

    Member

  • Members
  • PipPip
  • 15 posts

Posted 17 July 2015 - 09:55 PM

I just discovered Grocery CRUD and it looks really promising... 

BUT I have a small problem...  

 

In my controller I have something pretty simple like this:

public function __construct() {
	parent::__construct();
	$this->load->library('grocery_CRUD');
}

public function index() {
	$crud = new grocery_CRUD();

	$crud->set_table('table_name');
	$crud->set_subject('Name');

	$output = $crud->render();
	$this->load->view('view.php', $output);
}

And in my view I have a simple echo

<?php echo $output; ?>

And this works just fine...

 

But the problem I have is how do I pass more data from controller to the view.

I have tried passing array for example

$data['output'] = $output;
$data['some_more_data'] = 'Some random data'; 
$this->load->view('view.php', $data);

But this simply won't work...

What am I doing wrong?

Anyone have a solution for this?

 

 

PS: I'm a newbie when it comes to CI (and this CRUD) ...



#2 geekygirl

geekygirl

    Member

  • Members
  • PipPip
  • 15 posts

Posted 17 July 2015 - 10:02 PM

I managed to solve it using this...

 

First I had to fix the css and js output scripts

<?php  foreach($output->css_files as $file): ?>
	<link type="text/css" rel="stylesheet" href="<?php echo $file; ?>" />
<?php endforeach; ?>
<?php foreach($output->js_files as $file): ?>
	<script src="<?php echo $file; ?>"></script>
<?php endforeach; ?>

And then the output

<?php echo $output->output; ?>

And then it worked out...

 

I could also access my additional data 

<?php echo $some_more_data; ?>

But is there some more elegant solution for this?



#3 Paul Savostin

Paul Savostin

    Advanced Member

  • Members
  • PipPipPip
  • 229 posts

Posted 17 July 2015 - 10:43 PM

Hi! Lets say u have the base admin controller :

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

/**
 * Common controller where we use Grocery CRUD
 */
 
class Admin_Controller_GC extends CI_Controller
{
    //GC
    public $crud;

    //current table
    public $table;

    //all data that we will output
    public $data = array();
    
    function __construct()
    {
        parent::__construct();
        
        $this->load->library('grocery_CRUD');
        
        $this->crud = new grocery_CRUD(); //init Grocery Crud
        
        //set theme of Grocery CRUD
        $this->crud->set_theme('datatables');
        
        //common properties - for example u have common fields active users, active posts and so on  
        $this->crud->field_type('active','true_false',array(0 => 'No', 1 => 'Yes'));                
        
          
        
    }
    
    
    
    //output data
    function _example_output()
    {
        //set current table
        $this->crud->set_table($this->table_bd);
        
        //join our additional data with GC data
        $this->data = array_merge($this->data, (array) $this->crud->render());        
        
        //do the output
        $this->load->view('admin/main_tpl', $this->data);
    }
}

So I stay only that stuff u asking about, cause here in this controller we can do many many useful things and I do:)

Then u extend this controller with that u need, add your data like $this->data['some_var'] = $some_data;

Set your database tables like $this->table = 'your_table';

And after all actions with CG api at the end of function just call $this->_example_output();

That's it! ;)



#4 geekygirl

geekygirl

    Member

  • Members
  • PipPip
  • 15 posts

Posted 17 July 2015 - 11:01 PM

array_merge works! Thanks!

 

Just curious why do you define these variables $crud, $table and $data and then access them trough $this->data?

So you can use them in entire class (class variables)?



#5 Paul Savostin

Paul Savostin

    Advanced Member

  • Members
  • PipPipPip
  • 229 posts

Posted 17 July 2015 - 11:33 PM

I can use them in all subclasses that extends this controller, like u see above i can set common properties and many other stuff and in subclasses add some other data or ovveride anything.

One little example, in every controller u should write $crud = new grocery_CRUD(); and here u write this only one time!

believe me this is very useful and i think this is just the best practices :)






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users