php - Passing JSON data from Controller to Model -
i have following codeigniter application, in trying read external json file in controller, , pass model function getkey()
, , finally, pass data returned function view. keep getting error message "php parse error: syntax error, unexpected '$this' (t_variable), expecting function (t_function)" , unsure of causing it. new using codeigniter, appreciated.
my controller:
class test extends ci_controller{ public function __construct(){ parent::__construct(); } var $test_id = 1; var $json_key; $this->load->model('test_model'); $json_key = $this->test_model->getkey($test_id); $json_key = json_decode($json_key); $data['test_key'] = $json_key; $this->load->view('test_view', $data); }
my model:
class test_model extends ci_model{ var $image_array = array(); var $test_key = array(); var $test_name = ''; public function __construct(){ parent::__construct(); } public function getkey($test_id){ switch($test_id){ case 1: $test_name = "sample1"; break; case 2: $test_name = "sample2"; break; case 3: $test_name = "sample3"; break; } $image_array = file_get_contents('../files/' . $$test_name . '_key.json'); $test_key = shuffle($image_array); return $image_array; } }
my view:
<html> <head> <title>test</title> </head> <body> <h1>test screen</h1> <?php print_r($test_key); ?> <?php print_r($json_key); ?> </body> </html>
thanks ahead of time.
your controller wrong. code should in function. this:
class test extends ci_controller { public function __construct() { parent::__construct(); } function index() { $this->load->model('test_model'); $test_id = 1; $json_key = $this->test_model->getkey($test_id); $json_key = json_decode($json_key); $data['test_key'] = $json_key; $this->load->view('test_view', $data); } }
Comments
Post a Comment