PHP/database table loop - display only 15 rows at time

∥☆過路亽.° 提交于 2019-12-24 16:42:01

问题


I have a database I'm trying to display for edit through PHP. On the main page I want to show only the first 15 rows of the table then have the user click to generate more table rows.

<?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();

?>

回答1:


Probably you are looking for Pagination, this can be done through mysql LIMIT

Something like that

$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 ")

Where $page is for offset

Here are few tutorial for creating custom pagination

Pagination of MySQL Query Results

Pagination - what it is and how to do it




回答2:


Use LIMIT:

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



回答3:


Check out the limit syntax in this manual page. select * from table limit 0,15 will return 15 rows starting with offset 0 (the first row). For the second page you'd use limit 15, 15 to get rows 16-30, then limit 30,15 and so on.




回答4:


You need to look at Paging. This tutorial may help: http://www.php-mysql-tutorial.com/wikis/php-tutorial/paging-using-php.aspx



来源:https://stackoverflow.com/questions/10742378/php-database-table-loop-display-only-15-rows-at-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!