2011-02-10 29 views
0

我基本上需要用while循環從數據庫中取出一些視頻信息並將它們放入div。唯一的問題是我需要在a和tag之間每次只放6個,然後轉到下6個等等。這裏是我的代碼:如何在一個while循環內分頁?

$count = 0; 
$sql = "SELECT * FROM videos ORDER BY id DESC"; 
$result_set = $database->query($sql); 
while($videos = $database->fetch_array($result_set)) { 
$count++; 
    // i know this is horribly wrong... 
if($count == 0 || (($count % 6)+1 == 1)) { 
    echo '<div>'; 
} 
    // i need 6 videos to go in between the <div> and </div> tags then go on to another 6 
    echo "<a href=\"video/{$videos}\">{$videos['title']}</a>"; 


if($count == 0 || (($count % 6)+1 == 1)) { 
    echo '<div>'; 
}   
} 
+0

你想加載所有從該數據庫中的視頻,並做傳呼的客戶端?或從數據庫一次加載6並做尋呼服務器端? – 2011-02-10 05:22:02

+0

我想加載它們並在客戶端執行分頁 – 2011-02-10 13:36:01

回答

1

這是一個efficent的方式做你想要什麼:

$resultPerPage = 6; 
$count = 0; 
$sql = "SELECT * FROM videos ORDER BY id DESC"; 
$result_set = $database->query($sql); 
$noPage = 1; 

echo '<div id="page_1" class="pages">'; 
while($videos = $database->fetch_array($result_set)) { 
    $count++; 
    echo "<a href=\"video/{$videos}\">{$videos['title']}</a>"; 
    if($count == $resultPerPage) { 
     echo '</div><div id="page_' . $noPage++ . '" class="pages">'; 
     $count=0; 
    }  
} 
echo '</div>';