Reputation: 1045
I'm new to CodeIgniter. I'm trying to output a view again after clicking a submit but the view has a variable from the controller, but when I tried it, it doesn't work.
<?php
class Site extends CI_Controller{
public $data1['value'] = "What to insert";
public function index(){
$this->load->view('home',$this->data1);
}
public function get_product(){
$data = array(
'product_name' => $this->input->post('prod_name')
);
$this->site_model->insert_product($data);
$this->load->view('home',$this->data1);
}
}
?>
Here's my view:
<div id="container">
<h1><?php echo $data1;?></h1>
<?php echo form_open('site/get_product'); ?>
<p>
<label for="product">Product Name </label>
<input type="text" id="product" name="prod_name" />
</p>
<input type="submit" name="submit_but" value="submit">
<?php echo form_close(); ?>
</div>
In get_product
I need to call again the view but I know it'll be such a hassle if I'm going to declare it again inside the function.
Error:
Parse error: syntax error, unexpected '[', expecting ',' or ';' in C:\xampp\htdocs\code_igniter\application\controllers\site.php on line 9
Upvotes: 0
Views: 1660
Reputation: 535
__construct function is calling every time you run the script so you can add your constant values in this function for view or other usage.
<?php
class Site extends CI_Controller{
public $data1 = array();
public function __construct() {
$this->data1['value'] = "bla bla";
}
public function index(){
$this->load->view('home',$this->data1);
}
public function get_product(){
$data = array(
'product_name' => $this->input->post('prod_name')
);
$this->site_model->insert_product($data);
$this->load->view('home',$this->data1);
}
}
?>
Upvotes: 1
Reputation: 522024
The description of the actual problem or error is vague at best, but I'll guess that this is the problem:
public $data1['value'] = "What to insert";
That's not a valid declaration of a property. If you want to declare the property "$data1
" as being an array, then you have to do so:
public $data1 = array('value' => "What to insert");
Upvotes: 2