Codeigniter分页不添加新页面

时间:2017-11-17 11:08:09

标签: php codeigniter pagination codeigniter-3

我尝试使用codeigniter分页库,但它对我不起作用。我试图在所有产品页面上有24个产品,当有更多产品时,它应该添加到下一页的链接。所以现在我在网站上有超过24种产品,没有添加新页面。

通过这一行,我可以从产品表中获取所有产品:

$data['products'] = $this->Product_model->selectProducts();

这是我的控制器功能:

  public function pagination() {
        //load pagination library
        $this->load->library('pagination');
        //laad Products_model voor pagination
        $this->load->model('Products_model');
        $this->load->model('Product_model');

        $config = array();
        $config["base_url"] = base_url() . "AlleCadeausController/pagination";
        $config["total_rows"] = $this->products->record_count();
        $config["per_page"] = 24;
        $config["uri_segment"] = 3;

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

        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data['products'] = $this->Product_model->selectProducts();
        $data["links"] = $this->pagination->create_links();

        $this->load->view("allecadeaus", $data);
    }

我的模特(Products_model):

<?php
class Products extends CI_Model
{

    public function __construct() {
        parent::__construct();
    }

    public function record_count() {
        return $this->db->count_all("products");
    }

    public function fetch_products() {
        $query = $this->db->get("products");

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
   }
}

?>

这是我的视图文件(我不知道是否需要在视图文件中添加任何内容才能使分页生效):

<?php   include_once ('templates/header.php');  ?>

<!-- Alle cadeaus gele title bovenaan pagina -->
<div class="all-content">
    <div class="row">
        <div class="col-lg-12 bg-warning header-text" style="font-size:25px;font-weight: bold;">
            <center>Alle cadeaus</center>
        </div>
        </div>


 <hr />

<br>

<!-- Cadeau categorie side menu -->

    <div class="row">
            <div class="menu">
                <div id="categorymenu">
                    <center>  <h3>Categorieën</h3> </center>


 <div class="categorieen">
      <?php foreach (get_categories_h() as $category) : ?>
  <input id="box1" type="checkbox" />
  <label for="box1"><a class="listcat" href="<?php echo base_url().'Product/category/' . $category->id; ?>"> <?php echo $category->name; ?></a></label>
  <?php endforeach; ?>
</div>                   




                </div>
            </div> 

<!-- Laat cadeau zien op alle cadeaus pagina -->

<div class="main">
<?php foreach($products as $product) : ?>

   <a href="<?php echo base_url() ?>/Product/details/<?php echo $product["product_id"]; ?>"> <div class="main-products">

          <img id="cadeaufoto" src="<?php echo base_url(); ?>upload/<?php echo $product["product_foto_thumb"]; ?>">

        <div class="product_naam"><h3><?php echo $product["product_naam"]; ?></h3></div>
        <div class="ophaal_plaats">
           <?php  echo $product["ophaal_plaats"]; ?>
        </div>
        </a>

         <div class="aangeboden_door">     
         <?php 
          //Here is the active record query which is getting the 'voorname' and other data
          $userarray = $this->db->get_Where('users', array('user_id'=>$product["user_id"]))->row_array(); 
          // you can print_r($userarray); for see the array you get
      ?>
      <a href="<?php echo base_url() . 'User/userdetails/'.$product['user_id'];?>">
      <td><?php echo $userarray['voornaam'];?></td>
    </tr> 


        </div> 

</div>

<?php endforeach; ?>
</div>
</div>
</div>
</div>
<div class="clearfix"></div>

 <?php   include_once ('templates/footer.php');?>

1 个答案:

答案 0 :(得分:0)

以下是精细的分页代码,希望它能解决您的问题。

<强>模型

public function fetch_oldusers($limit, $start) {
        $this->db->limit($limit, $start);
        $query = $this->db->get_where('dc_user',array('Is_Hidden'=>0));

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }

查看

 <div class="text-center"><nav aria-label="Page navigation">
                                            <ul class="pagination">

                                                <li><?php echo $links; ?></li>

                                            </ul>
                                        </nav></div>

<强>控制器

public function Users()
    {
        $config = array();
        $config["base_url"] = base_url() . "master/Users";
        $config["total_rows"] = $this->bm->record_countolduser();
        $config['use_page_numbers'] =True;

        $config['cur_tag_open'] = '<a><b class="text-success">';
        $config['cur_tag_close'] = '</b></a>';
        $config["per_page"] =10;
        $config["uri_segment"] = 3;

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

        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data['title'] = "Users List";
        $data["posts"] = $this->bm->fetch_oldusers($config["per_page"], $page);
        $data["links"] = $this->pagination->create_links();
        $this->load->view('Admin/header',$data);
        $this->load->view('Admin/nav');
        $this->load->view('Admin/sidebar');
        $this->load->view('Admin/old_user', $data);
        $this->load->view('Admin/footer');
    }

这对我来说很好。