我想从下拉列表中保存排序结果,并在php和mysql中进行分页

时间:2015-04-07 13:14:27

标签: php mysql html5 sorting

大家好:我的网站上有问题正在处理它们。我搜索了一个解决方案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>

最后这是第一个问题,并首先尝试在此网站上提问 任何人都可以帮我修复这段代码..

抱歉英语不好

1 个答案:

答案 0 :(得分:0)

您必须定义$ offset,例如:

$offset=$i*$recored_limit;

$ recored_limit也需要定义,尝试:

$recored_limit=5;

LIMIT从$ offset返回$ recored_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";
}