【问题标题】:Add multiple pages displaying ~10 database records on the same page添加多个页面,在同一页面上显示约 10 条数据库记录
【发布时间】:2014-06-12 14:04:23
【问题描述】:

我有从数据库中检索记录并将它们显示在页面上的代码。我正在尝试获取它,以便当它检索超过 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 等。

【问题讨论】:

    标签: php html database


    【解决方案1】:

    您的查询中需要LIMITOFFSET

    查询示例:

    select * from table limit 10, 0
    

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

    select * from table limit 10, 11
    

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

    如何计算偏移量:

    offset = (page - 1) * itemsPerPage + 1
    

    Source

    那么你需要什么?:

    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 函数不正确,但我只是想展示如何实现你想要的。

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

    【讨论】:

      猜你喜欢
      • 2015-03-09
      • 2018-10-14
      • 2010-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      • 1970-01-01
      • 2011-07-13
      相关资源
      最近更新 更多