添加多个页面,在同一页面上显示约10条数据库记录


Add multiple pages displaying ~10 database records on the same page

我有从数据库中检索记录并将其显示在页面上的代码。我正在尝试获得它,这样当它检索到超过10条记录时,它会添加一个子页面,用户可以在那里点击查看接下来的10条记录,而无需点击makiing.php页面。


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Good Homes</title>
<link href="CSS.css" rel="stylesheet" type="text/css" media="screen">
<style type="text/css">
</style>
</head>
<script>
</script>
<body>
<?php
$myConnection=mysqli_connect("goosedesignscouk.ipagemysql.com","watsonr","password","webtech");
if (mysqli_connect_errno()) {
    echo "Unable to connect, error: " . mysqli_connect_error();
}
$myResults=mysqli_query($myConnection, "SELECT NAME, SURNAME, DATE, MESSAGE FROM testimonies ORDER BY TID DESC");
?>
<div id="topbar">
<div id="orangebar">
</div>
<div id="logo">
    <img src="logo.png" alt="logo" height="100">
</div>
<div id="navigation">
    <ul id="css3menu1" class="topmenu">
        <li class="topfirst"><a href="index.php" style="width:120px;height:28px;line-height:28px;">Home</a></li>
        <li class="topmenu"><a href="search.php" style="height:28px;line-height:28px;">Search Property</a></li>
        <li class="topmenu"><a href="account.php" style="width:117px;height:28px;line-height:28px;">My Acount</a></li>
        <li class="topmenu"><a href="testimonies.php" style="width:115px;height:28px;line-height:28px;">Testimonies</a></li>
        <li class="toplast"><a href="about.php" style="width:112px;height:28px;line-height:28px;">About us</a></li>
    </ul>
</div>
<div id="login">
    <form>
        <table>
            <tr>
                <td><label><font color="#fff">Username:</font></label></td>
                <td><input type="text" name="username"></td>
            </tr>
            <tr>
                <td><label><font color="#fff">Password:</font></label></td>
                <td><input type="password" name="password"></td>
            </tr>
            <tr>
                <td></td><td align="right"><input type="button" onclick="parent.location='register.php'" value="Register"><input type="submit" value="Submit"></td>
            </tr>
         </table>
    </form>
</div> 
</div>
<div id="textbody">
    <h1>Add Your Testimonial</h1>
        <form method="post" action="addtestimonial.php">
            <table>
            <tr>
                <td>Name:</td>
                <td><input name="name" size="20" maxlength="20"></td>
            </tr>
            <tr>
                <td>Surname:</td>
                <td><input name="surname" size="20" maxlength="20"></td>
            </tr>
            <tr>
                <td>Testimonial:</td>
                <td><textarea name="testimonial" cols="100" rows="5" ></textarea></td>
            </tr>
        </table>
        <br />
        <input  type="submit" value="Add New" name="submit">
        <input type="reset" value="Cancel">
    </form>
<h1>Most Recent Testimonials</h1>
<?php 
    while ($currentRow = mysqli_fetch_array($myResults)) {
?>
<div id"testimonies">
    <p> 
        <?php 
            echo $currentRow['MESSAGE'];
            echo "<br>";
            echo $currentRow['NAME'] . " " . $currentRow['SURNAME'] . " - " . $currentRow['DATE'];
        ?>
    </p>
</div>
<?php
    }
?>
</div>

<div id="footer">
</div>
<?php
    mysqli_close($myConnection);
?>
</body>
</html>

所以我试图让它在第1页显示记录1-10,在第2页显示记录11-20等等。

查询中需要LIMITOFFSET

查询示例:

select * from table limit 10, 0

这将返回偏移量为0的10行。

select * from table limit 10, 11

这将返回偏移量为11的10行。Etc等

如何计算偏移量:

offset = (page - 1) * itemsPerPage + 1

那么你需要什么呢?:

  1. 从链接中检索页面和每页项目(或者您可以为每页项目分配常数值,在脚本中分配此变量)
  2. 根据页面和每页的项目生成查询

您的情况:

<? //connection to db
$items = 10;
$page = $_GET['page'];
$offset = ($page-1)*$items+1;
$myResults=mysqli_query($myConnection, "SELECT NAME, SURNAME, DATE, MESSAGE FROM testimonies ORDER BY TID DESC LIMIT $items, $offset");
?>

注意:我没有使用mysqli,所以我不知道,也许我使用mysqli函数的例子不正确,但我只是想展示如何实现您想要的。

如果有什么不清楚的地方,请告诉我。