我正在使用codeigniter框架在我们的项目中进行分页,我希望显示20个产品/页面,但现在我可以显示1个产品/页面

时间:2016-11-08 07:04:20

标签: php codeigniter

我正在使用codeigniter框架在我们的项目中进行分页,我希望显示20个产品/页面,但现在我可以显示1个产品/页面。我尝试通过更改代码$ config [" per_page&# 34;] = 1;到$ config [" per_page"] = 20;但它显示1个产品/页面,产品很少,而不是全部。我的代码在下面

在我的视图页面中:

<div class="container">        
      <?php  $chunkedArray  = array_chunk($results, 4);
       foreach($chunkedArray as $newRow) {
       echo ' <div class="row section">';?>
  <?php
    foreach ($newRow as $display) {
?>
          <div class="col-md-3 product-display">
              <a href="<?php echo base_url();?>index.php/productdisplay_controller/productImage?id=<?php echo $display->id; ?>">
              <img src="<?php echo 'data:image;base64,'.$display->product_image; ?>" class="img-responsive image" />
              </a>

        <div class="imgage-description">
            <a href="#" title="<?php echo $display->product_name; ?>"><b><p><?php echo $display->product_name; ?></p></b></a> 
                <div class="stitle"> 
                    <a data-p4plog="60491438452" data-domdot="id:2679,pid:60491438452,ext:'n=1|s=p|t={{attr target}}'" href="//gesac.en.alibaba.com/company_profile.html#top-nav-bar" target="_blank">eletronics pvt ltd</a>
                </div>
                <div class="pmo"> 
                    <div class="price"> <b class=""> US $1-1000 </b> / Piece </div> 
                    <div class="min-order"> <b>10 Pieces</b> (Min. Order) </div> 
                </div>
              <a href="<?php  echo base_url();?>index.php/welcome/contact"> 
                  <button class="desc-btn" title="Contact Supplier"><i class="fa fa-envelope"></i> Contact Supplier</button> 
              </a>
              </div>
          </div>

          <?php
    }?>
            <?php
    echo '</div> ';
}
    ?>

  </div>
     <div class="center">
    <ul class = "pagination">
    <?php foreach ($links as $link) {
echo "<li>". $link."</li>";
} ?>
</ul>
   </div> 

我的模特页面:

<?php
class homemodel extends CI_Model{
function __construct() {
parent::__construct();
}

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



// Fetch data according to per_page limit.
public function fetch_data($limit, $id) {
$this->db->limit($limit);
$this->db->where('id', $id);
$query = $this->db->get("product_sub_category3");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}

return $data;
}
return false;
}

/*public function fetchData() {

  $query=$this->db->select('*');   
  $this->db->order_by('product_name','RANDOM');
  $this->db->from('product_sub_category3');
  $query = $this->db->get();
  return $query->result();
   } */
}

?>

在我的控制器中:

public function index()
    {
             $this->load->helper('url');
             $this->load->model('homemodel');

$config = array();
$config["base_url"] = base_url()."index.php/welcome/index";
$total_row = $this->homemodel->record_count();
$config["total_rows"] = $total_row;
$config["per_page"] =1;
$config['use_page_numbers'] = TRUE;
$config['num_links'] =2;
$config['cur_tag_open'] ='&nbsp;<a class="current">';
$config['cur_tag_close'] = '</a>';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->load->library('pagination');
$this->pagination->initialize($config);
if($this->uri->segment(3)){
$page = ($this->uri->segment(3)) ;
}
else{
$page = 1;
}
$data["results"] = $this->homemodel->fetch_data($config["per_page"], $page);
$str_links = $this->pagination->create_links();
$data["links"] = explode('&nbsp;',$str_links );

// View data according to array.
$this->load->view("home", $data);                  
    }

2 个答案:

答案 0 :(得分:0)

$str_links = $this->pagination->create_links();
$data['links'] = $str_links;
在您的视图中,只需执行echo $links;

让我知道它是否有效。

答案 1 :(得分:0)

我认为您需要提供偏移以及获取记录的限制。

$this->db->limit($limit,$offset);

并删除此行$this->db->where('id', $id);

$offset将从哪一点获取下一条记录。例如:1-20,21-40等。