Welcome Guest, Not a member yet? Register   Sign In
codeigniter parser php issue and set value in form
#1

I use Codeigniter 2.x and this is function in my controller:
Code:
$query = $this->users_mod->changeProductData($idPro);
$url = base_url();
$data = array(
             'ap_heading' => 'Edit product',
             'base_url' => base_url(),
             'ap_entries' => $query,
             'ap_product' => 'active',
             'ap_username' => $this->session->userdata['logged']['username'],
             'ap_content' => $this->load->view('editproduct', '', true)
           );

$this->parser->parse('template', $data);

In template view I call another view with this method:
Code:
<div class="container-fluid main-body">
   <?= $ap_content ?>      
</div>

And on the end in view editproduct.php I list data in form.

But I have problem how to set value in form when form validation show some errors to still keep data in field. I done that like this before I start use parse:

Code:
<label class="radio-inline"><input type="radio" name="active" id="active1" value="0" class="radio" <?php if($row->active==0) { echo set_radio("active", "0", true); } else { echo set_radio("active", "0"); } ?>>inactive</label>

I have two question:

  1. As you can see in template.php I can write php code and call variable (<?= $ap_content), but that doesn't work in view editproduct.php. Can I somehow use another variable from array in editproduct.php with php tag?

   2. Can I somehow in controller in array write some variable with php code. For example:

   'ap_var' = '<?php echo set_radio("active", "0", true); ?>'

and call that variable in editproduct.php as {ap_var}?
Reply
#2

1.

PHP Code:
'ap_content' => $this->load->view('editproduct'''true

The second parameter of $this->load->view() is the data you are passing to the view (and this is supposed to be an array or object). Since you're loading the view into $data['ap_content'] when initially defining the $data array, you can't pass anything from $data into the view.

You could do something like this, though:

PHP Code:
$query $this->users_mod->changeProductData($idPro);
$url base_url();
$data = array(
    
'ap_heading'  => 'Edit product',
    
'base_url'    => base_url(),
    
'ap_entries'  => $query,
    
'ap_product'  => 'active',
    
'ap_username' => $this->session->userdata['logged']['username'],
);

$ap_content $this->load->view('editproduct'$datatrue);

$data['ap_content'] = $ap_content;
$this->parser->parse('template'$data); 

Note that you still wouldn't be able to access $ap_content within the editproduct view, since the 'ap_content' key wasn't set in $data when the editproduct view was loaded (and, since the content of $ap_content is the editproduct view, I wouldn't expect that to work out very well, anyway).

2.

PHP Code:
'ap_var' '<?php echo set_radio("active", "0", true); ?>' 

You could do something like this:
PHP Code:
$data = array(
    
'ap_heading'  => 'Edit product',
    
'base_url'    => base_url(),
    
'ap_entries'  => $query,
    
'ap_product'  => 'active',
    
'ap_username' => $this->session->userdata['logged']['username'],
    
'ap_var'      => set_radio("active""0"true),
);

$ap_content $this->load->view('editproduct'$datatrue);

$data['ap_content'] = $ap_content;
$this->parser->parse('template'$data); 

However, using the form helper functions this way feels really awkward.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB