codeigniter

时间:2015-05-30 08:07:21

标签: php codeigniter

我在使用codeigniter框架开发的应用程序视图中有以下代码

<?php foreach($query->result_array() as $row){ ?>
        <tr>
        <?php echo form_open('MainController/AttendanceSet');?>
            <input type="hidden" name="child_nric" value="<?php echo $row['child_nric']; ?>"/>
            <input type="hidden" name="teachernric" value="<?php echo $_SESSION['username']; ?>"/>
            <input type="hidden" name="progcode" value="<?php echo $program; ?>"/>


            <td><?php echo $row['child_nric']; ?></td>
            <td>    
                <select name="attendance">                  
                    <option value="1" selected="<?php if($selected==1) echo "selected"; ?>">Present</option>
                    <option value="0" selected="<?php if($selected==0) echo "selected"; ?>">Absent</option>
                </select>

            </td>
            <td> <input type="submit" name="submit" value="Submit"></form></td>
        </tr>

        <?php } ?>  

根据以下答案对代码进行了编辑,现在就像这样

<?php foreach($query->result_array() as $row){ ?>
        <tr>
        <?php echo form_open('MainController/AttendanceSet');?>
            <input type="hidden" name="child_nric" value="<?php echo $row['child_nric']; ?>"/>
            <input type="hidden" name="teachernric" value="<?php echo $_SESSION['username']; ?>"/>
            <input type="hidden" name="progcode" value="<?php echo $program; ?>"/>


            <td><?php echo $row['child_nric']; ?></td>
            <td>    
                <select name="attendance">                  
                    <option value="1" selected="<?php if($selected==1) echo "selected"; ?>">Present</option>
                    <option value="0" selected="<?php if($selected==0) echo "selected"; ?>">Absent</option>
                </select>

            </td>
            <td> <input type="submit" name="submit" value="Submit"></td>
        </tr>

        <?php echo form_close();}   ?>  

但输出仍然相同

但是当加载视图时,代码会像这样出现

<tr>
        <form action="http://sms-dev.anovatesoft.com/index.php/MainController/AttendanceSet" method="post" accept-charset="utf-8"></form>
            <input type="hidden" name="child_nric" value="A12">
            <input type="hidden" name="teachernric" value="T001">
            <input type="hidden" name="progcode" value="FEE001">


            <td>A12</td>
            <td>    
                <select name="attendance">                  
                    <option value="1" selected="">Present</option>
                    <option value="0" selected="">Absent</option>
                </select>

            </td>
            <td> <input type="submit" name="submit" value="Submit"></td>
        </tr>

注意</from>位置的差异?在我的代码中,我在提交按钮之后得到它,但当它呈现它时,在<form>打开后关闭,使表单无用,因为我无法提交它。 这种奇怪行为背后的原因是什么?我该如何解决?

视图

<?php
$this->load->helper('form');
?>

<script>
    function ListView(){
        //if($("#id").val() != "0"){
        var formURL = "<?php echo base_url();?>MainController/Attendance/"+$("#program").val();
        $.post(formURL).done(function(data){$("#body_view_paste").html(data); });

    }

    function fire(){
        alert("I have been fired");
    }
</script>

<div align="center">
    <table align="center"  style="max-width:80%">
    <tr>
            <td>Program</td>
            <td>
               <select id="program" name="program" onchange="ListView()">
           <option value="0">Select</option>            
                <?php 
                $nricno = $_SESSION['username'];
                $sql = "SELECT distinct programs.activities, programs.progcode FROM events
        LEFT JOIN programs ON programs.progcode=events.progcode WHERE events.teacher_nric='$nricno'";

        $activities = $this->db->query($sql);
        foreach($activities->result_array() as $row){?>

                            <option value="<?php echo $row['progcode'];?>"><?php echo $row['activities'];?></option>  
                   <?php } ?>
            </select>

            </td>
         </tr>
      </table>
 </div>
 <div>
 <?php if(isset($program)){ 
 $selected=3;
    $query = $this->db->query("SELECT child_nric FROM child_reg_prog WHERE progcode = '$program'");
                ?>
    <table>
        <thead>
            <tr>
                <td>NRIC NO</td>
                <td>Attendance</td>     
        <td>Action</td>
            </tr>
    </thead>

        <?php foreach($query->result_array() as $row){ ?>
        <tr>
        <?php echo form_open('MainController/AttendanceSet');?>
            <input type="hidden" name="child_nric" value="<?php echo $row['child_nric']; ?>"/>
            <input type="hidden" name="teachernric" value="<?php echo $_SESSION['username']; ?>"/>
            <input type="hidden" name="progcode" value="<?php echo $program; ?>"/>


            <td><?php echo $row['child_nric']; ?></td>
            <td>    
                <select name="attendance">                  
                    <option value="1" selected="<?php if($selected==1) echo "selected"; ?>">Present</option>
                    <option value="0" selected="<?php if($selected==0) echo "selected"; ?>">Absent</option>
                </select>

            </td>
            <td> <input type="submit" name="submit" value="Submit"></td>
        </tr>

        <?php echo form_close();}   ?>      

    </table>    
    <?php } ?>
 </div>

控制器部分

public function Attendance($program = "")
    {
        $this->main_model->pagePermissions("Attendance");
        if ($program == "") {
            $this->load->view('Attendance');
        } else {
            $data['program'] = $program;
            $this->load->view('Attendance', $data);
        }
    }
    public function AttendanceSet()
    {
        $child_nric = $_POST['child_nric'];
        $attendance = $_POST['attendance'];
        $teachernric = $_POST['teachernric'];
        $progcode = $_POST['progcode'];
        $ispresent="No";
        if($attendance==1)
        {
            $ispresent="Yes";
        }
        $this->load->model("Attendance_model");
        $this->Attendance_model->insert($progcode, $childnric, $teachernric, $ispresent);       

        $this->load->view('Attendance');
    }

模型部分

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


class Attendance_model extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    function insert($progcode, $childnric, $teachernric, $ispresent)
    {
        $sql = "INSERT INTO attendence (progcode, childnric, teachernric, ispresent ) VALUES (?,?,?,?) "; 
        $query = $this->db->query($sql, array($progcode, $childnric, $teachernric, $ispresent));        
    }


}
?>

2 个答案:

答案 0 :(得分:1)

试试这个:

<?php foreach($query->result_array() as $row){ ?>
    <?php echo form_open('MainController/AttendanceSet');?>
    <tr>
        <input type="hidden" name="child_nric" value="<?php echo $row['child_nric']; ?>"/>
        <input type="hidden" name="teachernric" value="<?php echo $_SESSION['username']; ?>"/>
        <input type="hidden" name="progcode" value="<?php echo $program; ?>"/>


        <td><?php echo $row['child_nric']; ?></td>
        <td>    
            <select name="attendance">                  
                <option value="1" selected="<?php if($selected==1) echo "selected"; ?>">Present</option>
                <option value="0" selected="<?php if($selected==0) echo "selected"; ?>">Absent</option>
            </select>

        </td>
        <td> <input type="submit" name="submit" value="Submit"></td>
    </tr>
    <?php echo form_close(); ?>
    <?php } ?>

答案 1 :(得分:0)

以下两个陈述都应该超出foreach循环

<?php echo form_open('MainController/AttendanceSet');?>
 <?php echo form_close(); ?>// you need to close it