php - Conditional does not seem to be working in Controller -
my goal to, upon clicking button, redirect practice_test_view main controller's function setuptask, passing in variable $test_id. part seems work. however, once function called, no matter test_id variable is, seems running through main else clause, because calls getkey function model, when should not. causes following errors appear in browser tools:
<p>severity: notice</p> <p>message: undefined variable: test_name</p> <p>filename: models/main_model.php</p> <p>line number: 45</p> <p>severity: warning</p> <p>message: file_get_contents(files/_key.json): failed open stream: no such file or directory</p> <p>filename: models/main_model.php</p> <p>line number: 45</p>
practice_test_view (relevant portion):
<div style='position: relative; width: 595px; margin: 20px auto;'> <input style='text-align:center; width:220px;' type="button" id='rounded' value="click here begin test" onclick="location.href='<?php echo site_url("main/setuptask/". "/" . "$test_id"); ?>'";> </div>
my main controller function:
public function setuptask($test_id){ $this->load->model('main_model'); if($test_id == 5 || $test_id == 6){ $random_array = $this->main_model->createrandomarray(); shuffle($random_array); if($test_id == 6){ $test_key = $this->main_model->createspatialkey($random_array); $data['test_key'] = $test_key; $data['test_id'] = $test_id; } else{ $data['test_key'] = $random_array; $data['test_id'] = $test_id; } $this->load->view('test_view', $data); } else{ $json_key = $this->main_model->getkey($test_id); $json_key = json_decode($json_key, true); shuffle($json_key); $data['test_key'] = $json_key; $data['test_id'] = $test_id; $this->load->view('test_view', $data); } }
my main model:
class main_model extends ci_model{ public function getkey($test_id){ switch($test_id){ case 1: $test_name = "example"; break; case 2: $test_name = "example"; break; case 3: $test_name = "example"; break; } $image_array = file_get_contents('files/' . $test_name . '_key.json'); return $image_array; } public function createrandomarray(){ $verbal_array = array(); for($i=0; $i<60; $i++){ $random = mt_rand(1,4); $random_array[$i] = $random; } return $random_array; } public function createspatialkey($random_array){ $test = array(); $image_array = file_get_contents('files/spatial_working_memory_key.json'); $image_array = json_decode($image_array, true); foreach($random_array $question){ $q = $question - 1; array_push($test, $image_array[$q]); } return $test; } }
in switch statement in model, i've replaced actual test names 'example' , have shortened because rest of statement organized partial shown.
any appreciated!
in view change
<?php echo site_url("main/setuptask/". "/" . "$test_id"); ?>
to
<?php echo base_url("main/setuptask/". $test_id); ?>
make following changes getkey() function
public function getkey($test_id){ switch($test_id){ case 1: $test_name = "example"; break; case 2: $test_name = "example"; break; case 3: $test_name = "example"; break; default: $test_name = ''; break; } if($test_name !== ''){ $image_array = file_get_contents('files/' . $test_name . '_key.json'); return $image_array; } else { return false; } }
Comments
Post a Comment