gpt4 book ai didi

php - 我如何知道以表单传递的值或数组

转载 作者:行者123 更新时间:2023-11-30 00:13:00 25 4
gpt4 key购买 nike

我遇到了这个问题,但我不知道如何解决。我有一个与数据库交互的项目,我制作了一个用于交互的 CRUD html 表,这意味着我在相关数据库中有 15 个表,我想要插入、删除和编辑记录,所以这是我的代码。我的问题是:我如何知道何时将这些值插入到该表中,以将其插入到数据库中的同一个表中,我真的不知道。

这是我的 Controller :

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

类 Main 扩展 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' => 'lab_name',
'LabSize' => 'lab_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);
}
}

这是我的观点:

<?php echo form_open('main') ?>

<table id="dg" title="Andro-CH Table" class="easyui-datagrid" style="width:700px;height:250px"
url="get_users.php"
toolbar="#toolbar" pagination="true"
rownumbers="true" fitColumns="true" singleSelect="true">
<thead>
<tr>
<?php foreach ($fields as $field_name => $field_display): ?>
<th field="<?php echo $field_name?>" width="50"> <?php echo $field_display; ?> </th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?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 id="toolbar">
<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">New User</a>
<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">Edit User</a>
<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">Remove User</a>
</div>

<div id="dlg" class="easyui-dialog" style="width:400px;height:280px;padding:10px 20px" closed="true" buttons="#dlg-buttons">
<div class="ftitle">Table Information</div>
<div class="fitem">
<?php foreach ($fields as $field_name => $field_display): ?>
<?php echo form_label("$field_display:"); ?>
<?php echo form_input("$field_name", '', 'class="easyui-validatebox" required="true"'); ?>
<br>
<?php endforeach; ?>
</div>
</div>
<div id="dlg-buttons">
<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveUser()">Save</a>
<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">Cancel</a>
</div>
<script type="text/javascript">
var url;
function newUser(){
$('#dlg').dialog('open').dialog('setTitle','New User');
$('#fm').form('clear');
url = 'save_user.php';
}
function editUser(){
var row = $('#dg').datagrid('getSelected');
if (row){
$('#dlg').dialog('open').dialog('setTitle','Edit User');
$('#fm').form('load',row);
url = 'update_user.php?id='+row.id;
}
}
function saveUser(){
$('#fm').form('submit',{
url: url,
onSubmit: function(){
return $(this).form('validate');
},
success: function(result){
var result = eval('('+result+')');
if (result.errorMsg){
$.messager.show({
title: 'Error',
msg: result.errorMsg
});
} else {
$('#dlg').dialog('close'); // close the dialog
$('#dg').datagrid('reload'); // reload the user data
}
}
});
}
function destroyUser(){
var row = $('#dg').datagrid('getSelected');
if (row){
$.messager.confirm('Confirm','Are you sure you want to destroy this user?',function(r){
if (r){
$.post('destroy_user.php',{id:row.id},function(result){
if (result.success){
$('#dg').datagrid('reload'); // reload the user data
} else {
$.messager.show({ // show error message
title: 'Error',
msg: result.errorMsg
});
}
},'json');
}
});
}
}
</script>
<style type="text/css">
#fm{
margin:0;
padding:10px 30px;
}
.ftitle{
font-size:14px;
font-weight:bold;
padding:5px 0;
margin-bottom:10px;
border-bottom:1px solid #ccc;
}
.fitem{
margin-bottom:5px;
}
.fitem label{
display:inline-block;
width:80px;
}
</style>

这是我的模型,尚未完成

<?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();
}
}

最佳答案

是否也发送数据类型?

...
case 'case1':
$data['table'] = $result..
$data['table_type'] = 'case1';
break;
case 'case2':
$data['table'] = $result..
$data['table_type'] = 'case2';
break;
...

然后,您可以在 View 中将 table_type 变量放在提交按钮上:

<input type="submit" name="<?=$table_type;?>" value="update">

隐藏区域

<input type="hidden" name="<?=$table_type;?>" value="TRUE">

然后在您接收POST数据的 Controller 上,您可以通过以下方式验证它:

if($this->input->post('case1'))
{
//for case 1
//insert/update to case1 table
}elseif($this->input->post('case2'){
//for case 2
//insert/update to case2 table
}else{
//default
}

这只是一个简单的,您可以添加更多安全性或使 POST 值的检查更加动态。

关于php - 我如何知道以表单传递的值或数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23893386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com