php - Declaring a variable outside a function in CodeIgniter -


i'm new codeigniter. i'm trying output view again after clicking submit view has variable controller, when tried it, doesn't work.

<?php     class site extends ci_controller{          public $data1['value'] = "what 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 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 need call again view know it'll such hassle if i'm going declare again inside function.

error:

parse error: syntax error, unexpected '[', expecting ',' or ';' in c:\xampp\htdocs\code_igniter\application\controllers\site.php on line 9 

__construct function calling every time run script can add constant values in function 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);     } } ?> 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -