我有一个问题,我不知道如何从控制器中循环获取多个值以获得用户在每个课程中的存在。我想在视图中显示用户在每个课程中的存在。非常感谢你。
这是我的控制器(MenteeController)
public function indexMentee(){
$this->load->model('UserModel');
$userID=$this->session->userdata('userID');
$groupID = $this->UserModel->getGroupID($userID);//to get user's groupID
$records = $this->UserModel->getGroupCourseLearned($groupID);//to get group's course learned
$data['courseID'] = $records['courseID'];
$data['countCourseID'] = $records['countCourseID'];
foreach($data['courseID'] as $d){
$data['present'] = $this->UserModel->totalPresentCourse($d->courseID,$userID);//total user's presence in certain course
}
$this->load->view('mentee/home',$data);
}
这是我的模型(UserModel)
public function getGroupID($userID){
$query = $this->db->query("SELECT DISTINCT groupID FROM mslearningsession WHERE menteeID='".$userID."'");
if($query->num_rows()>0){
return $query->row()->groupID;
}else{
return false;
}
}
public function getGroupCourseLearned($groupID){
$query = $this->db->query("SELECT DISTINCT courseID FROM mslearningsession WHERE groupID IN('".$groupID."')")->result();
return array(
'courseID' => $query,
'countCourseID' => count($query),
);
}
public function totalPresentCourse($courseID,$userID){
$query = $this->db->query("SELECT COUNT(sessionID)AS present FROM mslearningsession WHERE menteeID='".$userID."' AND courseID='".$courseID."'");
if($query->num_rows()>0){
return $query->row()->present;
}else{
return false;
}
}
这是我的观点(home.php)
<?php
echo "total courses learned : ".$countCourseID."<br>";//the courses are more than one
echo "total user's presence in each course :";
foreach($courseID as $course){
echo $course->courseID;
//I don't know how to get multiple values from looping in controller to get the user's presence in each course
}
?>
答案 0 :(得分:0)
你可以这样做
$data['new_array'] = array(); //declare an empty array first
foreach($data['courseID'] as $d){
$data['new_array'] = $this->UserModel->totalPresentCourse($d->courseID,$userID);//all the value will be pushed in to new array
}
$this->load->view('mentee/home',$data);//pass the array to get the data