如何对HABTM数据进行分页?

时间:2023-01-27 16:38:01

i have a table where a Collection has many Entities and an Entity has and belongs to many colections..now for a particular collection there are many entities..how can i paginate those entities belonging to a particular collection..

我有一个表,其中一个集合有许多实体和一个实体已经并属于许多colections ..现在对于一个特定的集合有很多实体..我可以分页那些属于特定集合的实体。

my find query says.., $this->Collection->find('first', array('condition'=>array('uid'=>$uid)), 'contains(array('Entity'))); now how to paginate the result of entities..

我的查询说..,$ this-> Collection-> find('first',array('condition'=> array('uid'=> $ uid)),'contains(array('Entity'))) ;现在如何分页实体的结果..

2 个解决方案

#1


In your controller action

在你的控制器动作中

$this->paginate=array('Entity' => array( 'conditions' => "Entity.collection_id=$id", 'fields' => array('Entity.*') ) );

$ this-> paginate = array('Entity'=> array('conditions'=>“Entity.collection_id = $ id”,'fields'=> array('Entity。*')));

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

#2


I'm assuming here that you're using an SQL database.
Now i haven't tested the code, but i think it should work.

我在这里假设您正在使用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>");
}

#1


In your controller action

在你的控制器动作中

$this->paginate=array('Entity' => array( 'conditions' => "Entity.collection_id=$id", 'fields' => array('Entity.*') ) );

$ this-> paginate = array('Entity'=> array('conditions'=>“Entity.collection_id = $ id”,'fields'=> array('Entity。*')));

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

#2


I'm assuming here that you're using an SQL database.
Now i haven't tested the code, but i think it should work.

我在这里假设您正在使用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>");
}