使用codeigniter

时间:2015-06-05 06:06:38

标签: php codeigniter

我是这个的初学者,所以我刚开始分页。我的问题是,我可以显示页面以便查看,但是一旦我更新了记录,该方法就会导致错误。这是代码

控制器

<?php

class Regc extends CI_Controller 
{

function __construct()
{
    parent::__construct();
    $this->load->helper(array('form', 'url'));

}

function index() 
{
    $this->load->view('regv');
}

function insert()
{
    $a=$this->input->post('fname');
    $b=$this->input->post('lname');    
    $c=$this->input->post('date');    
    $d=$this->input->post('place');    
    $e=$this->input->post('qual');    
    $f=$this->input->post('gender');    
    $g=$this->input->post('text');
    $h=$this->input->post('mobile');    
    $i=$this->input->post('email');    
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png|mp3';
    $config['max_size'] = '10000000';
    $config['max_width'] = '1024';
    $config['max_height'] = '768';

    $this->load->library('upload', $config);


        if (!$this->upload->do_upload())
        {
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('name', $error);
        } 
        else 
        {
        $x = $this->upload->data();
        $image = $x["file_name"];
        $this->load->model("regm");

        $data = array('upload_data' => $this->upload->data());

    $this->load->model('regm');
    $query=$this->regm->insertdata($a,$b,$c,$d,$e,$f,$g,$h,$i,$image); 
    $this->load->view('regv');
        }
}
function select()
    {

        $this->load->model('regm');

        $this->load->library('pagination');
    $config['base_url'] = base_url() . 'index.php/regc/select';
    $config['total_rows'] = $this->db->get_where('register')->num_rows();
    $config['per_page'] = 2;
    $this->pagination->initialize($config);
    $q['link'] = $this->pagination->create_links();
    $this->load->model('regm');
    $off = $this->uri->segment(3, 0);
    $q['query'] = $this->regm->select($config['per_page'], $off);
    $this->load->view('new', $q);

    }
    function update($id)
        {
    $this->load->model('regm');
    $result['query']=$this->regm->update($id);
    $this->load->view('regedit',$result);
        }
    function edit($id)
        {

$fname=$this->input->post('fname');
$lname=$this->input->post('lname');
$date=$this->input->post('date');
$place=$this->input->post('place');
$qual=$this->input->post('qual');
$gender=$this->input->post('gender');
$text=$this->input->post('text'); 
$mobile=$this->input->post('mobile');
$email=$this->input->post('email');

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|mp3';
$config['max_size'] = '10000000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
    $this->load->library('upload', $config);
    if (!$this->upload->do_upload())
        {
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('name', $error);
        } 
        else 
        {
        $x = $this->upload->data();
        $image = $x["file_name"];
        $this->load->model("regm");

        $data = array('upload_data' => $this->upload->data());


        $this->load->model('regm');



        $xt['qry']=$this->regm->edit($id,$fname,$lname,$date,$place,$qual,$gender,$text,$mobile,$email,$image);

        $this->load->library('pagination');
        $config['base_url'] = base_url() .'index.php/regc/select';
        $config['total_rows'] = $this->db->get_where('register')->num_rows();
        $config['per_page'] = 2;
        $this->pagination->initialize($config);
        $xt['link'] = $this->pagination->create_links();
        $this->load->model('regm');
        $off = $this->uri->segment(3, 0);
        $xt['query'] = $this->regm->select($config['per_page'], $off);


        $this->load->view('new1',$xt);


        }

        }
    function delete($id)
   {
     $this->load->model('regm');
     $this->regm->row_delete($id);
     redirect($_SERVER['HTTP_REFERER']); 
   }
  } 

模型

<?php
class Regm extends CI_Model 
 {
function __construct()
{
    parent::__construct();
   $this->load->database();
}
function insertdata($a,$b,$c,$d,$e,$f,$g,$h,$i,$image)

{
$this->db->set('fname',$a); 
$this->db->set('lname',$b); 
$this->db->set('dob',$c); 
$this->db->set('place',$d); 
$this->db->set('qual',$e); 
$this->db->set('gender',$f); 
$this->db->set('address ',$g); 
$this->db->set('mobile',$h); 
$this->db->set('email',$i); 
$this->db->set('img',$image);
$this->db->insert('register');
}
 function select($c,$r)
{
    $this->db->select('*');
    $query= $this->db->from('register');
    $query= $this->db->limit($c,$r)->get();
    return  $query->result();
}
function update($id)
{
    $this->db->select('*');
    $this->db->where('id',$id);
    $query=$this->db->get('register');
    return $query->result();
    }
function   edit($id,$fname,$lname,$date,$place,$qual,$gender,$text,$mobile,$email,$image)

   {

$this->db->where('id',$id);

$this->db->set('fname',$fname); 
$this->db->set('lname',$lname); 
$this->db->set('dob',$date); 
$this->db->set('place',$place); 
$this->db->set('qual',$qual); 
$this->db->set('gender',$gender); 
$this->db->set('address ',$text); 
$this->db->set('mobile',$mobile); 
$this->db->set('email',$email); 
$this->db->set('img',$image);
$this->db->update('register');
$qry=$this->db->get('register');

return $qry->result();
}
function row_delete($id)
    {
    $this->db->where('id', $id);
    $this->db->delete('register'); 
    }

}

查看

<html>
<body>
    <?php 
    echo form_open_multipart('regc/insert');
    ?>
    <table> 
        <tr>
            <td>Image</td>



                <input type="file" name="userfile" size="20" />
        </tr>
        <tr>
            <td>Firstname:</td>
            <td><input type="text" name="fname" value=""></td>
        </tr>
        <tr>
            <td>Lastname:</td>
            <td><input type="text" name="lname" value=""></td>
        </tr>
        <tr>
        <td>DOB:</td>
        <td><input type="date" name="date" value=""></td>
        </tr>
        <tr>
        <td>place:</td>
        <td><input type="text" name="place" value=""></td>
        </tr>
        <tr>
        <td>qualification</td>
                    <td><select name="qual">
                            <option value="btech">btech</option>
                            <option value="mtech">mtech</option>
                        </select>
             </tr>
              <tr>
                    <td>gender</td>
                    <td><input type="radio" name="gender" value="male"/>male 
                        <input type="radio" name="gender" value="female"/>female 
                    </td>  
                </tr>
                <tr>
                    <td>address:</td>
                    <td><textarea  name="text" rows="10" cols="10"/></textarea>
                </tr>
                <tr>
                    <td>mobile:</td>
                    <td><input type="tel" name="mobile" value=""/></td>   
                </tr>
                <tr> 
                    <td>email:</td>
                    <td><input type="email" name="email" value=""/></td>
                </tr>


    </form>  
        </table>
                    <input type="submit" name="submit" value="register"/></td>

    <?php echo form_close();?>
    <?php echo form_open('regc/select');?>
     <input type="submit" name="view" value="view"/>

    <?php echo form_close();?>

</body>

其他视图页

<html>
<body>
    <?php echo form_open();?>
    <table border="2">
        <tr>
            <th>Image</th>
            <th>FirstName</th>
            <th>LastName</th>
            <th>DOB</th>
            <th>Place</th>
            <th>Qualification</th>
            <th>Gender</th>
            <th>Address</th>
            <th>Mobile</th>
            <th>Email</th>
            <th>Edit</th>
            <th>Delete</th>
        </tr>  
        <?php
        foreach($query as $row)
        {
            $id=$row->id;
            $image=$row->img;
            $fname=$row->fname;
             $lname=$row->lname;
              $date=$row->dob;
               $place=$row->place;
                $qual=$row->qual;
                 $gen=$row->gender;
                  $addr=$row->address;
                   $mob=$row->mobile;
            $em=$row->email;
            ?><tr>
                <td><img src="<?php echo base_url().'uploads/'.$row->img ?>" class="img-responsive" width="100" height="100"></td>
                <td align="center"><?php echo $fname; ?></td>
                 <td align="center"><?php echo $lname; ?></td>
                  <td align="center"><?php echo $date; ?></td>
                   <td align="center"><?php echo $place; ?></td>
                    <td align="center"><?php echo $qual; ?></td>
                    <td align="center"><?php echo $gen; ?></td>
                     <td align="center"><?php echo $addr; ?></td> 
                      <td align="center"><?php echo $mob; ?></td>
                       <td align="center"><?php echo $em; ?></td>

                <td><a href="<?php echo site_url('regc/update/'.$id);?>">Edit</a></td>
                <td><a href="<?php echo site_url('regc/delete/'.$id);?>">Delete</a></td>
            </tr>


        <?php }
     ?>   

    </table>

    <?php echo form_close() ?>
    <?php
    echo $link ;
  ?>
</body>

名new1

<html>
<body>
    <?php echo form_open_multipart();?>
    <table border="1">
        <tr>
            <th>Image</th>
            <th>FirstName</th>
            <th>LastName</th>
            <th>DOB</th>
            <th>Place</th>
            <th>Qualification</th>
            <th>Gender</th>
            <th>Address</th>
            <th>Mobile</th>
            <th>Email</th>
            <th>Edit</th>
            <th>Delete</th>
        </tr>  
        <?php
        foreach($qry as $row)
        {
            $id=$row->id;
            $image=$row->img;
            $fname=$row->fname;
             $lname=$row->lname;
              $date=$row->dob;
               $place=$row->place;
                $qual=$row->qual;
                 $gen=$row->gender;
                  $addr=$row->address;
                   $mob=$row->mobile;
            $em=$row->email;
            ?><tr>
                <td><img src="<?php echo base_url().'uploads/'.$row->img ?>" class="img-responsive" width="100" height="100"></td>
                <td><?php echo $fname; ?></td>
                <td><?php echo $lname; ?></td>
                 <td><?php echo $date; ?></td>
                  <td><?php echo $place; ?></td>
                   <td><?php echo $qual; ?></td>
                    <td><?php echo $gen; ?></td>
                     <td><?php echo $addr; ?></td> 
                     <td><?php echo $mob; ?></td>
                      <td><?php echo $em; ?></td>

                <td><a href="<?php echo site_url('regc/update/'.$id);?>">Edit</a></td>
                <td><a href="<?php echo site_url('regc/delete/'.$id);?>">Delete</a></td>
            </tr>


        <?php }
     ?>   

    </table>

    <?php echo form_close() ?>

</body>

我只想用分页显示更新的表。可能有错误,但请告诉我什么是错的。

提前致谢!

1 个答案:

答案 0 :(得分:0)

在控制器

function kk_youtube_handler($post) {
    $value = get_post_custom($post->ID);

    $youtube_link = esc_attr($value['kk_youtube'][0]);

    echo '<label for="kk_youtube">YouTube Video Link</label>
          <input type="text" id="kk_youtube" name="kk_youtube" value="' . 
          $youtube_link . '" />';
}
模型

中的

<?php
    $this->load->model('regm');
    $this->pagination->initialize($config);

    $count= $this->db->get_where('register')->num_rows();//Count

    $this->load->library('pagination');

    $config['base_url'] = base_url() .'index.php/regc/select';
    $config['total_rows'] = $count;
    $config['per_page'] = 2;
    $config['uri_segment'] = 3;
    $limit = $config['per_page']; //define how many items to show   


    $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;

    $data['links'] = $this->pagination->create_links();
    $data['query'] = $this->regm->select($limit, $data);

视图(分页视图)

function select($page , $limit)
{
    $query = $this->db->query("SELECT * FROM register LIMIT $page, $limit");
    $result = $query->result_array();
    return $result;
}