大家好:我的网站上有一个问题,正在处理它们。我在一个解决方案上搜索了5天,但我找不到任何我需要的结果…
简而言之:我有一个汽车列表,我想通过价格降序和升序对其进行排序。使用下拉列表,我使用分页将记录拆分为一页中的5项。。
例如,当我选择从下拉列表中降序时,它会显示成功的结果,但当我在分页中转到下一页时,它就会丢失。。
我尝试使用SESSION,但还没有工作,当我按下下一页时,仍然保持排序丢失的
我确实在第一次重播后修改了代码的更新,但问题仍然存在:(
这是我的html代码:
<?php session_start();?>
<select name="price" id="price" class="css-dropdowns" tabindex="1" >
<option value="0">Price Ascending</option>
<option value="1">Ascending</option>
<option value="2">Descending</option>
</select>
这是我的寻呼代码:
$select_demo="select c.car_id, car_name,price,model_year,drive_traine,mile_age,exterior_color,interior_color,mbg,stocke_num,vin_num,video_url,image,body_style,cylinders_num,displacement,gearbox_speed,gearbox_type,brand_name
from car as c join car_body as cb
on c.car_id=cb.car_id
join engine as e
on c.car_id=e.car_id
join brand as b
on b.brand_id = c.brand_id
join transmission as t
on c.car_id=t.car_id";
if(!$demo_result=$db->query($select_demo))
{
die ($db->error);
}
$recored_count=$demo_result->num_rows;
$recored_limit=5;
if(isset($_GET['page']))
{
$page=$_GET['page'];//+1
}
else
{
$page=1;
}
$offset=($page-1)*$recored_limit;
$total_page=ceil($recored_count / $recored_limit );
?>
这是显示数据和排序的查询:
$sql_paging="select c.car_id,car_name,price,model_year,drive_traine,mile_age,exterior_color,interior_color,mbg,stocke_num,vin_num,video_url,image,body_style,cylinders_num,displacement,gearbox_speed,gearbox_type,brand_name
from car as c join car_body as cb
on c.car_id = cb.car_id
join engine as e
on c.car_id = e.car_id
join brand as b
on b.brand_id = c.brand_id
join transmission as t
on c.car_id = t.car_id";
//to submit the dropdown list to get sort i want
if($_SERVER['REQUEST_METHOD']=='POST')
{
{
$_SESSION["price_filter"]=$_POST["price_filter"]; // call the ajax function to submit price drop down list
if (isset($_SESSION["price_filter"]) && !empty ($_SESSION["price_filter"]))
{
//query to show price 1 result
if ($_SESSION["price_filter"] == 1)
{
$sql_paging.=" order by price asc";
}
else if($_SESSION["price_filter"] == 2)
{
$sql_paging.=" order by price desc";
$_SESSION["price"]=($price ==2);
}
} //end of if filter
}//end of if request
$sql_paging.=" LIMIT $offset,$recored_limit";
if(!$paging_result=$db->query($sql_paging))
{
die ($db->error);
}
while($row=$paging_result->fetch_assoc())
{
##I don't paste all of codes it's just part of them for example ##
<code> echo "price".$row["price"]; }
当我转到下一页时,用会话进行分页以保持排序结果的这一部分
<!-- 1 st page-->
<li class=""> <a href="<?php echo $_SERVER["PHP_SELF"]."?page="."1"."&f=".$_SESSION["price_filter"]?>"><i class="fa fa-angle-left"></i></a></li>
<?php
for ($i=1; $i<=$total_page; $i++)
{?>
<li><a href="<?php echo $_SERVER["PHP_SELF"]."?page=".$i."&f=".$_SESSION["price_filter"]?>"><?php echo $i;?></a></li>
<?php } // end of for ?>
<!-- last page-->
<li><a href="<?php echo $_SERVER["PHP_SELF"]."?page=".$total_page ?>"><i class="fa fa-angle-right"></i></a></li>
最后,这是第一个问题,也是第一次尝试在这个网站上提问有人能帮我修复这个代码吗。。
很抱歉英语不好
您必须定义$offset,例如:
$offset=$i*$recored_limit;
$recred_limit也需要定义,请尝试:
$recored_limit=5;
LIMIT从$offset返回$recred_LIMIT记录;用于保存过滤器上行:
$_SESSION["price_filter"]=$_POST["price_filter"]
...
if ($_SESSION["price_filter"] == 1){
$sql_paging.=" order by price asc";
}
elseif($_SESSION["price_filter"] == 2){
$sql_paging.=" order by price desc";
}