【问题标题】:I'm working on pagination in our project using codeigniter framework,i want to display 20 products/page but now i can display 1product/page我正在使用 codeigniter 框架在我们的项目中进行分页,我想显示 20 个产品/页,但现在我可以显示 1 个产品/页
【发布时间】:2017-03-21 17:19:39
【问题描述】:

我正在使用 codeigniter 框架在我们的项目中进行分页,我想显示 20 个产品/页面,但现在我可以显示 1 个产品/页面。我尝试通过更改代码 $config["per_page"] =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);                  
    }

【问题讨论】:

  • 增加这个$config['num_links'] =2;
  • 我增加了但不能正常工作
  • $this-&gt;homemodel-&gt;fetch_data($config["per_page"], $page); 这里 $page 是第三个 uri 段,应该用作 $offset 由另一个用户回答。但是在您的模型类中,您接收为$id 并检索id 为该值的行。这是错误的。

标签: php codeigniter


【解决方案1】:
$str_links = $this->pagination->create_links();
$data['links'] = $str_links;

在您看来,只需执行echo $links;

让我知道它是否有效。

【讨论】:

  • 在我得到这个错误之后,遇到了一个 PHP 错误 严重性:通知消息:数组到字符串的转换文件名:views/home.php 行号:65 回溯:文件:C:\xampp\htdocs \etrade\application\views\home.php 行:65 功能:_error_handler 文件:C:\xampp\htdocs\etrade\application\controllers\Welcome.php 行:39 功能:查看 文件:C:\xampp\htdocs\etrade \index.php 行:315 功能:require_once
  • 据我所知$this-&gt;pagination-&gt;create_links(); 返回一个字符串。请参阅此处的文档codeigniter.com/userguide3/libraries/pagination.html
  • 但在我看来,这个回显代码运行良好
      "。 $链接。""; } ?>
【解决方案2】:

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

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

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

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

【讨论】:

  • $offset = ($this->uri->segment(3) != '' ? $this->uri->segment(3): 0);
  • 并将$offset 传递到您在控制器中获取记录的函数中。在模型中添加 $this->db->limit($limit,$offset);并删除 ID 条件。
  • 代码在第 1 页显示 1-20 个产品,但在第 2 页显示 3-22 个产品,在第 3 页显示 4-23 个产品,以此类推,为什么呢?
  • 在模型中使用它:$this-&gt;db-&gt;limit($offset,$limit); 并在控制器中添加 $offset = $limit * $page。例如: $offset = 20 * 1 , 20*2 等
  • 将其用作 $page,$page = ($this-&gt;uri-&gt;segment(3) != '' ? $this-&gt;uri-&gt;segment(3): 0);
猜你喜欢
  • 2013-02-06
  • 1970-01-01
  • 1970-01-01
  • 2016-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-12
  • 1970-01-01
相关资源
最近更新 更多