上一个/下一个按钮?

我想输入上一个和下一个按钮为我有一个论坛,因为在论坛变长之后,它超过了背景图像,并且看起来不太好!有谁知道要么做一个简单的方法,或者我可以看看上一个和下一个按钮?我想要它,所以每页仅限于每页5个帖子或其他内容。上一个/下一个按钮?

继承人的代码,如果它有所帮助。如果这是一个愚蠢的问题,我很抱歉。

<?php 

$dbc=mysql_connect('host','user','password','database') or die(mysql_error());

mysql_select_db('database',$dbc) or die(mysql_error());

?>

View Posts <br>

<form method="get" action="view_forum.php">

<label>Select Weather to Filter </label><br />

<select name="weather">

<option value="all">all</option>

<option value="cloudy">Cloudy</option>

<option value="sunny">Sunny</option>

<option value="windy">Windy</option>

<option value="snowy">Snowy</option>

<option value="mixy">Wintery Mix</option>

<option value="rainy">Rainy</option>

</select>

<input type="submit" value="view" />

</form>

<div id="view">

<center><img src="images/forum.png" width="589" height="97"></center>

</div>

<div id="white">

<div id="blue">

<div id="grey">

<div id="container">

<?php

$weather = mysql_real_escape_string($_GET["weather"]); // keep your input clean

if ($weather == "all") {

$sql = "SELECT * FROM stories ORDER BY id DESC";

} else {

$sql = "SELECT * FROM stories WHERE weather = '$weather' ORDER BY id DESC";

}

$result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) {

echo "<div class=\"names\"> {$row['name']}<br /></div>";

echo "<div class=\"weathers\">{$row['weather']}<br /></div>";

echo "<div class=\"stories\">{$row['story']}<br /></div>";

echo "<img src=\"images/line.png\" width='800' height='3'>";

echo "<br />";

}

?>

</div>

</div>

</div>

</div>

回答:

很简单。你在请求中保留一个页面变量。

if (!isset($_GET['page']) { 

$page = 1;

} else {

$page = (int)$_GET['page'];

}

而且在你的SQL语句,你会把这样的事情,它使用的页面变量调整的查询范围:

$query = 'SELECT * FROM someTable WHERE 1 LIMIT ' . (($page - 1) * $recordsPerPage) . ' ' . $recordsPerPage; 

类似的东西反正。现在为您上一页和下一页链接你把这样的事情,这样就可以增加/减少页面变量:

<? if ($page > 1) : ?> 

<a href="self.php?page=<?= $page - 1 ?>">Prev</a>

<? endif ?>

<? if ($page != $maxPages) : ?>

<a href="self.php?page=<?= $page + 1 ?>">Next</a>

<? endif ?>

回答:

您需要分页,在MySQL中,你可以使用TOP:

-- Fist 10 results: 

SELECT * FROM stories ORDER BY id DESC TOP 10

-- 10 results from the number 11 (0 is the first)

SELECT * FROM stories ORDER BY id DESC TOP 10,10

-- 10 results from the number 50

SELECT * FROM stories ORDER BY id DESC TOP 50,10

对于知道总的结果,你可以使用SQL_CALC_FOUND_ROWS:

SELECT SQL_CALC_FOUND_ROWS as total, stories.* FROM stories ORDER BY id DESC TOP 10 

从PHP,你可以计算出分页:

$page = isset($_GET['page']) ? (int) $_GET['page'] : 1; 

if ($page < 1) $page = 1;

$sql = "SELECT SQL_CALC_FOUND_ROWS as total, stories.* FROM stories ORDER BY id DESC TOP " . ($page-1) . ",10";

以上是 上一个/下一个按钮? 的全部内容, 来源链接: utcz.com/qa/263833.html

回到顶部