zoenbo 发表于 2020-11-3 08:07

紧急求助SQLite分页问题

本帖最后由 zoenbo 于 2020-11-3 11:16 编辑

众大佬帮我看看SQLite怎么分页吧!参考了无数MYSQL的分页方法始终失败,弄了整整一天没任何结果,现在只能是出所有结果... 在线等...
<?php
try {
    $sqlite = new PDO("sqlite:test.db");
    $sqlite->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    die($e);
}
    $sth = $sqlite->prepare('SELECT *
      FROM dict');
    $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>
    <p>
      <span>查询内容:</span>
      <input id="box" type="text" placeholder="请输入要查询的内容">
    </p>
    <p>
      <button>查询</button>
    </p>
    <?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 />';?>
<a href="">上一页</a> <a href="">下一页</a> 总结果?条
</body>
</html>

zfzhhh 发表于 2020-11-3 10:04

本帖最后由 zfzhhh 于 2020-11-3 10:58 编辑

<?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 numFROM dict ' . $kwsql);
$sth->execute();
$rows = $sth->fetchAll();
$numRows = (int)$rows["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>

不知道改成啥 发表于 2020-11-3 08:47

var sql = string.Format("select * from dict order by Id limit {0} offset {0}*{1}", size, index-1);//size:每页显示条数,index页码
用limit啊

xaibin 发表于 2020-11-3 08:53

看不懂但是感觉好厉害的样子,先学习了,谢谢分享。

zoenbo 发表于 2020-11-3 09:14

tanzhiwei 发表于 2020-11-3 08:47
var sql = string.Format("select * from dict order by Id limit {0} offset {0}*{1}", size, index-1);// ...

这个具体怎么用在前端页面呢?

zfzhhh 发表于 2020-11-3 09:31

https://i.loli.net/2020/11/03/bqF6i1KsUf3DLYP.gif

<?php
error_reporting(0);
///获取页码
$page = $_GET["page"];
$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) {
    die($e);
}

////获取总行数
$sth = $sqlite->prepare('SELECT count(*) as numFROM dict ');
$sth->execute();
$rows = $sth->fetchAll();
$numRows = (int)$rows["num"];
//////
///

    $sth = $sqlite->prepare('SELECT * FROM dict 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>
    <p>
      <span>查询内容:</span>
      <input id="box" type="text" placeholder="请输入要查询的内容">
    </p>
    <p>
      <button>查询</button>
    </p>
    <?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) .'">上一页</a>';
    }
    if($page*$num<$numRows){
      echo '<a href="/test2.php?page=' .($page+1) .'">下一页</a>';
    }
    echo '总结果'.$numRows.'条';
    ?>
</body>
</html>

不知道改成啥 发表于 2020-11-3 09:33

zoenbo 发表于 2020-11-3 09:14
这个具体怎么用在前端页面呢?

不懂php怎么做

zoenbo 发表于 2020-11-3 09:36

zfzhhh 发表于 2020-11-3 09:31





哇,厉害厉害!大佬,你看能不能帮我实现一下搜索关键词出结果并分页不?那个实现了就彻底完美了~~{:1_893:}

zfzhhh 发表于 2020-11-3 09:41

zoenbo 发表于 2020-11-3 09:36
哇,厉害厉害!大佬,你看能不能帮我实现一下搜索关键词出结果并分页不?那个实现了就彻底完美了~~{:1_89 ...

那样你不就没事干了吗?
不能这样做{:1_911:}

zoenbo 发表于 2020-11-3 09:44

zfzhhh 发表于 2020-11-3 09:41
那样你不就没事干了吗?
不能这样做

大佬求你了~好不容易遇到个救星~我有个悬赏的帖子,大佬把答案贴上去,我直接采纳哈 拜托了拜托了,真的等了很多天了 {:1_893:}

zoenbo 发表于 2020-11-3 09:47

zfzhhh 发表于 2020-11-3 09:31





大佬开恩,再帮帮我哈,最近也是刚学起,完全小白啥也不懂,此后一定好好学习天天向上
页: [1] 2
查看完整版本: 紧急求助SQLite分页问题