本帖最后由 zfzhhh 于 2020-11-3 10:58 编辑
[PHP] 纯文本查看 复制代码 <?php
error_reporting(0);
///获取页码
$page = $_GET["page"];
$kw = $_GET['kw'];
$kwsql ="";
$kwpage ="";
if($kw !==null &&$kw!=""){
$kw = strip_tags($kw);
$kwsql = " where word like '%".$kw ."%' ";
$kwpage ="&kw=".$kw;
}
$num = 4; //设置每页显示数量
if($page ===null ){ $page ="1"; }
$page = (int)$page;
//////
try {
$sqlite = new PDO("sqlite:test.db");
$sqlite->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo "error";
die($e);
}
////获取总行数
$sth = $sqlite->prepare('SELECT count(*) as num FROM dict ' . $kwsql);
$sth->execute();
$rows = $sth->fetchAll();
$numRows = (int)$rows[0]["num"];
//////
///
$sth = $sqlite->prepare('SELECT * FROM dict '.$kwsql.' limit '.($page-1)*$num . ' ,'.$num);
$sth->execute();
$result = $sth->fetchAll();
?>
<html>
<head>
<title>DICTIONARY</title>
<meta charset="utf-8">
<meta name='viewport' content='width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no'>
</head>
<body>
<h1>词语查询</h1>
<div>
<form action="" method="get" >
<span>查询内容:</span>
<input id="box" name="kw" type="text" placeholder="请输入要查询的内容">
<input type="submit" value="查询">
</form>
</div>
<?php
foreach($result as $row)
echo '<div class="jieguo">
<strong>词语:</strong><span>'.$row['word'].'</span>
<strong>发音:</strong><span>'.$row['fayin'].'</span>
<strong>释义:</strong><span>'.$row['shiyi'].'</span>
<strong>出处:</strong><span>'.$row['chuchu'].'</span>
</div><br />';
///////////////下面是显示上下页的代码
if($page>1){
echo '<a href="/test2.php?page=' . ($page-1).$kwpage .'">上一页</a>';
}
if($page*$num<$numRows){
echo '<a href="/test2.php?page=' . ($page+1).$kwpage .'">下一页</a>';
}
echo '总结果'.$numRows.'条';
?>
</body>
</html> |