更新codeigniter中的多个复选框

时间:2014-07-21 05:26:38

标签: php forms codeigniter

我是codeigniter的新手。我正在开发学生信息项目。有一个带有多个复选框的表单如下所示:

首先,插入的值放在复选框内。首先检查该值,但是当我选择另一组值时,只更新最后一个值。

我想更新多个复选框。

提前致谢。

我的代码

控制器:delete.php

<?php
class delete extends CI_Controller
{
  function __construct()
    {
            parent::__construct();

            $this->load->helper('url');
            $this->load->helper('form');
            $this->load->helper('html');
            $this->load->model("delete_model");
            $this->load->library("pagination");
            //$this->load->library('table');
    }

   function GetAll()
  {
        $this->load->model('delete_model'); 
        $data['query']=$this->delete_model->GetAll();
        $this->load->view('studentview_view',$data);
  }

   //Get Record By Id
  function getstudbyid($id)
    {
    $this->load->model("delete_model"); 
    $data['student']=$this->delete_model->getstuddetailsbyid($id);
    //echo $this->db->last_query();
    $this->load->view("displayview",$data);
    }


  //pagination

   function test($start_from = 1)
{

    $this->load->model('delete_model'); 
    $this->load->library('pagination');
    // $data['query']=$this->delete_model->GetAll();
    $data = array();

    $per_page = 3;
    $this->load->model('delete_model');

    $total = $this->delete_model->record_count();
     $start = $per_page * ($start_from-1);

    $data['student'] = $this->delete_model->get($per_page, $start);

    $config['base_url'] = base_url() . "index.php/delete/test";
    $config['total_rows'] = $total;
    $config['per_page'] = $per_page;
    $config['uri_segment'] = 2;
    $config['num_links'] = 2;
    $config['use_page_numbers'] = TRUE;
       //$config['overwrite'] = TRUE;


    $this->pagination->initialize($config); 


    $data['pagination'] = $this->pagination->create_links();




        foreach($data['student'] as $key=>$value){
        //echo $key;

        foreach($value as $k=>$v)
        {


         if ( $k == 'id') {

                anchor('delete/del'.$v,'delete');
                anchor('delete/input/'.$v,'Edit');

                }
        }}  
            // $data['table'] = $this->table->generate();
             $this->load->view('studentlist' ,$data);
}


  //Delete Record

  function del($id){

    $this->load->helper('html');    
    $this->load->model('delete_model');

    if((int)$id > 0){
      $this->delete_model->delete($id);

    }

    $data = $this->delete_model->delete();

    $data['student'] = $this->delete_model->delete();

          redirect($_SERVER['HTTP_REFERER']);  
          //$this->load->view('studentlist',$data);    
  }

  //Update Record

  function input($id=0)
  {
    $this->load->model('delete_model');


    if($this->input->post('btnupdate'))
    {
        if($this->input->post('id'))
        {
        $this->delete_model->entry_update();
        echo $this->db->last_query();
        }
    else
        {
        $this->student_model->student();
        }
    }

    $data = $this->delete_model->student_data();


    if((int)$id > 0)
    {
    $query = $this->delete_model->getstuddetailsbyid($id);
    print_r($query);
    //echo $this->db->last_query();
     // $data['fid']['value'] = $query['id'];
     // $data['up_std_name']['value'] =$query['std_name'];
     // $data['up_std_address']['value']=$query['std_address'];
     // $data['stud_class']['value']=$query['std_class'];
     // $data['up_std_language']['value']=$query['std_language'];

     $data['fid'] = $query['id'];
     $data['up_std_name']=$query['std_name'];
     $data['up_std_address']=$query['std_address'];
     $data['stud_class']=$query['std_class'];




     $data['up_std_language']=$query['std_language'];
      $data['up_std_gender'] =$query['std_gender'];
     echo "--------";

        //print_r($data);
        echo "----bhumika----";
        //print_r($query['std_languag']);
    print_r($data['up_std_language']);
        echo "---a---";
        echo "<br/>";
        echo "<br/>";







      // if($query['std_gender']=='F')
      // {
        // $data['up_std_gender']['checked'] = TRUE;

        // }
        // else
        // {

          // $data['up_std_gender']['checked'] = FALSE;  
        // }


    // $data['up_std_birthdate']['value']=$query['std_birthdate'];
     // $data['up_std_photo']['value']=$query['std_photo'];
     // $data['up_std_ilanguage']['value']=$query['std_int_language'];
     // print_r($data['up_std_ilanguage']['value']);

     $data['up_std_birthdate']=$query['std_birthdate'];
     $data['up_std_photo']=$query['std_photo'];
     $data['up_std_ilanguage']=$query['std_int_language'];
    // print_r($data['up_std_ilanguage']);
    }
    $this->load->view('finalview',$data);   
  }


}

  ?>

型号:delete_model.php

<?php
    class delete_model extends CI_Model{

      function stud_delete_model()
      {
        parent::CI_Model();

      }

      //View Record
      public function GetAll()
        {
        $this->load->database();
        $query=$this->db->get('student');
        return $query->result();
        }

      //paging
       public function record_count() {
            $this->load->database();
         $this->db->count_all("student");
        // echo $this->db->last_query();
            return $this->db->count_all("student");
        }


        function get($limit,$start_from)
        {
         $this->load->database();
         $sql = "SELECT * FROM student LIMIT $start_from, $limit";

            $query = $this->db->query($sql);
         //  echo $this->db->last_query();
            return $query->result_array();
        }
          //view Record by id
          public function getstuddetailsbyid($id)
            {
            $this->load->database();
            $query=$this->db->get_where('student',array('id'=>$id));
            //$this->db->last_query();
            return $query->row_array();
            }


        //Delete Record

         function delete($id)
         {
        $this->load->database();
        $this->db->where('id',$id);

        $this->db->delete('student'); 



      }

      //Update Record

      function entry_update()
      {
      $this->load->database();
      $date=$this->input->post('datepicker');



      $data=array(
      'std_name'=>$this->input->post('std_name1'),
      'std_address'=>$this->input->post('std_address1'),
      'std_class'=>$this->input->post('std_class'),
      'std_language'=>$this->input->post('std_language1'),
      'std_gender'=>$this->input->post('std_gender1'),
      'std_birthdate'=>date('Y-m-d', strtotime(str_replace('-', '/', $date))),
      'std_photo'=>$this->input->post('userfile'),
      'std_int_language'=>$this->input->post('std_ilanguage1')
      );
     //$t=$this->input->post('std_language1');
        //      echo "***";
            //  echo $t;
            //print_r($t);

                 // if(isset($this->input->post['std_language1']))
                 // {
                 // $q=implode(',',$this->input->post['std_language1'] );
                 // echo "(((((";
                 // print_r($q);
                //$data['q']=$q;
                //print_r($data);
                // }
                // else
                // {
                // $q='';
                // }
                // echo $q;


       print_r($data);

      $this->db->where('id',$this->input->post('id'));
      $this->db->update('student',$data);
      }


     function student_data()
     {
     //Label of form
    // $data=array();
     //$data['fid']      =array();
    //$data['stud_class']   = '';
     //$data['up_std_name'] = array();
     //$data['up_std_address']=array();

      $data['fid']      ='';

     $data['up_std_name']   = '';
     $data['up_std_address']='';
     $data['stud_class']   = '';
      $data['up_std_language']='';
      $data['up_std_gender']='';
      $data['up_std_birthdate']='';
      $data['up_std_photo']='';
     //echo "ddddddd";
    // print_r($data['up_std_address']);


     //$data['std_name1']="Name";
     //$data['std_address1']="Address";
     //$data['std_class']="Class";
     // $data['up_std_class_list']=array(
                            // 'select' => 'Select',
                            // '10th'  => '10th',
                            // '12th'    => '12th',
                            // 'Graduate'   => 'Graduate'

                            // );

     //$data['std_language1']="Language";
     //$data['std_gender1']="Gender";
     //$data['std_birthdate']="Birthdate";
     //$data['std_photo']="Photo";
     //$dat['std_int_language']="Interested Language";

     //values get from the database
     //$data['fid']['value'] = 0;
     //print_r($data['fid']['value']);
     // $data['up_std_name'] = array(
                  // 'name'        => 'std_name',
                  // 'id'          => 'std_name',
                 // 'maxlength'   => '50',
                  // 'size'        => '30',
                  // 'style'       => 'width:30%'
                // );


    // $data['up_std_address']=array(
                            // 'name'=>'std_address',
                            // 'id'=>'std_address',
                            // 'rows'=>'2',
                            // 'columns'=>'5',
                            // 'width'=>'20',
                            // 'size'=>'50',
                            // 'maxlength'   => '100',
                            // 'style'=>'width:30%'
    // );



    // $data['up_std_language']=
                                // array('name'=>'std_language',
                                    // 'value'=>'.Net',
                                    // 'checked'=>True

                                // );
                                // (array('name'=>'std_language',
                                    // 'value'=>'PHP',
                                    // 'checked'=>false
                                // )
                                // );
                                // (array('name'=>'std_language',
                                    // 'value'=>'JAVA',
                                    // 'checked'=>false
                                // )
                                // );
                                // (array('name'=>'std_language',
                                    // 'value'=>'Android',
                                    // 'checked'=>false
                                // )
                                // );
                                // (array('name'=>'std_language',
                                    // 'value'=>'Iphone',
                                    // 'checked'=>false
                                // )
                                // );
                                //print_r($data['up_std_language']);

    // $data['up_std_gender']=(array
                                // (
                                // "name"=>"std_gender",
                                // "id"=>"std_gender",
                                // "value"=>"M", 
                                // 'checked'=>set_radio('std_gender', 'M', True)
                                // )
                                // );
                                // ?>

                                 <?php 
                                // (array
                                // (
                                // "name"=>"std_gender",
                                // "id"=>"std_gender",
                                // "value"=>"F", 
                                // 'checked'=>set_radio('std_gender', 'F', True)
                                // )
                                // );
                                ?>

    <?php // $data['up_std_birthdate']=''; ?>
                                 <td>

                   </td>




                    <?php       

    //$data['up_std_photo']=''; 






    // $data['up_std_ilanguage']=array(
                      // 'select' => 'Select',
                      // '.Net'  => '.Net',
                      // 'PHP'    => 'PHP',
                      // 'JAVA'   => 'JAVA',
                      // 'Android'=> 'Android',
                      // 'IPhone'   => 'IPhone'
                    // );

            ?>              




                        <?Php   return $data;   

     }







    }
    ?>

查看:finalview.php

<?php
    //echo validation_errors();

    ?>

    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
    <link rel="stylesheet" href="/resources/demos/style.css">
    <script>
    $(function() {
    $( "#datepicker" ).datepicker();
      dateFormat: 'yyyy-mm-dd'
      onclick="javascript: ('datepicker','yyyyMMdd','arrow',true,'12',true)" 
    });
    </script>
    </head>
    <body>
        <? echo form_open('delete/input');
    //print_r($fid);    ?>
        <? echo form_hidden('id',$fid); ?>

    <table>

    <tr>
    <td>
    <label for='std_name'>Student Name</label>
    </td>
    <td>
    <?php
    $data = array(
                  'name'        => 'std_name1',
                  'id'          => 'std_name1',
                  'maxlength'   => '100',
                  'size'        => '50',
                  //'value'     => set_value('std_name1'),
                  'style'       => 'width:50%'
                );

    //echo form_input('std_name1',set_value('std_name1'));
    echo form_input('std_name1',$up_std_name);
    //print_r($up_std_name);
    //echo $std_name1;
    //print_r($std_name1);
    ?></td>
    </tr>

    <tr>
    <td>
    <label for='std_address'>Address</label>
    </td>
    <td>
    <?php
    $data=array(
    'name'=>'std_address1',
    'id'=>'std_address1',
    'rows'=>'3',
    'columns'=>'3',
    'size'=>'50',
     'maxlength'   => '100',
    'style'=>'width:50%'
    );

    echo form_textarea('std_address1',$up_std_address);
    print_r($up_std_address);
    ?>

    </td>
    </tr>

    <tr>
    <td>
    <label for='std_class'>Class</label>
    </td>
    <td>
    <?php
    $options = array(
                    'select' => 'Select',
                      '10th'  => '10th',
                      '12th'    => '12th',
                      'Graduate'   => 'Graduate'

                    );
                    echo form_dropdown('std_class',$options,$stud_class);
                    print_r($stud_class);
                    ?></td>

    <tr>
    <td>
    <label for='std_language'>Language</label>
    </td>
    <td>
    <?php



            // $query['']=array('.Net'=>'.Net','PHP'=>'PHP');
            // print_r($query);
            //echo "----charmi---";
        //  print_r($up_std_language);
        //  print_r($data['up_std_language']['value']);

              //    print_r($language);
             //echo "bbb";
             //echo "<br/>";

            //echo set_checkbox(in_array('std_language', $language) // TRUE checks the box, FALSE does no
            // );
              $language = explode(',',$up_std_language); 







                $a='';
                $b='';
                $c='';
                $d='';
                $e='';



         //print_r($t);
         // if(isset ($this->input->post['std_language1'])) {
                // $role = implode(',', $this->input->post('std_language1'));  
                // print_r($role);
                // } else {
                // $role = '';
                // }


                if(in_array('.Net',$language))
                {
                $a='checked="checked"';
                //echo $a;
                }
                if(in_array('PHP',$language))
                {
                $b='checked="checked"';
                //echo $b;
                }
                if(in_array('JAVA',$language))
                {
                $c='checked="checked"';
                }   
                if(in_array('Android',$language))
                {
                $d='checked="checked"';
                }
                if(in_array('IPhone',$language))
                {
                $e='checked="checked"';
                }
                    // $skill = implode(',', $this->input->post('skill'));
                     //$data['skill'] = $skill;




               echo form_checkbox('std_language1','.Net',$a). ".Net";
              echo form_checkbox('std_language1','PHP',$b)."PHP";
               echo form_checkbox('std_language1','JAVA',$c)."JAVA";
                echo form_checkbox('std_language1','Android',$d)."Android";
                 echo form_checkbox('std_language1','IPhone',$e)."IPhone";

                $t=$this->input->post('std_language1';
                echo "***";
                echo $t;
            //print_r($t);
                 //print_r($language);
                 $v=$language;
                 print_r ($v);
                 if(isset($t))
                 {
                 $q=implode(',',$this->input->post['std_language1'] );
                 echo "(((((";
                 print_r($q);
                //$data['q']=$q;
                //print_r($data);
                 }
                // else
                // {
                // $q='';
                // }
                // echo $q;



              ?>




    </td>
    </tr>

    <tr>
    <td>
    <label for='std_gender'>Gender</label>
    </td>
    <td>
    <?php
    //echo $gender=$this->input->post('std_gender1');
    // if($gender=='male' || empty($gender)) $mchecked=true;
        // else if($gender=='female') $fchecked=true;

        // if($gender=="male")
        // {
        // echo "checked";
        // }

           // echo form_radio('std_gender1', 'male', @$mchecked, 'id=male').form_label('Male', 'male'); 
        // echo  form_radio('std_gender1', 'female', @$fchecked, 'id=female').form_label('Female','female'); 

        //print_r($up_std_gender);
         // $checked = '';          
         // if($checked)
         // {
         // $checked='F';
        //echo $checked;
        //$false='M';
         // }
         // else
         // {
         // $checked='M';
        //$false='F';
         // }
    // echo form_radio('std_gender1', 'M', $checked)."Male";
    // echo form_radio('std_gender1', 'F', $checked)."Female";


        $gender=array(
                'name'=>'std_gender1',
                'id'=>'std_gender1'
                //'checked'=>set_value('std_gender1', 'F', FALSE)
                );
                echo "=======";
        print_r($up_std_gender);
        // $checked = '';   
    $w='M'; 
    $m='F';
          if($up_std_gender)
          {
         $w='checked="checked"';
         }
        else 
    if($up_std_gender)
         {
         $m='checked="checked"';
         }


        //echo $checked;
        // $false='M';
         // }
         // else
         // {
         // $checked='M';
        // $false='F';
         // }
        echo form_radio('std_gender1', 'M',$w)."Male";
        echo form_radio('std_gender1', 'F',$m)."Female";

        // echo form_radio($gender,$up_std_gender,set_value('std_gender1', 'F', TRUE))."Male";
        // echo form_radio($gender,'F',set_value('std_gender1', 'M', TRUE))."Female";
             // echo form_radio(array
                                // (
                                // "name"=>"std_gender1",
                                // "id"=>"std_gender1",
                                // "value"=>"F", 
                                // 'checked'=>set_value('std_gender1', 'F', FALSE)
                                // )
                                // );
    // ?>

    <?php 
            // echo form_radio(array
                                // (
                                // "name"=>"std_gender1",
                                // "id"=>"std_gender1",
                                // "value"=>"M", 
                                // 'checked'=>set_value('std_gender1', 'M', FALSE)
                                // )
                                // );
    ?>
    </td>
    </tr>

    <tr>
    <td valign="top">
    <label for='std_birthdate'>Birthdate</label>
    </td>
    <?php
    $date=array(
        'name'=>'datepicker',
        'id'=>'datepicker'
        );


    ?>
                     <td><?php echo form_input($date,$up_std_birthdate); ?><!--<input   type="text" id="datepicker" name="datepicker"   />--></td>
    </tr>

    <tr>
    <td>
    <label for='std_photo'>Photo</label>
    </td>
    <td>

    <?php //echo $error;?>
    <?php echo form_open_multipart('student/do_upload');

    echo form_upload('userfile',$up_std_photo);
    echo form_label($up_std_photo);

    ?>

    <!--<input type="file" name="userfile" size="20" />-->
    <?php echo form_close();?>

    </td>

    </tr>



    <tr>
    <td>
    <label for='std_ilanguage[]'>Interested Language</label>
    </td>
    <td>


    <!--<select size="" name="ilanguage[]" multiple="multiple">

    <option>.Net</option>

    <option>PHP</option>

    <option>JAVA</option>

    <option>Android</option>

    <option>IPhone</option>

    </select>-->
    <?php
    $options = array(
                    'select' => 'Select',
                      '.Net'  => '.Net',
                      'PHP'    => 'PHP',
                      'JAVA'   => 'JAVA',
                      'Android'=> 'Android',
                      'IPhone'  => 'IPhone'
                    );


        //$select=array('.Net','PHP','JAVA','Android','IPhone');
        echo form_multiselect('std_ilanguage',$options);
                    ?></td>
    </tr>
    <tr>
    <td>

    <? echo form_submit('btnupdate','Update!');  ?>

    </td>
    </tr>


    </table>
    <p><?php echo anchor('delete/test','Go To View!'); ?></p>  
    </form>
    </html>
    <? echo form_close(); ?>

我使用implode()功能,但它没有获得更新的版本,所以给我解决方案

这里,默认值的所有记录都显示在控件中,但是当我检查多个时,它只会更新一个复选框。

再次感谢。

0 个答案:

没有答案