⚠ In case you've missed it, we have migrated 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 is an archived forum. ⚠

  •     

profile picture

How to pass extra data on a grocery crud view



davidoster

davidoster
  • profile picture
  • Member

Posted 09 November 2012 - 17:22 PM

I was trying to pass some extra values of data on grocery crud view when I came up with the following solution.
It's simple, fast and would be great if the core developer of grocery crud could implement it.
FYI: I am using the current latest version of grocery crud 1.3.2.

Normally we would write this:

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


I propose this(after the changes):


$output = $this->grocery_crud->render($extra);
$this->load->view('template.php',$output);



In order to make it happen:
(file: application/libraries/grocery_crud.php)
[b]Step 1 : function render [/b](line:~3822)


public function render($extra = null)
{
...

return $this->get_layout($extra);
}


[b]Step 2 : function get_layout [/b](line:~1825)

protected function get_layout($extra = null)
{
...
if($this->echo_and_die === false)
{
return (object)array('output' => $this->views_as_string, 'js_files' => $js_files, 'css_files' => $css_files, 'extra' => $extra);
}
...
}


And from now on you will be able to pass data to any grocery crud view!!!

jgalak

jgalak
  • profile picture
  • Member

Posted 24 February 2013 - 00:53 AM

This worked great.  Please, please, please incorporate it (or something like it) into the official version.


sachin vairagi

sachin vairagi
  • profile picture
  • Member

Posted 24 February 2013 - 18:57 PM

thank you very much davidoster, it is really much helpfull.


silva96

silva96
  • profile picture
  • Member

Posted 27 February 2013 - 14:31 PM

I would suggest what I do always with views, Use a template with header, content, footer. (after the theory I'll explain how to use it with grocery crud)

 

first: The template: (template.php)

 

<?php
$this->load->view('includes/header');
$this->load->view($content_view);
$this->load->view('includes/footer');

 

With this, we have a standard template.

 

To load a view, use this code:

 

class Users enxtends CI_Controller {
//any controller...
  public function login(){ //any function...
    $data['content_view']='users/login';
    $this->load->view('template',$data);

  }

This way, you are loading a view within a template, passing extra data to the template (second parameter $data).

 

With this logic, you don't need to change grocery-crud's core, try this instead:

 

Header: include crud's styles and javascripts. (includes/header.php)

 

<?php 
if(isset($css_files)){
      foreach($css_files as $style){
          echo '<link href="'.$style.'" rel="stylesheet"/>';
      }
}
if(isset($js_files)){
      foreach($js_files as $script){
          echo '<script src="'.$script.'" type="text/javascript"></script>';
      }
}

 

crud_content_view: echo the output (crud_content_view.php)

 

<?php echo $output;?>

 

Controller/function (crud_controller.php)

 

 ·
 ·
 ·
$output=$crud->render(); //$output is an object with attributes: js_files,css_files,output
$output->content_view='crud_content_view'; //we add a new attribute called content_view
//because our template loads the view content_view for the content.
//if we want to pass data, try this.
$data=array();
$data['variable1']='variable_value';
$data['variable2']=12;
$data['variable3']=array('subvariable1'=>'value1-1','subariable2'=>'value1-2');
//etc, then assign data to output.
$output->data=$data;
$this->load->view('template',$output);

 

Then to access data in the view try this: (in crud_content_view.php)

 

echo $data['variable1'];
echo $data['variable2'];
foreach($data['variable3'] as $var3){
 echo $var3;
}

 

This is good practice from codeigniter's manual, try to not alter the crud library core.

 

Regards


abhishek

abhishek
  • profile picture
  • Member

Posted 09 May 2013 - 07:59 AM

I used the code but dint work out.

 

 

/topic/1657-pass-extra-data-to-view/


abhishek

abhishek
  • profile picture
  • Member

Posted 09 May 2013 - 11:01 AM

I have used a different approach, just copied the set_subject function and it is working fine, so i want to share it with you.

 

Go to grocery crud Library and follow these simple steps:

 

Step1. Declare         

protected $extra = null;

 

Step2.Create function

public function set_extra($extra)
    {        
        $this->extra = $extra;
        return $this;
    }

Step 3.Add this into protected function get_common_data()

$data->extra                 = $this->extra;

 

and now You can pass Value in your controller like

$crud->set_extra('Value');

 

Now you can use  $extra in your view


Kangz

Kangz
  • profile picture
  • Member

Posted 09 April 2014 - 09:47 AM



I would suggest what I do always with views, Use a template with header, content, footer. (after the theory I'll explain how to use it with grocery crud)

 

first: The template: (template.php)

<?php
$this->load->view('includes/header');
$this->load->view($content_view);
$this->load->view('includes/footer');

With this, we have a standard template.

 

To load a view, use this code:

class Users enxtends CI_Controller {
//any controller...
  public function login(){ //any function...
    $data['content_view']='users/login';
    $this->load->view('template',$data);

  }

This way, you are loading a view within a template, passing extra data to the template (second parameter $data).

 

With this logic, you don't need to change grocery-crud's core, try this instead:

 

Header: include crud's styles and javascripts. (includes/header.php)

<?php 
if(isset($css_files)){
      foreach($css_files as $style){
          echo '<link href="'.$style.'" rel="stylesheet"/>';
      }
}
if(isset($js_files)){
      foreach($js_files as $script){
          echo '<script src="'.$script.'" type="text/javascript"></script>';
      }
}

crud_content_view: echo the output (crud_content_view.php)

<?php echo $output;?>

Controller/function (crud_controller.php)

 ·
 ·
 ·
$output=$crud->render(); //$output is an object with attributes: js_files,css_files,output
$output->content_view='crud_content_view'; //we add a new attribute called content_view
//because our template loads the view content_view for the content.
//if we want to pass data, try this.
$data=array();
$data['variable1']='variable_value';
$data['variable2']=12;
$data['variable3']=array('subvariable1'=>'value1-1','subariable2'=>'value1-2');
//etc, then assign data to output.
$output->data=$data;
$this->load->view('template',$output);

Then to access data in the view try this: (in crud_content_view.php)

echo $data['variable1'];
echo $data['variable2'];
foreach($data['variable3'] as $var3){
 echo $var3;
}

This is good practice from codeigniter's manual, try to not alter the crud library core.

 

Regards

 

Thank you silva96. you saved my life on this one especially passing the extra variables to the view. Been having problems all week, until i saw this.

 

.


Nitin Thokare

Nitin Thokare
  • profile picture
  • Member

Posted 12 June 2014 - 01:51 AM

I have used a different approach, just copied the set_subject function and it is working fine, so i want to share it with you.

 

Go to grocery crud Library and follow these simple steps:

 

Step1. Declare         

protected $extra = null;

 

Step2.Create function

public function set_extra($extra)
    {        
        $this->extra = $extra;
        return $this;
    }

Step 3.Add this into protected function get_common_data()

$data->extra                 = $this->extra;

 

and now You can pass Value in your controller like

$crud->set_extra('Value');

 

Now you can use  $extra in your view

 

I tried this (followed all steps as mentioned), but I'm getting Null when checking $extra variable in view..

What could be the problem??

 

I think.. It's because, $data is not passed to any of the views (create/read/update/delete)...

(none of the variables from $data is found to be defined on view page)

 

I think better way is to set variable in $output itself after render as follows (mentioned by [member=silva96]):

 

$output = $crud->render();

$output->extra = $extra_contents;


sourov08

sourov08
  • profile picture
  • Member

Posted 27 October 2014 - 14:52 PM

I was trying to Paas GC and some extra data into view via array  when I came up with the following solution.

This is controller :

            $crud = new grocery_CRUD();
            $crud->set_table('tickets');
            $crud->set_subject('Order');            
            $output = $crud->render();

                $data = array(
                    'title' => "Unsolved Tickets ! ",                          
                    'page_heading' => "Unsolved tickets ! ",                          
                    'output'      =>  $output,
                );      
        
            $this->load->view('example',$data);  

and this view file :     
            <?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; ?>

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

 


sparklet

sparklet
  • profile picture
  • Member

Posted 25 May 2015 - 22:04 PM

Thank you Nitin, your solution worked perfectly for me.

 

In my controller I added the following:

        $output = $crud->render();
        
        $state = $crud->getState();
        if ($state=='list') { 
          $output->extra = '<h3>Please add an item to the list</h3>';
        }
        elseif ($state=='edit' || $state=='add') {
          $output->extra = '<h3>Make sure to write some serious stuff in here</h3>';
        }
        elseif ($state=='success') {
          $output->extra = '<h3>Thank you for your participation</h3>';
        }
        else {
          $output->extra = '';
        }
     
        $this->_example_output($output);  

So in my view file I could use the variable $extra:

<!-- Beginning header -->
     <div>
        <h2>Welcome to my survey</h2>
        <?php echo $extra; ?>  
     </div>
     <div style='height:20px;'>&nbsp;</div>
<!-- End of header-->

     <div>
       <?php echo $output; ?>
     </div>

<!-- Beginning footer -->
   ....


                                        

rostamiani

rostamiani
  • profile picture
  • Member

Posted 16 November 2015 - 03:02 AM

This approach is so simple without any modification:

 

$output = $crud->render();
$Value = array("Value" => 2);

$output = array_merge( (array)$output, $Value);

$this->load->view('myView',$output);

 

Goodluck


mp3man

mp3man
  • profile picture
  • Member

Posted 19 June 2019 - 14:59 PM

These things didn't work for me, but I've found a way to work perfectly. My main idea is that the main edit.php grocery customized view, includes some extra data. This is passed as a $extra associative array, and it work as follows:

 

(You need to modify native grocery library)

 

Grocery_CRUD_Layout class:

/* New property */
/** @var array New by DRC: Associative array to set extra variables to pass to final grocery view (i.e. edit.php) */
protected $extra_variables          = array();

Grocery_CRUC_Layout modify methods showAddForm(), showEditForm() and showReadForm(), adding next lines just before call to

/* NEW IF */
if(!empty($this->extra_variables))
    $data['extra'] = $this->extra_variables;

$this->_theme_view(...)

Grocery_CRUD main class, add next methods:

 
public function set_extra_variable(string $varName, $value) {
    $this->extra_variables[$varName] = $value;
    }
public function set_extra_variables(array $vars) {
    $this->extra_variables = $vars;
}
public function get_extra_variables() {
    return $this->extra_variables;
}
public function get_extra_variable($var) {
    if(in_array($var, $this->extra_variables))
        return $this->extra_variables[$var];
    return null;
}

Now on your working controller, the way to use these new funcionality is just the next:

$crud->set_extra_variable('user_id', $userId); //This sets in $extra view the key 'user_id' with it's value

Finally, inside final grocery theme view, like "edit.php" you can call

$extra['user_id'] //to get the value of $userId

Avinesh

Avinesh
  • profile picture
  • Member

Posted 03 July 2019 - 04:22 AM

Thanks davidoster, this works for me

I was trying to pass some extra values of data on grocery crud view when I came up with the following solution.
It's simple, fast and would be great if the core developer of grocery crud could implement it.
FYI: I am using the current latest version of grocery crud 1.3.2.

Normally we would write this:

$output = $this->grocery_crud->render();
$this->load->view('template.php',$output);
I propose this(after the changes):

$output = $this->grocery_crud->render($extra);
$this->load->view('template.php',$output);

In order to make it happen:
(file: application/libraries/grocery_crud.php)
Step 1 : function render (line:~3822)

public function render($extra = null)
{
...

return $this->get_layout($extra);
}
Step 2 : function get_layout (line:~1825)
protected function get_layout($extra = null)
{
...
if($this->echo_and_die === false)
{
return (object)array('output' => $this->views_as_string, 'js_files' => $js_files, 'css_files' => $css_files, 'extra' => $extra);
}
...
}
And from now on you will be able to pass data to any grocery crud view!!!

 


daniilmedved

daniilmedved
  • profile picture
  • Member

Posted 18 December 2021 - 14:19 PM

Thank you so much davidoster, it is really much helpfull for me

 

 

thank you dgcustomerfirst.blog


daniilmedved

daniilmedved
  • profile picture
  • Member

Posted 22 December 2021 - 12:31 PM

Goodluck from dg