我有以下代码,我不知道什么是错的。有人可以帮帮我吗?我认为错误在数组中,但我不确切地知道错误是什么。
这是我使用Codeigniter框架的完整代码。
我想从html表中存在的文本框中获取值并添加 它到数据库表,我想使用ajax提交值 数据库中。
如果有任何想法jquery代码比我的更好,请告诉我并告诉我应该在insert_table函数中写什么。
注意:我在这个主题上的经验是零,这是我的第一个项目。
这是我的控制器代码:
<?php
class Main extends CI_Controller {
function index($some)
{
$this->load->model('main_model');
switch ($some) {
case 'class':
$data['fields'] = array(
'ClassID' => 'class id',
'ClassNum' => 'class name',
'ClassSize' => 'class size',
'Floor' => 'floor'
);
$data['tables'] = $this->main_model->getClass();
break;
case 'group':
$data['fields'] = array(
'GroupID' => 'id',
'GroupNum' => 'group name',
'SectionNum' => 'section',
'YearNum' => 'year',
'ProfNameAra' => 'proffetion name'
);
$data['tables'] = $this->main_model->getGroupSection();
break;
case 'hole':
$data['fields'] = array(
'HoleID' => 'id',
'HoleNum' => 'hole name',
'HoleSize' => 'hole size',
'Floor' => 'floor'
);
$data['tables'] = $this->main_model->getHole();
break;
case 'lab':
$data['fields'] = array(
'LabID' => 'id',
'LabName' => 'hole name',
'LabSize' => 'hole size',
'Floor' => 'floor'
);
$data['tables'] = $this->main_model->getLab();
break;
case 'lecture':
$data['fields'] = array(
'LectureID' => 'id',
'SubjectAra' => 'subject name',
'Is_Lecture' => 'yes/no',
'YearNum' => 'year',
'ProfNameAra' => 'proffission',
'SubjectSemester' => 'semester'
);
$data['tables'] = $this->main_model->getLectureSubject();
break;
case 'prof':
$data['fields'] = array(
'ProfID' => 'id',
'ProfNameAra' => 'prof name'
);
$data['tables'] = $this->main_model->getProf();
break;
case 'section':
$data['fields'] = array(
'SectionID' => 'id',
'SectionNum' => 'section name',
'YearNum' => 'year',
'ProfNameAra' => 'proffission'
);
$data['tables'] = $this->main_model->getSection();
break;
case 'subgroup':
$data['fields'] = array(
'SubgroupID' => 'id',
'SubgroupNum' => 'section name',
'GroupNum' => 'group name',
'SectionNum' => 'section name'
);
$data['tables'] = $this->main_model->getSubgroup();
break;
case 'subject':
$data['fields'] = array(
'SubjectID' => 'id',
'SubjectAra' => 'subject name',
'Is_Lecture' => ' yes/no',
'YearNum' => 'year',
'ProfNameAra' => 'proffission'
);
$data['tables'] = $this->main_model->getSubject();
break;
case 'teacher':
$data['fields'] = array(
'TeacherID' => 'id',
'TeacherName' => 'teacher name'
);
$data['tables'] = $this->main_model->getTeacher();
break;
case 'timetable':
$data['fields'] = array(
'TimetableID' => 'id',
'YearDate' => 'date',
'Semester' => 'semester',
'YearNum' => 'year',
'ProfNameAra' => 'proffission',
'SectionNum' => 'sectin name'
);
$data['tables'] = $this->main_model->getTimetable();
break;
case 'timetabledetail':
$data['fields'] = array(
'DetailID' => 'id',
'Day' => 'day',
'Period' => 'period',
'YearDate' => 'year date',
'Semester' => 'semester',
'YearNum' => 'year',
'ProfNameAra' => 'proffission',
'SectionNum' => 'section name',
'GroupNum' => 'group name',
'SubgroupNum' => 'subgroup name',
'LabName' => 'lab name',
'HoleNum' => 'hole name',
'ClassNum' => 'class name',
'TeacherName' => 'teacher name',
'SubjectAra' => 'subject name'
);
$data['tables'] = $this->main_model->getTimetabledetail();
break;
case 'year':
$data['fields'] = array(
'YearID' => 'id',
'YearNum' => 'year'
);
$data['tables'] = $this->main_model->getYear();
break;
case 'year_prof':
$data['fields'] = array(
'YearNum' => 'year',
'ProfNameAra' => 'proffission'
);
$data['tables'] = $this->main_model->getYearProf();
break;
}
$data['main_content'] = 'main';
$this->load->view('includes/template', $data);
}
function insert_table()
{
// What i should write here i don't know
}
}
这是我的模特:
<?php
class Main_model extends CI_Model {
function getClass() {
$query = $this->db->select('*')
->from('class')
->get();
return $query->result();
}
function getGroup() {
$query = $this->db->select('*')
->from('group')
->get();
return $query->result();
}
function getGroupSection() {
$q = $this->db->select('group.GroupID, group.GroupNum, section.SectionNum,year.YearNum,prof.ProfNameAra')
->from('group')
->join('section', 'group.Section_id = section.SectionID')
->join('year', 'year.YearID = section.Year_id')
->join('prof', 'section.Prof_id = prof.ProfID');
return $q->get()->result();
}
function getHole() {
$query = $this->db->select('*')
->from('hole')
->get();
return $query->result();
}
function getLab() {
$query = $this->db->select('*')
->from('lab')
->get();
return $query->result();
}
function getLecture() {
$query = $this->db->select()
->from('lecture')
->get();
return $query->result();
}
function getLectureSubject() {
$q = $this->db->select('lecture.LectureID, subject.SubjectAra, subject.Is_Lecture, year.YearNum, prof.ProfNameAra, subject.SubjectSemester')
->from('lecture')
->join('subject', 'lecture.Subject_id = subject.SubjectID')
->join('prof', 'prof.ProfID = subject.Prof_id')
->join('year','year.YearID = subject.Year_id');
return $q->get()->result();
}
function getProf() {
$query = $this->db->select('*')
->from('prof')
->get();
return $query->result();
}
function getSection() {
$query = $this->db->select('section.SectionID, section.SectionNum, year.YearNum, prof.ProfNameAra')
->from('section')
->join('year', 'section.Year_id = year.YearID')
->join('prof', 'section.Prof_id = prof.ProfID')
->get();
return $query->result();
}
function getSubgroup() {
$query = $this->db->select('subgroup.SubgroupID, subgroup.SubgroupNum, group.GroupNum, section.SectionNum')
->from('subgroup')
->join('group', 'subgroup.Group_id = group.GroupID')
->join('section', 'group.Section_id = section.SectionID')
->get();
return $query->result();
}
function getSubject() {
$query = $this->db->select('subject.SubjectID,subject.SubjectAra,subject.SubjectSemester,subject.Is_Lecture,
year.YearNum,prof.ProfNameAra')
->from('subject')
->join('year', 'year.YearID = subject.Year_id')
->join('prof', 'prof.ProfID = subject.Prof_id')
->get();
return $query->result();
}
function getTeacher() {
$query = $this->db->select('*')
->from('teacher')
->get();
return $query->result();
}
function getTimetableDetail() {
$query = $this->db->select('DetailID,Day,Period,
timetable.TimetableID,timetable.YearDate,timetable.Semester,
year.YearNum,
prof.ProfNameAra,
section.SectionNum,
group.GroupNum,
subgroup.SubgroupNum,
lab.LabName,
hole.HoleNum,
class.ClassNum,
teacher.TeacherName,
subject.SubjectAra')
->from('timetabledetail')
->join('timetable','Timetable_id = TimetableID')
->join('year', 'timetable.Year_id = year.YearID')
->join('prof', 'prof.ProfID = timetable.Prof_id')
->join('section', 'section.SectionID = timetable.Section_id')
->join('group', 'GroupID = Group_id')
->join('subgroup','SubgroupID = Subgroup_id')
->join('lab','LabID = Lab_id')
->join('hole', 'HoleID =Hole_id')
->join('class','ClassID = Class_id')
->join('teacher','TeacherID = Teacher_id')
->join('subject','SubjectID = Subject_id')
->get();
return $query->result();
}
function getTimetable() {
$query = $this->db->select('timetable.TimetableID,timetable.YearDate,timetable.Semester,
year.YearNum,
prof.ProfNameAra,
section.SectionNum')
->from('timetable')
->join('year', 'timetable.Year_id = year.YearID')
->join('prof', 'prof.ProfID = timetable.Prof_id')
->join('section', 'section.SectionID = timetable.Section_id')
->get();
return $query->result();
}
function getYear() {
$query = $this->db->select('*')
->from('year')
->get();
return $query->result();
}
function getYearProf()
{
$q = $this->db->select('year.YearNum, prof.ProfNameAra')
->from('year_prof')
->join('year', 'year_prof.Year_id = year.YearID')
->join('prof', 'year_prof.Prof_id = prof.ProfID')
->get();
return $q->result();
}
}
这是我的观看代码:
<?php echo form_open('main') ?>
<script type="text/javascript" src="<?php echo base_url(); ?>js/jquery.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>js/jquery.easing- sooper.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>js/jquery.sooperfish.js"> </script>
<script>
$(document).ready(function() {
$('#add').click(function() {
var str = '#';
var arrayFromPHP = <?php echo json_encode($fields); ?>
var array = new Array();
$.each(arrayFromPHP, function(key) {
var temp = str.concat(key);
var temp1 = $(temp).val();
var list = [key, temp1];
array.push(list);
});
$.ajax({
url: "<?php echo site_url('main/insert_table'); ?>",
type: 'POST',
data: array,
success: function(msg){
alert('You insert data into database !!!');
}
});
});
});
</script>
<div id="main">
<div id="header_container">
<div class="container">
<!--Header-->
<div class="4u">
<h2>Andro-CH server page</h2>
</div>
<nav id="main-nav" class="8u">
<ul>
<li><a href="class">Class</a></li>
<li><a href="group">Group</a></li>
<li><a href="hole">Hole</a></li>
<li><a href="lab">Lab</a></li>
<li><a href="prof">Prof</a></li>
<li><a href="section">Section</a></li>
<li><a href="subgroup">Subgroup</a></li>
<li><a href="subject">Subject</a></li>
<li><a href="teacher">Teacher</a></li>
<li><a href="timetable">Timetable</a></li>
<li><a href="timetabledetail">Timetable Detail</a></li>
<li><a href="year">Year</a></li>
</ul>
</nav>
</div>
</div>
<table>
<thead>
<?php foreach ($fields as $field_name => $field_display): ?>
<th>
<?php echo $field_display; ?>
</th>
<?php endforeach; ?>
</thead>
<tbody>
<tr>
<?php foreach ($fields as $field_name => $field_display): ?>
<td>
<?php echo form_input($field_display, '', "id = $field_name"); ?>
</td>
<?php endforeach; ?>
</tr>
<?php foreach ($tables as $table): ?>
<tr>
<?php foreach ($fields as $field_name => $field_display): ?>
<td>
<?php echo $table->$field_name; ?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<div>
<?php echo form_button('add', 'Add', 'id = "add"'); ?>
</div>
<?php echo form_close(); ?>
答案 0 :(得分:0)
您正在尝试访问myArray数组中的不存在的项目。要向数组中添加新项,需要使用.push()函数,或者如果要使用特定键,则必须使用Hash。
$(document).ready(function() {
$('#add').click(function() {
var arrayFromPHP = <?php echo json_encode($fields); ?>
var myArray = [];
var str = "#";
//var i = 0; // this is not needed since push adds everytime to the end of array
$.each(arrayFromPHP, function(key) {
var temp = str.concat(key);
var temp1 = $(temp).val();
myArray.push(temp1);
//i = i + 1; // this is not needed then
alert(temp1);
});
});
});