更新:
在下面的代码中,更新部分有2个输入框和2个下拉列表。当我选择下拉菜单代码时,相应的主题代码应显示,当我更新时,它应该更新为db。在我选择课程代码的情况下存在的主题代码变为null(没有值是prent)。所以任何人都可以帮助我。
控制器:student_site
<?php
class Student_site extends ci_controller
{
function index()
{
$data = array();
if($query = $this->student_model->get_exam_data())
{
$data['exam_data'] = $query;
}
$this->load->view('student_view', $data);
}
function studentupdate()
{
$data = array();
$exam_name = $this->input->post('exam_name');
$course_name = $this->input->post('course_name');
if($query = $this->student_model->get_exam_data())
{
$data['exam_data'] = $query;
}
if($query = $this->student_model->get_records($exam_name))
{
$data['records'] = $query;
}
if($query = $this->student_model->get_course_code_records($exam_name))
{
$data['course_records'] = $query;
}
if($query = $this->student_model->get_all_coursesubject_records())
{
$data['all_coursesubject_records'] = $query;
}
$this->load->view('student_detail_view', $data);
}
function studentcreate()
{
$data = array();
$exam_name = $this->input->post('exam_name');
$data['exam_name'] = $exam_name;
/*if($query = $this->student_model->get_exam_data())
{
$data['exam_data'] = $query;
}*/
if($query = $this->student_model->get_course_code_records($exam_name)) //get unique records of coursecode from the coursesubject table
{
$data['course_records'] = $query;
}
/*if($query = $this->student_model->get_subject_code_records($exam_name)) //write seperate function to get the the subject codes frm the coursesubject table based on the selected course code.
{
$data['subject_records'] = $query;
} */
$this->load->view('student_update', $data);
}
function subject_records()
{
$data = array();
$exam_name = $this->input->post('exam_name');
$course_name = $this->input->post('course_name');
if($query = $this->student_model->get_subject_records($exam_name,$course_name))
{
$data['subject_records'] = $query;
}
$this->load->view('subject_view', $data);
}
function create_student()
{
/*$data = $this->input->post();
print_r($data);
var_dump($this->input->post());
$data=$this->input->post();*/
$j=1;
$createstudent = $this->input->post('createstudent');
if( $this->input->post('createstudent') != false ){
foreach($createstudent as $row_id)
{
$this->form_validation->set_rules("register_number_" . $row_id, "Register No", "required|min_length[2]");
//$this->form_validation->set_rules("name_". $row_id,"`Name`","required|xss_clean|min_length[1]");
}
}
if ($this->form_validation->run() == FALSE){
$data["message"]="";
//$this->load->view("master_data/view_master_data_header",$data);
//$this->load->view("master_data/view_master_data_nav");
$this->load->view("student_detail_view",$data);
//$this->load->view("master_data/view_master_data_footer");
}
while($j<=$this->uri->segment(3))
{
$data = array(
'exam_name' => $this->input->post('exam_name'),
'register_number' => $this->input->post('register_number_'.$j),
'name' => $this->input->post('name_'.$j),
'course_code' => $this->input->post('course_name_id'.$j),
'subject_code' => $this->input->post('subject_name'),
);
// $exam_name = mysql_real_escape_string($_POST["exam_name_id".$j]);//
$register_number=$this->input->post('register_number_'.$j);
if ($register_number != "")
{
$this->student_model->add_record($data);
}
//$course_code = $this->input->post('course_code_id'.$j);
//$this->student_model->add_record($exam_name,$course_code,$subject_code,$data);
//$this->student_model->add_record($data);
$j++;
}
redirect('student_site', 'refresh');
}
function delete()
{
$this->student_model->delete_row();
redirect('student_site', 'refresh');
}
function manage_student()
{
$data['title']="Manage Student";
//query model to get data results for form
$data=array();
if($query=$this->student_model->get_student_records()){
$data['records']=$query;
}
$editstudent = $this->input->post('editstudent');
if( $this->input->post('editstudent') != false ){
foreach($editstudent as $row_id)
{
$this->form_validation->set_rules("register_number_" . $row_id, "register_number", "required|min_length[2]");
//$this->form_validation->set_rules("name_". $row_id,"`Name`","required|xss_clean|min_length[1]");
//$this->form_validation->set_rules("course_code_". $row_id,"`CourseCode`","required|xss_clean|min_length[1]");
//$this->form_validation->set_rules("subject_code_". $row_id,"`SubjectCode`","required|xss_clean|min_length[1]");
}
}
if ($this->form_validation->run() == FALSE){
$data["message"]="";
//$this->load->view("master_data/view_master_data_header",$data);
//$this->load->view("master_data/view_master_data_nav");
$this->load->view("student_detail_view",$data);
//$this->load->view("master_data/view_master_data_footer");
} else {
// single update - working
if( $this->input->post('editstudent') != false )
{
foreach ($editstudent as $row_id)
{
$data = array(
'register_number' => $this->input->post('register_number_'.$row_id),
'name' => $this->input->post('name_'.$row_id),
'course_code' => $this->input->post('course_code_id'.$row_id),
'subject_code' => $this->input->post('subject_code_id'.$row_id),
);
$this->student_model->update_student_records( $row_id, $data );
}
$this->session->set_flashdata('dbaction', 'Selected Records have been updated successfully');
redirect('student_site', 'refresh');
}
}
}
function update()
{
$data = array(
'exam_name' => $this->input->post('exam_name_id'),
'register_number' => $this->input->post('register_number'),
'name' => $this->input->post('name'),
'subject_code' => $this->input->post('subject_code'),
'course_code' => $this->input->post('course_code'),
);
$exam_name = $this->input->post('exam_name');
$register_number = $this->input->post('register_number');
$name = $this->input->post('name');
$course_code = $this->input->post('course_code');
$subject_code = $this->input->post('subject_code');
$this->student_model->update_student_record($exam_name,$register_number,$name,$course_code,$subject_code,$data);
$data = array();
if($query = $this->student_model->get_records($exam_name))
{
$data['records'] = $query;
}
$this->load->view('student_detail_view', $data);
}
function crud()
{
}
function deleting()
{
}
//No Need
/*function studentdata()
{
$this->load->model("student_model");
$data["result"]=$this->student_model->getdata("exam_name");
$this->load->view("student_view",$data);
}*/
/*function delete()
{
$data = array();
$exam_name = $this->input->post('exam_name');
if($query = $this->student_model->delete_row($exam_name))
{
$data['records'] = $query;
}
$this->load->view('student_detail_view', $data);
}*/
}
?>
视图:student_detail_view
<section id="tables">
<script>
jQuery(document).ready(function() {
oTable = jQuery('#studenttable').dataTable({
"bJQueryUI": true,
"sPaginationType": "full_numbers"
});
});
</script>
<script type="text/javascript" charset="utf-8">
function get_studentdetails() {
var exam_name = jQuery('#exam_name_id').val();
//alert("exam_name"+exam_name);
jQuery.ajax({
data: {
exam_name: exam_name,
},
type: 'POST',
url: 'student_site/studentupdate',
success: function(data){
//alert("inside change");
console.log(data);
jQuery('#details').html(data);
jquery('#deleting').html(data);
}
});
jQuery(document).ready(function(){
jQuery.ajax({
data: {
exam_name: exam_name,
},
type: 'POST',
url: 'student_site/studentcreate',
success: function(data){
//alert("inside change");
console.log(data);
jQuery('#crud').html(data);
jquery('#deleting').html(data);
}
});
});
}
function CheckBoxVerification(From)
{
alert('hai');
alert('hai:' $("table input[type=checkbox]:checked").length);
if(From == "Insert")
{
if(!!$('#rowInsert input:checked').length == false)
{
alert('please select at least one check box to Create ...!');
}
}
if(From == "Update")
{
if(!!$('#rowUpdate input:checked').length == false)
{
alert('please select at least one check box to Update ...!');
}
}
}
function get_subjectdetails1() {
//var index = jQuery('#index').val();
var course_name = jQuery('#course_name_id').val();
//alert("course_name"+course_name);
var exam_name = jQuery('#course_name_id>option:selected').text();
var exam_name = jQuery('#exam_name_id').val();
var ssubject_code = jQuery('#ssubject_code_id').val();
//var partsArray = exam_name.split('.');
//alert("ssubject_code"+ssubject_code);
//alert("course_name"+course_name);
//alert("exam_name"+exam_name);
jQuery.ajax({
data: 'exam_name='+exam_name+'&course_name=' + course_name,
type: 'POST',
url: 'student_site/subject_records',
success: function(data){
//alert("inside change");
console.log(data);
//alert ("data"+data);
//for(var j = course_name; j < ssubject_code; j++)
//{
jQuery('#ssubject_code').empty().append(data);
//}
}
});
}
</script>
<?php
$attributes=array(
'name'=>'updatecustomer',
'id'=>'updatecustomer'
);
echo form_open('student_site/manage_student',$attributes);
?>
<div id="validation_failed">
<?php
echo validation_errors();
?>
<?php $data = array();
foreach ($course_records as $row)
{
$data[$row->course_code] = $row->course_code;
}
$subject_data = array();
foreach ($all_coursesubject_records as $row)
{
$subject_data[$row->subject_code] = $row->subject_code;
}
?>
<div id="Processy">
<table class="display table table-bordered table-striped" id='studenttable'>
<thead>
<tr font style='font-size:13px'>
<th> </th>
<th> </th>
<th>Register Number</th>
<th>Name </th>
<th>Course Code</th>
<th>Subject Code</th>
</tr></thead>
<?php if(isset($records)) : foreach($records as $row) : ?>
<tr >
<td>
<?php echo anchor('student_site/delete/'.$row->id, 'Delete',array('onClick'=>"return confirm('Are you sure want to delete..?')")); ?>
</td>
<td id="rowUpdate">
<input type=checkbox name="editstudent[]" id="editstudent[]" value="<?php echo $row->id ?>">
</td>
<td ><input class="inputmedium span2" type="text" name="register_number_<?php echo $row->id ?>" id="register_number_<?php echo $row->id ?>" value="<?php echo $row->register_number; ?>" ></td>
<td ><input class="inputmedium span2" type="text" name="name_<?php echo $row->id ?>" id="name_<?php echo $row->id ?>" value="<?php echo $row->name; ?>" ></td>
<td >
<?php
$js = 'class="dropdown_class" id="course_code_id'.$row->id.'" onChange="get_subjectdetails112('.$row->id.')" ';
$js_name = 'course_code_id'.$row->id;
echo form_dropdown($js_name, $data, $row->course_code, $js);
?>
</td>
<input type="hidden" name="index" id="index" value="<?php echo $row->id; ?>"/>
<td>
<?php
$js = 'class="dropdown_class" id="subject_code_id'.$row->id.'"';
$js_name = 'subject_code_id'.$row->id;
echo form_dropdown($js_name, $subject_data, $row->subject_code, $js);
?>
</td>
<?php
$row->id++;
?>
</tr>
<?php endforeach; ?>
</table>
</div>
<center>
<br /><input type="submit" class="btn-success btn" value="Update Student"></center>
<?php else : ?>
<h2>No records were returned.</h2>
<?php endif; ?>
视图:student_update:
<script type="text/javascript" charset="utf-8">
function get_subjectdetails112(index) {
alert ("enter first");
//var index = jQuery('#index').val();
//alert("index"+index);
var course_name = jQuery('#course_code_id'+index).val();
alert("course_name"+course_name);
//var exam_name = jQuery('#course_name_id>option:selected').text();
var exam_name = jQuery('#exam_name_id').val();
var ssubject_code = jQuery('#subject_code_id'+index).val();
alert(ssubject_code);
//var partsArray = exam_name.split('.');
//alert("ssubject_code"+ssubject_code);
//alert("course_name"+course_name);
//alert("exam_name"+exam_name);
jQuery.ajax({
data: 'exam_name='+exam_name+'&course_name=' + course_name,
type: 'POST',
url: 'student_site/subject_records',
success: function(data){
//alert("inside change");
console.log(data);
//alert ("data"+data);
//for(var j = course_name; j < ssubject_code; j++)
//{
jQuery('#subject_code_id'+index).empty().append(data);
//}
}
});
}
</script>