我怎么知道以表格形式传递它的值或数组

Posted

技术标签:

【中文标题】我怎么知道以表格形式传递它的值或数组【英文标题】:How can i know the value or array which pass it in the form 【发布时间】:2014-05-27 15:28:48 【问题描述】:

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

这是我的控制器:

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

主类扩展 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?>" > <?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();
  

【问题讨论】:

【参考方案1】:

如何也发送数据类型。

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

然后在您的视图中,您可以将table_type 变量放在提交按钮

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

隐藏字段

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

然后在您收到 POST 数据的控制器上,您可以通过以下方式对其进行验证:

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 值的检查更加动态。

【讨论】:

以上是关于我怎么知道以表格形式传递它的值或数组的主要内容,如果未能解决你的问题,请参考以下文章

查找和引用函数INDEX

unity 传递数组是值传递还是引用

index函数

如何检查数组中的声明值或 Ocelot 网关中的任何值?

EXCEL中INDEX怎么使用

c ++按值或指向函数语法的指针传递数组