【问题标题】:How can I do pagination of HABTM data?如何对 HABTM 数据进行分页?
【发布时间】:2009-05-18 21:00:01
【问题描述】:

我有一个表,其中一个集合有许多实体,一个实体具有并属于许多集合..现在对于特定集合有许多实体..我如何对属于特定集合的那些实体进行分页..

我的查找查询说.., $this->Collection->find('first', array('condition'=>array('uid'=>$uid)), '包含(数组('实体'))); 现在如何对实体的结果进行分页..

【问题讨论】:

    标签: php cakephp


    【解决方案1】:

    在你的控制器动作中

    $this->paginate=array('Entity' => array( '条件' => "Entity.collection_id=$id", '字段' => 数组('实体。*') ) );

    $this->set('entities', $this->paginate($this->Collection->Entity));

    【讨论】:

      【解决方案2】:

      我在这里假设您使用的是 SQL 数据库。
      现在我还没有测试代码,但我认为它应该可以工作。

      // First query to get some info.
      $testquery = mysql_query("SELECT * FROM `table` WHERE `entity` = 'something'");
          if(!$testquery) die(mysql_error());
      
      $total_items      = mysql_num_rows($testquery);     // Count the total number of entity's that match the criteria.  
      $limit            = 10;     // Maximun number of entity's on page.
      $page             = $_GET['page'];
      
      //calcuate total pages
      $total_pages     = ceil($total_items / $limit); // ceil is used to round up fractions to the next int
      $set_limit          = $page * $limit - ($limit);
      
      
      $query2 = mysql_query("SELECT * FROM `table` WHERE `entity` = 'something' LIMIT $set_limit, $limit");
          if(!$query2) die(mysql_error());
      
      
      //show data matching query:
      while($code = mysql_fetch_object($query2)) {
          echo("item: ".$code->title."<BR>");
      }
      
      
      // This displays the "previous page" link if there is a previous page.
      $prev_page = $page - 1;
      if($prev_page >= 1) {
          echo("<a href=yourpagename.php?page=$prev_page>Previous</a>");
      }
      
      
      //Display middle pages: 
      $mid_page = 1;
      while ($total_pages >= $mid_page) {
          if ($page == $midpage){
              echo ("<b>$mid_page</b> | ");
          }
          else {
              echo ("  <a href=yourpagename.php?page=$mid_page> $mid_page </a> | ");
              $midpage++;
          }
      }
      
      // This page will display a "next page" link if there is one.
      $next_page = $page + 1;
      if($next_page <= $total_pages) {
          echo("<a href=yourpagename.php?page=$next_page>Next</a>");
      }
      

      【讨论】:

        猜你喜欢
        • 2019-06-09
        • 2011-10-02
        • 2016-03-29
        • 2010-11-30
        • 1970-01-01
        • 2022-01-21
        • 2023-04-01
        • 2020-02-18
        • 2016-11-06
        相关资源
        最近更新 更多