我正在構建一個小應用程序,允許用戶添加/刪除課程中的學生。每次我添加/刪除課程時,數據庫都會更新,我會更新用戶的課程總數。當我將一個學生添加到課程中時,它工作正常,但是當我去除它們時,我返回0(來自我的PHP模型函數)。我相信這可能是與jQuery的問題,我想我可能需要使用克隆?所以我很抱歉這麼長的帖子只是想確保你們有足夠的代碼來看待。 這裏是添加和刪除用戶的過程中我的jQuery代碼:添加/刪除元素並通過AJAX更新其屬性
function addUserToClass(user)
{
//creates the a data string to send to the controller with the user's id and the course id
var postString = "courseid="+$("#course option:selected").val()+"&uid="+user.find('input').val();
//adds the user to the course list in the database
$.post('index.php/admin/addUserToCourse',postString);
$("#courseList").append(user);
$('#courseList .addUser').text('(-)').removeClass('addUser').addClass('removeUser');
}
//removes a user from the selected course in the database as well as on the screen
function removeUserFromClass(user)
{
var postString = "courseid="+$("#course option:selected").val()+"&uid="+user.find('input').val();
//removes the user fromt the course in the database
$.post('admin/removeUserFromCourse',postString);
$(user).remove();
$('#usertable').append(user);
$('#usertable .removeUser').text('(+)').removeClass('removeUser').addClass('addUser');
}
這裏是我的jQuery,更新他們在球場數量:
function updateUserCourses(uid)
{
var postString = 'uid='+uid.siblings(':input').val();
$.post('index.php/admin/getUsersCourses', postString , function(data){
data = jQuery.parseJSON(data);
uid.siblings('.internalCounter').text(data.internal);
uid.siblings('.externalCounter').text(data.external);
})
}
不知道你需要這個但我的PHP(笨控制器)被稱爲是:
function getUsersCourses()
{
$this->load->model('course_model');
$courses = $this->course_model->getUsersCourses($_POST['uid']);
echo json_encode($courses);
}
而且我的模型功能:
function getUsersCourses($uid)
{
$external = array();
$internal = array();
$final = array();
//Selects all of the courses the user has been added to
$usersCourses = $this->db->query("SELECT courseid FROM final_course_list WHERE uid='{$uid}'");
//If they have courses, proceed to process them
if ($usersCourses->num_rows() > 0)
{
//For every course they have, we get the category of the course
foreach($usersCourses->result() as $row)
{
$courseCat = $this->db->query("SELECT category FROM courses WHERE id = '{$row->courseid}'")->row_array();
if($courseCat['category'] == 'External Topics')
{
array_push($external, $courseCat);
}
else if($courseCat['category'] == 'Internal Topics')
{
array_push($internal, $courseCat);
}
}
//adds the internal and external amount of classes to one array
$final['internal'] = count($internal);
$final['external'] = count($external);
$result = $final;
}
else
{
$result = 0;
}
return $result;
}
我翻了一番檢查和正確的名字,我叫removeUserFromClass()在事件監聽器(此處未列出)。感謝您的幫助,並指出我的不一致的命名約定。 – dan 2011-01-28 16:18:00