Posted in PHP onApril 01, 2021
<?php
$wherelist = array();
$urlist = array();
if (!empty($_GET['news_title'])) {
$wherelist[] = " news_title like '%".$_GET['news_title']."%'";
$urllist[] = "news_title=".$_GET['news_title'];
}
$type=$_GET['type'];
var_dump($type);
if (!empty($_GET['type'])) {
$wherelist[] = " type_Id like '%" . $_GET['type'] . "%'";
$urllist[] = "type_Id=" . $_GET['type'];
}
$where = "";
if (count($wherelist) > 0) {
$where = " where ".implode(' and ', $wherelist);
$url = '&'.implode('&', $urllist);
}
// 链接
$hostname_conn = "localhost";
$database_conn = "sbing";
$username_conn = "root";
$password_conn = "root";
$conn = @mysql_connect($hostname_conn, $username_conn, $password_conn) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_conn, $conn);
//分页的实现原理
//1.获取数据表中总记录数
mysql_query("set names 'utf8'");
$sql = "select * from news_content $where";
$result = mysql_query($sql);
$totalnum = mysql_num_rows($result);
//每页显示条数
$pnews_datesize = 3;
//总共有几页
$maxpnews_date = ceil($totalnum / $pnews_datesize);
$pnews_date = isset($_GET['pnews_date']) ? $_GET['pnews_date'] : 1;
if ($pnews_date < 1) {
$pnews_date = 1;
}
if ($pnews_date > $maxpnews_date) {
$pnews_date = $maxpnews_date;
}
$limit = " limit ".($pnews_date - 1) * $pnews_datesize.",$pnews_datesize";
$sql1 = "select * from news_content {$where} order by news_Id desc {$limit}"; //此处加了id降序
$res = mysql_query($sql1);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户资料显示</title>
</head>
<body>
<form action="gnewshow.php" method="get">
<input type="hidden" name="type" value=<?php
echo $type;
?>>
用户名<input type="text" name="news_title" value="<?php echo $_GET['news_title'] ?>" size="8">
<!-- <input type="button" value="查看全部" onclick="window.location='gnewshow.php'">-->
<input type="submit" value="搜索">
</form>
<br/>
<table border="1" width="500">
<tr>
<td>编号</td>
<td>用户名</td>
<td>年龄</td>
<td>性别</td>
<td>电话</td>
<td>地址</td>
</tr>
<?php
if ($res) {
while ($row = mysql_fetch_assoc($res)) { ?>
<tr>
<td><?php echo $row['news_Id'] ?></td>
<td><?php echo $row['news_title'] ?></td>
<td><?php echo $row['news_date'] ?></td>
<td><?php if ($row['sex']) {
echo '男';
} else {
echo '女';
} ?></td>
<td><?php echo $row['news_author'] ?></td>
<td><?php echo $row['news_QzTop'] ?></td>
</tr>
<?php }
} else {
echo " <td>没有数据</td>";
} ?>
<tr>
<td colspan="6">
<?php
echo " 当前{$pnews_date}/{$maxpnews_date}页 共{$totalnum}条";
echo " <a href='gnewshow.php?type={$type}&pnews_date=1{$url}'>首页</a> ";
echo "<a href='gnewshow.php?type={$type}&pnews_date=".($pnews_date - 1)."{$url}'>上一页</a>";
echo "<a href='gnewshow.php?type={$type}&pnews_date=".($pnews_date + 1)."{$url}'>下一页</a>";
echo " <a href='gnewshow.php?type={$type}&pnews_date={$maxpnews_date}{$url}'>尾页</a> ";
?>
</td>
</tr>
</table>
</body>
</html>
php 原生分页
- Author -
HaKa一声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@