gpt4 book ai didi

php - 选择第 n 页而不是第 n 页

转载 作者:行者123 更新时间:2023-11-29 14:55:54 24 4
gpt4 key购买 nike

我有一个网页,它对我的​​ MySQL 数据库的结果进行分页。下面的代码用于创建下拉菜单来导航结果。目前它被设置为选择页面n;这假设当我请求第 30 页(例如)时,相关记录存储在结果集的第 30 行,但从一些测试来看,情况显然并非如此。我应该如何更改它,以便它返回第 n 页?我知道 while 语句需要调整,但我就是不太明白。

    //…previous code…

$rowsPerPage = 1;

// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}

$offset = ($pageNum - 1) * $rowsPerPage;

$query3 = 'SELECT t1.s_name AS t1_sname, t1.uh_heading AS t1_unit, t1.ut_latin AS t1_latin, t1.ut_english AS t1_english, t1.uh_id, t1.chapter_sequence AS t1_chapters, t1.unit_sequence AS t1_units,
t2.s_name AS t2_sname, t2.ut_latin AS t2_latin, t2.ut_english AS t2_english, t2.chapter_sequence AS t2_chapters, t2.unit_sequence AS t2_units
FROM vw_source_text_and_translation AS t1
LEFT JOIN vw_source_text_and_translation AS t2
ON (t2.uh_id = t1.uh_id AND t2.s_id = " ' . $_SESSION['source2'] . ' " )
WHERE t1.s_id = " ' . $_SESSION['source1'] . ' "
ORDER BY t1.chapter_sequence, t1.unit_sequence' .
" LIMIT $offset, $rowsPerPage";

//...more code...

$query4 = 'SELECT uh_id, chapter_sequence, unit_sequence, uh_heading
FROM vw_source_text_and_translation
WHERE s_id = "' . $_SESSION['source1'] . '"';

//...more code...

$rows= $result4;
$numrows = 0;
$nav = '';
while ($row = mysql_fetch_assoc($rows))
{
$nav .= ' <option value="'. $row['uh_id'] .'"> '.
$row['chapter_sequence'] .'.'.$row['unit_sequence'] .
' - '. $row['uh_heading'] .'</option>';
$numrows ++;
}

/*…more code… */

好的,在一些帮助下,这个问题已部分修复; while 语句现在读作:

while ($row = mysql_fetch_assoc($rows)) 
{
$numrows ++;
$nav .= ' <option value=" ' . $numrows . '"> '. $row['chapter_sequence'] .'.'.
$row['unit_sequence'] .' - '. $row['uh_heading'] .'</option>';
}

这修复了分页,尽管我仍然遇到一些记录未显示的问题,即使当我在 MySQL Toolkit 中运行 $query3 并设置 $_SESSION['source1'] 和 $_SESSION['source2'] 的值时也是如此效果很好。似乎如果 t2.ut_latin 或 t2.ut_english 为 NULL 值,则不会显示任何数据,即使它识别出存在一条已分页的记录。但是,我想这超出了这个问题的主题,所以我可能不得不在新问题中重新发布这个问题。

最佳答案

您应该使用带有偏移量的 LIMIT 子句。

SELECT col1, col2, col3
FROM yourtable
ORDER BY something
LIMIT 40, 10

要计算偏移量,请将页码(从 0 开始)乘以每页的行数。

关于php - 选择第 n 页而不是第 n 页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4642753/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com