duanduo7400 2015-06-05 06:06
浏览 52

通过使用codeigniter实现分页来显示更新的表

I'm a beginner at this, so I just started pagination. My problem is that I can display pages for viewing just fine but once I update a record the method causes error.Here is the code

controller

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

Model

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

}

view

<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>

other view pages

new

<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>

I just want to display the updated table with pagination.There might be mistakes but please tell me whats wrong.

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • duanqiu2064 2015-06-08 09:07
    关注

    In Controller

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

    in Model

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

    in view(pagination view)

    <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)
            {
                ?>
                <tr>
                    <td><img src="<?php echo base_url().'uploads/'.$row->img; ?>" class="img-responsive" width="100" height="100"></td>
                    <td align="center"><?php echo $row->fname; ?></td>
                    <td align="center"><?php echo $row->lname; ?></td>
                    <td align="center"><?php echo $row->dob; ?></td>
                    <td align="center"><?php echo $row->place; ?></td>
                    <td align="center"><?php echo $row->qual; ?></td>
                    <td align="center"><?php echo $row->gender; ?></td>
                    <td align="center"><?php echo $row->address; ?></td>
                    <td align="center"><?php echo $row->mobile; ?></td>
                    <td align="center"><?php echo $row->email; ?></td>
    
                    <td><a href="<?php echo site_url('regc/update/'.$row->id);?>">Edit</a></td>
                    <td><a href="<?php echo site_url('regc/delete/'.$row->id);?>">Delete</a></td>
                </tr>
    
            <?php
            }
        ?>
    
    </table>
    
    <?php echo form_close() ?>
    <?php
        echo $link ;
    ?>
    </body>
    </html>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?