我的目标是,在单击按钮后,从我的practice_test_view重定向到我的主控制器的函数setupTask,传入变量$ test_id。这部分似乎有效。但是,一旦调用了函数,无论test_id变量是什么,它似乎都在运行main else子句,因为它总是从我的模型中调用getKey函数,即使它不应该。这会导致以下错误出现在浏览器工具中:
<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 to open stream: No such file or directory</p>
<p>Filename: models/main_model.php</p>
<p>Line Number: 45</p>
practice_test_view(相关部分):
<div style='position: relative; width: 595px; margin: 20px auto;'>
<input style='text-align:center; width:220px;' type="button" id='rounded' value="Click here to begin test" onClick="location.href='<?php echo site_url("main/setupTask/". "/" . "$test_id"); ?>'";>
</div>
我的主控制器功能:
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);
}
}
我的主要模特:
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 as $question){
$q = $question - 1;
array_push($test, $image_array[$q]);
}
return $test;
}
}
在模型的switch语句中,我用'example'替换了实际的测试名称并缩短了它,因为语句的其余部分就像显示的部分一样。
非常感谢任何帮助!
答案 0 :(得分:2)
在您的视图中更改
<?php echo site_url("main/setupTask/". "/" . "$test_id"); ?>
到
<?php echo base_url("main/setupTask/". $test_id); ?>
对getKey()函数进行以下更改
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;
}
}