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

时间:2014-04-26 22:23:08

标签: php html database

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

1 个答案:

答案 0 :(得分:1)

您的查询中需要LIMITOFFSET

示例查询:

select * from table limit 10, 0

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

select * from table limit 10, 11

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

如何计算偏移量:

offset = (page - 1) * itemsPerPage + 1

Source

那么你需要什么?:

  1. 从链接检索每页的页面和项目(或者您可以为每页分配项目常量值,在脚本中指定此变量)
  2. 构建查询,具体取决于每页的页面和项目。
  3. 您的情况:

    <? //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功能不正确,但我只是想表明如何实现你想要的。

    告诉我某些事情是否不清楚。

相关问题