PHP /数据库表循环 - 一次只显示15行

时间:2012-05-24 17:25:18

标签: php sql loops mysqli

我有一个数据库,我试图通过PHP显示进行编辑。在主页面上,我只想显示表格的前15行,然后让用户点击以生成更多的表格行。

<?php 
include('../config.php'); 
$result = $mysqli->query("SELECT uid, title, description, tblFacilityHrsDateTimes.* FROM tblFacilityHrs LEFT JOIN tblFacilityHrsDateTimes ON tblFacilityHrs.uid = tblFacilityHrsDateTimes.owner_uid ORDER BY tblFacilityHrs.title") or die($mysqli->error);
    while($row =$result->fetch_assoc()){ 
        extract ($row);
        echo "<tr>";  
            echo "<td> {$id} </td>";  //hide when finished
            echo "<td> {$title} </td>"; 
            echo "<td> {$description} </td>";
            echo "<td> {$startEventDate} </td>";
            echo "<td> {$endEventDate} </td>";
            echo "<td> {$startTime} </td>";
            echo "<td> {$endTime} </td>";
            echo "<td> {$days} </td>";
            echo "<td> {$recurrence} </td>";
            echo "<td> {$finalDate} </td>";
            echo "<td>";
            echo "<a class=\"buttons\" href=edit.php?id={$id}&uid={$uid}>Edit</a><span class='icon'></span></a>
";
            echo " / ";
            echo "<a class=\"buttons\" href='javascript: Confirm()'>Delete</a>";
            echo "</td>";
        echo "</tr>";
        echo "</div>"; 
    } 

echo "</table>";

$result->free();
$mysqli->close();

?>

4 个答案:

答案 0 :(得分:4)

使用LIMIT

"SELECT uid, title, description, tblFacilityHrsDateTimes.* FROM tblFacilityHrs LEFT JOIN tblFacilityHrsDateTimes ON tblFacilityHrs.uid = tblFacilityHrsDateTimes.owner_uid ORDER BY tblFacilityHrs.title LIMIT 15"

答案 1 :(得分:2)

查看limit语法in this manual pageselect * from table limit 0,15将返回以offset 0(第一行)开头的15行。对于第二页,您使用limit 15, 15获取第16-30行,然后使用limit 30,15,依此类推。

答案 2 :(得分:2)

你需要看看Paging。本教程可能有所帮助:http://www.php-mysql-tutorial.com/wikis/php-tutorial/paging-using-php.aspx

答案 3 :(得分:1)

可能你正在寻找分页,这可以通过mysql LIMIT

来完成

像这样的东西

$result = $mysqli->query("SELECT uid, title, description, tblFacilityHrsDateTimes.* FROM tblFacilityHrs LEFT JOIN tblFacilityHrsDateTimes ON tblFacilityHrs.uid = tblFacilityHrsDateTimes.owner_uid ORDER BY tblFacilityHrs.title LIMIT 15, $page ")

$ page用于抵消

以下是一些创建自定义分页的教程

Pagination of MySQL Query Results

Pagination - what it is and how to do it