简体   繁体   中英

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

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

?>

使用LIMIT

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM