drm30963 2015-04-07 13:14
浏览 53

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

Hello everyone: I have a problem on my site is working on them. I searched on a solution for 5 days and I can't find any result I needed it...

in short : I have list of car and I want to sort it by price descending and ascending use dropdown list and I used paging to split the record to 5 item in one page ..

when I choose for example a descending from drop down list it's show the result successful but when i go to the next page in paging the sort it's lost..

I trying use SESSION but isn't worked yet and still when I press on next page keep the sorting lost

I do modify update on the code after first replay but problem still exist:(

this is my html code:

 <?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>

this my code to paging:

$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 );

?>

this is query to show data and sort:

    $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"]; } 

this part of pagination with session to keep sort result when i go to next page

<!-- 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>

finally this is first question and first try to question on this site can anybody help me to fixed this code ..

and sorry for bad English

  • 写回答

1条回答 默认 最新

  • dosrmo0442 2015-04-07 14:04
    关注

    You must define $offset, like:

    $offset=$i*$recored_limit;
    

    $recored_limit also need to be defined,try:

    $recored_limit=5;
    

    LIMIT return you $recored_limit record from $offset; For saving filter asceding:

    $_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";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染