Jump to content


How to pass extra data on a grocery crud view

view data render

  • Please log in to reply
12 replies to this topic

#1 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

Posted 09 November 2012 - 05: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)
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!!!

Edited by David Olive Tree Oster, 09 November 2012 - 05:51 PM.

____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#2 jgalak

jgalak

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts

Posted 24 February 2013 - 12:53 AM

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



#3 sachin vairagi

sachin vairagi

    Advanced Member

  • Members
  • PipPipPip
  • 39 posts

Posted 24 February 2013 - 06:57 PM

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



#4 silva96

silva96

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 27 February 2013 - 02: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



#5 abhishek

abhishek

    Member

  • Members
  • PipPip
  • 10 posts

Posted 09 May 2013 - 07:59 AM

I used the code but dint work out.

 

 

http://www.grocerycr...a-data-to-view/



#6 abhishek

abhishek

    Member

  • Members
  • PipPip
  • 10 posts

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



#7 Kangz

Kangz

    Newbie

  • Members
  • Pip
  • 1 posts

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.

 

.



#8 Nitin Thokare

Nitin Thokare

    Member

  • Members
  • PipPip
  • 25 posts

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 silva96):

 

$output = $crud->render();

$output->extra = $extra_contents;



#9 sourov08

sourov08

    Newbie

  • Members
  • Pip
  • 6 posts
  • LocationSylhet bangladesh

Posted 27 October 2014 - 02: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;   ?>
 

 



#10 sparklet

sparklet

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 25 May 2015 - 10: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 -->
   ....

					
					

#11 rostamiani

rostamiani

    Member

  • Members
  • PipPip
  • 13 posts

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



#12 mp3man

mp3man

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 19 June 2019 - 02: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


#13 Avinesh

Avinesh

    Newbie

  • Members
  • Pip
  • 2 posts
  • LocationFiji

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!!!

 







Also tagged with one or more of these keywords: view, data, render

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users