我正在构建产品目录,我希望根据用户设备确定。即,如果Windows设备显示特定产品。我的产品列表绝对正常,但是在实施分页时,我被卡住了。
我想每页只列出6个项目,我有更多结果。然而,第一页显示了更多结果。当我选择下一步时,ID号会改变,以显示它正在显示下一个结果,但它会继续显示相同的页面,所以很明显我错过/在某处与我的代码混淆。我很感激任何建议:
$current_url = $_SERVER['REQUEST_URI'];
$current_url = substr($current_url, 1);
$results = mysqli_query($conn, "SELECT * FROM apps A INNER JOIN device D ON D.DeviceID = A.DeviceID WHERE D.DeviceName = '$os'");
$foundnum = mysqli_num_rows($results);
if ($foundnum==0){
echo "Sorry, there are currently no applications that are compatible with your device. Please try another option.";
} else {
echo "$foundnum applications are avaliable for '$os' devices:<p>";
$per_page = 6;
$start = $_GET['start'];
$max_pages = ceil($foundnum / $per_page);
if(!$start)
$start=0;
while($obj = $results->fetch_object()){
$applicationid=$obj->ApplicationID;
$start=0;
echo "<div class=\"col-4 col-sm-4 col-lg-4\">";
echo '<form method="post" action="cart_update.php">';
echo "<div id='product'><a href='appproduct.php?id=$applicationid'><img src='images/app_images/$applicationid.jpg' alt='Product picture'/></div>";
echo '<h2>'.$obj->ApplicationName.'</h2>';
echo '<p>'.$obj->ApplicationDescription.'</p>';
if($obj->App_cost=="0.00"){
echo '<p>Free</p>';
}else{
echo '<p>£'.$obj->App_cost.'</p>';
}
echo '<button class="add_to_cart">Add To Cart</button>';
echo '<input type="hidden" name="product_code" value="'.$obj->ApplicationID.'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form><br /><br /></div>';
}
//Pagination Starts
echo "<center>";
$prev = $start - $per_page;
$next = $start + $per_page;
$last = $max_pages - 1;
if($max_pages > 1){
//previous button
if (!($start<=0))
echo "<a href='index.php?os=$os=Search+source+code&start=$prev'>Prev |</a> ";
//pages
$i = 0;
for ($counter = 1; $counter <= $max_pages; $counter++){
if($i == $start){
echo " <a href='index.php?os=$os=Search+source+code&start=$i'><b> $counter |</b></a> ";
}
else {
echo " <a href='index.php?os=$os=Search+source+code&start=$i'> $counter |</a> ";
}
$i = $i + $per_page;
}
}
//next button
if (!($start >=$foundnum-$per_page))
echo " <a href='index.php?os=$os=Search+source+code&start=$next'> Next</a> ";
}
echo "</center>"
?>
答案 0 :(得分:1)
您需要在MySQL查询中使用LIMIT才能一次只获取一页结果。它看起来像LIMIT 0, 6
。
答案 1 :(得分:1)
更改此行将起作用
$results = mysqli_query($conn, "SELECT * FROM apps A INNER JOIN device D ON D.DeviceID = A.DeviceID WHERE D.DeviceName = '$os' LIMIT $_GET['start'], 6;");
但它并不是进行分页或直接从全局变量中添加变量的最佳方式。我建议你在php中使用leat mysql escape string函数
答案 2 :(得分:1)
您应该尝试通过数据库进行分页,而不是急切地加载所有结果。如果您有100000条记录并且只想显示其中的前6条,则需要获取可能不会使用的99994条记录。尝试使用sql的limit
。
至于“加粗”当前页码的问题,这里有逻辑错误:
$i = 0;
for ($counter = 1; $counter <= $max_pages; $counter++) {
if($i == $start){
echo " <a href='index.php?os=$os=Search+source+code&start=$i'><b> $counter |</b></a> ";
} else {
echo " <a href='index.php?os=$os=Search+source+code&start=$i'> $counter |</a> ";
}
$i = $i + $per_page;
根据这个片段,你将$ i与$ start进行比较,其中$ i总是等于0,所以它只会在第一页上加粗。