2009-04-20 66 views
0

我有一個表集合有許多實體和一個實體有很多colections ..現在有一個特定的集合有很多實體..如何我可以分頁屬於一個特定的集合的那些實體..如何分頁HABTM數據?

(數組'(實體')),

我發現的查詢說.., $ this-> Collection-> find('first',array('condition'=> array('uid'=> $ uid)), ' ))); 現在如何分頁實體的結果..

回答

2

在您的控制器動作

$這 - >分頁=陣列( '實體'=>數組( '條件'=> 「Entity.collection_id = $ ID」, '字段'= > array('Entity。*') ) );

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

1

我假設你在使用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>"); 
}