您好我是codeigniter框架的初学者,我需要一些帮助。 我有一个表格(在表格组织中),需要填充各种数据。需要使用下拉菜单填充一个字段(第一行,第二列)(使用来自数据库的表中的数据)(选择选项菜单),然后我设法执行此操作,
但这就是我为此所做的事情:
当用户点击链接(T - 插入Labwork)时
实际上他调用了控制器的方法(my_courses())和那个方法:
1 - 从会话中获取用户名
2 - 加载模型。
3 - 然后他将用户名传递给加载的模型
4 - 如果模型返回false,则返回消息给用户
5 - 否则如果model返回true,那么我们将返回结果
CONTROLLERS方法如下所示:
function my_courses(){
$username = $this->session->userdata('username'); //1 username from session
$this->load->model('model_professor'); //2 we load a model,whose task is to load professors courses from table
$result =$this->model_professor->professors_classes($username); //3 so we send username to that model
if($result == false){
echo "Professor has no courses ";
}else{ //4 model will return results collected from table
$query = $this->model_professor->professors_classes($username);
$data['main_content'] = 'professor/t_professor_insert_labwork_2';
$data['another_content'] = 'professor/professor_view_links';
$data['records'] = $query;
$this->load->view('includes/template',$data);
}
}
MODEL方法如下所示:
function professors_classes($username){
$this->db->where("username", $username);
$query = $this->db->get("table_teach_a_course");
if($query->result()){
return $query->result();
}else{
return false;
}
}
最后我的VIEW(更短的版本,没有图像中的所有行)t_professor_insert_labwork_2看起来像这样:
<?php
echo form_open("controller_professor/insert_labwork");
?>
<table>
<tr>
<td>Course name:</td>
<td>
<?php if(isset($records)) { ?>
<select>
<?php foreach ($records as $row){ ?>
<option value=""> <?php echo $row->course_code; ?> </option>
<?php } ?>
</select>
<?php
}else {
echo"<h6>No records were returned!</h6>";
} ?>
</td>
</tr>
<tr> <td colspan="2"> <hr/> </td></tr>
<tr>
<td><input type="reset" value="Clear form"></td>
<td></td>
<td><input type="submit" value="Save"></td>
</tr>
</table>
<?php
echo form_close();
?>
<?php
echo validation_errors('<p style="color:red"></p>');
?>
他还会加载该视图和其他一些视图,当他加载我的t_professor_insert_labwork_2视图时,会出现问题。
这是HOME PAGE链接所称的:
$this->load->view('professor/t_professor_insert_labwork_2');
但是这只是加载了一个空的视图,并且没有填充下拉菜单,我理解为什么,因为没有什么可以触发我的方法来填充该菜单。但这是给我带来麻烦的,我只是不知道如何从我的视图中触发一些将数据返回到该字段的方法。
我的问题是,如何调用我的视图,这将自动触发一些将返回数据以填充此下拉字段的方法?
感谢大家愿意帮助我!!!!
答案 0 :(得分:0)
您需要修改您的控制器DEFAULT方法(当您访问www.mysite.com/mycontroller时调用的方法)这通常以您的控制器命名或称为索引,我将使用代码给出示例假设此方法是您的默认方法。查看此代码有两种可能的情况。
案例1 - 您在初始加载时没有用户名会话变量。
function my_courses(){
$this->load->model('model_professor'); //2 we load a model,whose task is to load professors courses from table
//set a default for username
$username = 'mydefaultusername';
//check if a username has been provided and update $username variable
if(isset($this->session->userdata('username')) && !empty($this->session->userdata('username'))){
$username = $this->session->userdata('username'); //1 username from session
}
$result =$this->model_professor->professors_classes($username); //3 so we send username to that model
if($result == false){
echo "Professor has no courses ";
}else{ //4 model will return results collected from table
$query = $this->model_professor->professors_classes($username);
$data['main_content'] = 'professor/t_professor_insert_labwork_2';
$data['another_content'] = 'professor/professor_view_links';
$data['records'] = $query;
$this->load->view('includes/template',$data);
}
}
&#13;
案例2 - 您有一个用户名会话变量,但如果没有,则需要默认结果集。
function my_courses(){
$username = $this->session->userdata('username'); //1 username from session
$this->load->model('model_professor'); //2 we load a model,whose task is to load professors courses from table
$result = $this->model_professor->professors_classes($username); //3 so we send username to that model
//check if you need to set a default
if(count($result) !> 0){
$result = //your method to get your default results.
}
if($result == false){
echo "Professor has no courses ";
}else{ //4 model will return results collected from table
$query = $this->model_professor->professors_classes($username);
$data['main_content'] = 'professor/t_professor_insert_labwork_2';
$data['another_content'] = 'professor/professor_view_links';
$data['records'] = $query;
$this->load->view('includes/template',$data);
}
}
&#13;
在第二种情况下,您必须在控制器的初始加载时设置会话或其他变量,以确保在您应该返回&#34;没有找到&#34;时,您不会返回默认结果。即。
//check if you need to set a default
if(count($result) !> 0 && !isset($_SESSION['init']) or $_SESSION['init'] == FALSE){
$_SESSION['init'] = 1;
$result = //your method to get your default results.
}