PHP/database table loop - display only 15 rows at

2019-07-27 14:35发布

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

?>

4条回答
时光不老,我们不散
2楼-- · 2019-07-27 15:20

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.

查看更多
倾城 Initia
3楼-- · 2019-07-27 15:23

Use LIMIT:

"SELECT uid, title, description, tblFacilityHrsDateTimes.* FROM tblFacilityHrs LEFT JOIN tblFacilityHrsDateTimes ON tblFacilityHrs.uid = tblFacilityHrsDateTimes.owner_uid ORDER BY tblFacilityHrs.title LIMIT 15"
查看更多
爷的心禁止访问
4楼-- · 2019-07-27 15:36
放我归山
5楼-- · 2019-07-27 15:37

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

查看更多
登录 后发表回答