gpt4 book ai didi

PHP - 带分页的 MySQL 查询

转载 作者:行者123 更新时间:2023-11-29 01:14:08 25 4
gpt4 key购买 nike

我将如何为这个 MySQL 和 PHP 查询制作一个分页脚本。

if (isset($_GET['c'])) {
$c = $_GET['c'];
}

$query = mysql_query("SELECT * FROM Categories WHERE category = '$c' ");

WHILE($datarows = mysql_fetch_array($query)):

$id = $datarows['id'];
$category = $datarows['category'];
$code = $datarows['code'];

endwhile;

$query2 = mysql_query("SELECT * FROM Games WHERE category = '$code' ");

WHILE($datarows_cat = mysql_fetch_array($query2)):

$title = $datarows_cat['title'];
$description = $datarows_cat['description'];
$imgurl = $datarows_cat['image_name'];
$category = $datarows_cat['category'];
$views = $datarows_cat['view_count'];
$pagename = $datarows_cat['pagename'];
$featured = $datarows_cat['featured'];

if ($featured =="1") {$f = "<img src='http://my-site.com/images/star.png' width='13px' title='Featured Game' /> Featured"; } else {$f = "";}
if(is_int($views/2)) {
$views = $views / 2;
} else { $views = $views / 2 + .5; }

if (strlen($description) > 95) {
$desc= substr($description,0,95);
$desmod = "$desc...<br/><a href=\"http://my-site.com/$pagename#1\" title=\"$description\">- Read More</a>";
}
else {$desmod = "$description";}

echo "$f - <a href=\"http://my-site.com/$pagename\">$title - $desmod</a><br/>";

endwhile;

当我访问 http://my-site.com/categories/Action 时例如,该代码在我的类别表中查找该类别,然后一旦获得该类别的唯一代码,它就会运行另一个查询以在另一个表中查找具有该类别代码的所有游戏。然而,目前我有 200 多个游戏加载到一个类别,这导致加载时间很长。

感谢您的帮助!

最佳答案

首先找出特定类别有多少游戏

换行

$query2 = mysql_query("SELECT * FROM Games WHERE category = '$code' ");

$sql="SELECT * FROM Games WHERE category = '$code' ";
$query_count=mysql_query($sql);

在其后添加以下内容

$per_page =30;//define how many games for a page
$count = mysql_num_rows($query_count);
$pages = ceil($count/$per_page);

if($_GET['page']==""){
$page="1";
}else{
$page=$_GET['page'];
}
$start = ($page - 1) * $per_page;
$sql = $sql." LIMIT $start,$per_page";
$query2=mysql_query($sql);

然后在你想要的地方显示页数

<ul id="pagination">
<?php
//Show page links
for ($i = 1; $i <= $pages; $i++)
{?>
<li id="<?php echo $i;?>"><a href="linktoyourfile?c=<?php echo $c;?>&page=<?php echo $i;?>"><?php echo $i;?></a></li>
<?php
}
?>
</ul>

使用 CSS 进行分页这样就可以了

关于PHP - 带分页的 MySQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12315920/

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