sorting problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pradeepjain
    Contributor
    • Jul 2007
    • 563

    sorting problem

    i had posted the same code in javascript area !! this i am posting bcos of different problem. as you can see in ma code there is a dropdown called sort ...its sorts the result by the value he selects . its working fine... say a user selects price as the sort thing . so it lists all mobiles with its price range with mobile in some range together . wht i need to do here is in the say there are 4 groups with diff price range and in each range there are 4 mobiles...here i need to sort those 4 mobiles according to names of mobiles.this i am not able to achieve .plzz c ma code .hope the code is clear and ma code too.

    ps : please tell me if ma code not according to some standards . how to improve .

    product_display .php

    Code:
    <?php
    header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
    session_start();
    $mobile_brand=$_GET['brand'];
    
    $price=$_GET['price'];
    
    $type=$_GET['type'];
    
    $os=$_GET['os'];
    $sort=$_GET['sort'];
    if($sort == "")
            {
                    $sort = "overall_rating";
            }
    
    $sorting_order=$_GET['sort_order'];
    if($sorting_order == "")
            {
                    $sorting_order = "Desc";
            }
    
    $build_query = "";
            $add = "";
            $append = "";
    if($mobile_brand != "")
    {
            $append = "yes";
            $build_query .= " property1 = '" .$mobile_brand ."'";
    }
    if($price != "")
    {
            if($append =="yes")
            {
            $add = "and";
            }
            $append = "yes";
            $build_query .= " $add property4 = '" .$price ."'";
    }
    
    if($type != "")
    {
      if($append =="yes")
            {
            $add = "and";
            }
            $append = "yes";
            $build_query .= " $add property3 = '" .$type ."'";
    }
    
    if($os != "")
    {
            if($append =="yes")
            {
            $add = "and";
            }
            $build_query .= " $add property6 = '" .$os ."'";
    }
    //echo $build_query;
    /*$wprice  = $_GET['price'];
    $wtype = $_GET['type'];
    $wos  = $_GET['os'];
    if(($wprice != "")||($wtype != "")||($wos != ""))
            {
                    if($wprice != "")
                    {
                    $sqladd = "where property4 ='" .$wprice ."'";
                    }
                    if($wtype != "")
                    {
                    $sqladd = "where property3 ='" .$wtype ."'";
                    }
                    if($wos != "")
                    {
                    $sqladd = "where property6 ='" .$wos ."'";
                    }
            }
            else
            {
                    $sqladd = "";
            }*/
    include_once('db.php');
    include_once('valid-scripts/validateData.php');
    error_reporting (E_ALL & ~ E_NOTICE);
    ?>
    <html>
    <head>
            <script src="jquery.js" type="text/javascript"></script>
            <script src="jquery.rating.js" type="text/javascript" language="javascript"></script>
            <link href="jquery.rating.css" type="text/css" rel="stylesheet"/>
            <link rel="stylesheet" type="text/css" href="style.css">
    
    <script type="text/javascript">
    $(document).ready(function() {
        $("#mobile_brand").change(function() {
                    $.post("mobile_change.php",{ brand:$("#mobile_brand").val(),price:$("#property4").val(),type:$("#property3").val(),os:$("#property6").val() } ,function(data){
     $("#change").html(data);
    });
        });
        $("#property3").change(function() {
                    $.post("mobile_change.php",{ brand:$("#mobile_brand").val(),price:$("#property4").val(),type:$("#property3").val(),os:$("#property6").val() } ,function(data){
     $("#change").html(data);
    });
        });
        $("#property4").change(function() {
                    $.post("mobile_change.php",{ brand:$("#mobile_brand").val(),price:$("#property4").val(),type:$("#property3").val(),os:$("#property6").val() } ,function(data){
     $("#change").html(data);
    });
        });
        $("#property6").change(function() {
                    $.post("mobile_change.php",{ brand:$("#mobile_brand").val(),price:$("#property4").val(),type:$("#property3").val(),os:$("#property6").val() } ,function(data){
     $("#change").html(data);
    });
        });
        $("#sort").change(function() {
                    $.post("mobile_change.php",{ brand:$("#mobile_brand").val(),price:$("#property4").val(),type:$("#property3").val(),os:$("#property6").val(),sort:$("#sort").val() } ,function(data){
     $("#change").html(data);
    });
        });
        $("#sort_order").change(function() {
       $.post("mobile_change.php",{ brand:$("#mobile_brand").val(),price:$("#property4").val(),type:$("#property3").val(),os:$("#property6").val(),sort:$("#sort").val(),sort_order:$("#sort_order").val() } ,function(data){
     $("#change").html(data);
    });
        });
    });
    </script>
    
    </head>
    <body>
    <div id="layout">
    <br/>
    <?php include_once('menu.php');
    ?>
    <h2>Mobiles</h2>
    <?php
    $sql1="select distinct(property1) from mobiles";
    $result1=mysql_query($sql1);
    $myresult = "";
    $myresult .= "<form name='' method='POST' action=''>";
    $myresult .= "<select name='mobile_brand' id='mobile_brand'>";
    $myresult .= "<option value=''>Mobile Brand</option";
            while($row1=mysql_fetch_array($result1))
            {       if($mobile_brand == $row1['property1'])
                    {
                            $selected5 = 'selected';
                    }
                    else
                    {
                            $selected5 = "";
                    }
                    $myresult .= "<option '. $selected5 .' value='".$row1['property1']."'>".$row1['property1']."</option";
            }
    $myresult .= "</select>&nbsp;";
    
            $enumFields = getEnumFieldValues('mobiles', 'property4');
            $myresult .=  "<select name='property4' id='property4'>";
            $myresult .= "<option value=''>Price</option";
             foreach($enumFields as $value)
                   {
                    $newvalue = str_replace("'","",$value);
                    if($newvalue == $price)
                    {
         $selected = "selected = 'yes'";
                    }
                    else
                    {
                            $selected = "";
                    }
                      $myresult .= '<option '.$selected .' value='.$value.'>'.str_replace("'","",$value).'</option>';
                    }
            $myresult .= "</select>&nbsp;";
    
    
            $enumFields = getEnumFieldValues('mobiles', 'property3');
            $myresult .=  "<select name='property3'id='property3'>";
            $myresult .= "<option value=''>Type</option";
             foreach($enumFields as $value)
                   {
                    $newvalue1 = str_replace("'","",$value);
                    if($newvalue1 == $type)
                    {
                            $selected1 = "selected = 'yes'";
                    }
                    else
                    {
                            $selected1 = "";
                    }
                      $myresult .= '<option '. $selected1 .' value='.$value.'>'.str_replace("'","",$value).'</option>';
                   }
    $myresult .= "</select>&nbsp;";
    
            $enumFields = getEnumFieldValues('mobiles', 'property6');
            $myresult .= "<option value=''></option";
            $myresult .=  "<select name='property6' id='property6' >";
            $myresult .= "<option value=''>Os</option";
             foreach($enumFields as $value)
                   {
                    $newvalue2 = str_replace("'","",$value);
                    if($newvalue2 == $os)
                    {
                            $selected2 = "selected = 'yes'";
                    }
                    else
                    {
                            $selected2 = "";
     }
                      $myresult.= '<option '.$selected2 .' value='.$value.'>'.str_replace("'","",$value).'</option>';
                   }
    $myresult .= "</select> &nbsp;&nbsp;&nbsp;&nbsp;";
    
            $myresult .= "Sort by : ";
            $sort_array = array('Rating' => 'overall_rating','Type' => 'property3','Price' =>'property4','Os' =>'property6');
            $myresult .=  "<select name='sort' id='sort' >";
            foreach ($sort_array  as $name => $value)
            {
                    if($sort == $value)
                    {
                            $selected6 = "selected";
                    }
                    else
                    {
                            $selected6 = "";
                    }
                    $myresult.= '<option '.$selected6 .' value='.$value.'>'.$name.'</option>';
            }
    $myresult .= "</select>";
    
            $myresult .= "";
            $sorting_array = array('Desc' => 'Desc','Asc' => 'Asc');
            $myresult .=  "<select name='sort_order' id='sort_order' >";
            foreach ($sorting_array  as $name => $value)
            {
                    if($sorting_order == $value)
                    {
                            $selected7 = "selected";
                    }
                    else
                    {
                            $selected7 = "";
                    }
                    $myresult.= '<option '.$selected7 .' value='.$value.'>'.$name.'</option>';
            }
    $myresult .= "</select>";
    
    
    $myresult .= "</form>";
    $sql2 = "select *  from mobile_master";
    $result2 = mysql_query($sql2);
    $myresult .="<div id='change'>";
    
    $perpage = 5;
    $lynx = $html = "";
    $startat = $_REQUEST[page] * $perpage;
    if($build_query  != "")
            {
                    $resultcount = mysql_query("select * from mobiles where $build_query");
            }
            else
            {
                    $resultcount = mysql_query("select * from mobiles");
            }
    $rowcount = mysql_num_rows($resultcount);
    $myresult .= "<center>Your search got $rowcount mobiles</center>";
    $pages = floor(($rowcount + $perpage - 1) / $perpage);
    //echo $rowcount;
    //echo $pages;
    //$pages=$pages-1;
    if($build_query  != "")
            {
                    $sql="select * from mobiles where $build_query order by $sort $sorting_order limit $startat,$perpage";
            }
            else
            {
                    $sql="select * from mobiles order by $sort $sorting_order limit $startat,$perpage";
            }
    //echo $sql;
    $result=mysql_query($sql);
    $i=0;
    while($row=mysql_fetch_array($result)){
    $sqlr="select overall_rating from mobiles where property1='". $row['property1']."' and property2='". $row['property2']."'";
    $resultr=mysql_query($sqlr);
    $rating = mysql_fetch_array($resultr);
        $myresult .= "<div id='top-rated'>";
        $myresult .= "<div class='image'>";
    
    if($row['pic'] != ""){
    $myresult .= "<a href='mobile_rating.php?brand=". $row['property1'] . "&model=".$row['property2'] . "'><img src='picdisplay.php?brand=". $row['property1'] . "&model=".$row['property2'] . "'</img></a>";
    }
    else
    {
    }
            $myresult .= "</div>";
    
            $myresult .= "<div class='rating'>";
    
                    $i++;
                    for($k = 1; $k <= 10; $k++)
                    {
                            if($rating['overall_rating'] == $k)
                            {
                                    $chk ="checked";
                            }
                            else
                            {
                                    $chk = "";
                            }
                            $myresult .= "<input class='star' type='radio' name='rating".$i."' id='rating".$i."' value='". $k ."' $chk title='". $k." out of 10 ' disabled/>&nbsp;&nbsp;";
                    }
            $myresult .= "</div>";
            $myresult .= "<div class='price'>";
            $myresult .= "Price Range : <a href='product_display.php?price=". $row['property4']."'>".$row['property4'] . "</a>";
            $myresult .= "</div>";
            $myresult .= "<div class='Type'>";
            $myresult .= "Type &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <a href='product_display.php?type=". $row['property3']."'>". $row['property3']."</a>";
            $myresult .= "</div>";
            $myresult .= "<div class='os'>";
            $myresult .= "Os  &nbsp;&nbsp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :&nbsp;<a href='product_display.php?os=". $row['property6']."'>". $row['property6']."</a>";
            $myresult .= "</div>";
    
            $myresult .= "<div  class='product-name'>";
            $myresult .= $row['property1']."&nbsp;".$row['property2'];
            $myresult .= "</div>";
            $myresult .= "</div>";
    }
    for ($k=0; $k<$pages; $k++) {
            if ($k != $_REQUEST[page]) {
             $lynx1 .= " <a href='http://192.168.2.213/rating1/product_display.php?page=".$k."&brand=".$_GET['brand']."&type=".$_GET['type']."&os=".$_GET['os']."&price=".$_GET['price']."&sort=".$_GET['sort']."&sort_order=". $_GET['sort_order']."'>".($k+1)."</a>";
            } else {
     $lynx1 .= " <b>--".($k+1)."--</b>";
            }
    }
    $myresult .= "<p align='right'>";
    $myresult .= $lynx1;
    $myresult .= "</p>";
    $myresult .="</div>";
    
    $myresult .="</div>";
    
    echo $myresult;
    ?>
    <br/>
    <br/>
    </body>
    </html>
    mobile_change.p hp
    Code:
    <?php
    require_once('db.php');
    include_once('valid-scripts/validateData.php');
    $_POST=snipExtras($_POST);
    $mobile_brand=$_POST['brand'];
    
    $price=$_POST['price'];
    
    $type=$_POST['type'];
    
    $os=$_POST['os'];
    
    $sort = $_POST['sort'];
    if($sort == "")
     {
            $sort = "overall_rating";
     }
    
    $sort_order = $_POST['sort_order'];
    if($sort_order == "")
     {
            $sort_order = "desc";
     }
    $build_query = "";
            $add = "";
            $append = "";
    if($mobile_brand != "")
    {
            $append = "yes";
            $build_query .= " property1 = '" .$mobile_brand ."'";
    }
    if($price != "")
    {
            if($append =="yes")
            {
            $add = "and";
            }
            $append = "yes";
            $build_query .= " $add property4 = '" .$price ."'";
    }
    
    if($type != "")
    {
            if($append =="yes")
            {
            $add = "and";
            }
            $append = "yes";
            $build_query .= " $add property3 = '" .$type ."'";
    }
    
    if($os != "")
    {
            if($append =="yes")
            {
            $add = "and";
            }
            $build_query .= " $add property6 = '" .$os ."'";
    }
    
    $myresult = "";
    $myresult .= "<script>";
    $myresult .= "$('input[type=radio].star').rating()";
    $myresult .= "</script>";
    
    ;
    $myresult .="<div id='change'>";
    $sql2 = "select *  from mobile_master";
    $result2 = mysql_query($sql2);
    $perpage = 5;
    $lynx = $html = "";
    $startat = $_REQUEST[page] * $perpage;
    if($build_query != ""){
    $resultcount = mysql_query("select * from mobiles where $build_query");
    $rowcount = mysql_num_rows($resultcount);
    $myresult .= "<center>Your search got $rowcount mobiles</center>";
    $pages = floor(($rowcount + $perpage - 1) / $perpage);
    //$pages=$pages-1;
    
    $sql="select * from mobiles where $build_query ";
    $result=mysql_query($sql);
    $count = mysql_num_rows($result);
    
    if($count > 0){
    $sql="select * from mobiles where $build_query order by $sort $sort_order  limit $startat,$perpage";
    $i=0;
    while($row=mysql_fetch_array($result))
    {
            $sqlr="select overall_rating from mobiles where property1='". $row['property1']."' and property2='". $row['property2']."'";
            $resultr=mysql_query($sqlr);
            $rating = mysql_fetch_array($resultr);
            $myresult .= "<div id='top-rated'>";
            $myresult .= "<div class='image'>";
            if($row['pic'] != "")
            {
                    $myresult .= "<a href='mobile_rating.php?brand=". $row['property1'] . "&model=".$row['property2'] . "'><img src='picdisplay.php?brand=". $row['property1'] . "&model=".$row['property2'] . "'</img></a>";
            }
            else
            {
                    $myresult .= "<a href='mobile_rating.php?brand=". $row['property1'] . "&model=".$row['property2'] . "'><img src='images/no-image.gif' width='50' height='70'</img></a>";
            }
            $myresult .="</div>";
    
            $myresult .= "<div class='rating'>";
            $i++;
            for($k = 1; $k <= 10; $k++)
            {
                    if($rating['overall_rating'] == $k)
                    {
                            $chk ="checked";
                    }
                    else
                    {
                            $chk = "";
                    }
                    $myresult .= "<input class='star' type='radio' name='rating".$i."' id='rating".$i."' value='". $k ."' $chk title='". $k." out of 10 ' disabled/>&nbsp;&nbsp;";
            }
            $myresult .="</div>";
            $myresult .= "<div class='price'>";
            $myresult .= "Price Range : <a href='product_display.php?price=". $row['property4']."'>".$row['property4'] . "</a>";
            $myresult .= "</div>";
            $myresult .= "<div class='Type'>";
            $myresult .= "Type &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <a href='product_display.php?type=". $row['property3']."'>". $row['property3']."</a>";
      $myresult .= "</div>";
            $myresult .= "<div class='os'>";
            $myresult .= "Os  &nbsp;&nbsp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :&nbsp;<a href='product_display.php?os=". $row['property6']."'>". $row['property6']."</a>";
            $myresult .= "</div>";
            $myresult .= "<div  class='product-name'>";
            $myresult .= $row['property1']."&nbsp;".$row['property2'];
            $myresult .="</div>";
            $myresult .="</div>";
    
    }
    
    }
    else if($count == 0 && $build_query != ""){
    $myresult .= "<p class='message'>Sorry No Product Yet available for Display </p>";
    }
    
    }
    else if($build_query == ""){
    $resultcount = mysql_query("select * from mobiles");
    $rowcount = mysql_num_rows($resultcount);
    $myresult .= "<center>Your search got $rowcount mobiles</center>";
    $pages = ($rowcount + $perpage - 1) / $perpage;
    $pages=$pages-1;
    $sql="select * from mobiles order by $sort $sort_order limit $startat,$perpage";
    $result=mysql_query($sql);
    $i=0;
    while($row=mysql_fetch_array($result))
    {
            $sqlr="select overall_rating from mobiles where property1='". $row['property1']."' and property2='". $row['property2']."'";
            $resultr=mysql_query($sqlr);
            $rating = mysql_fetch_array($resultr);
    
            $myresult .= "<div id='top-rated'>";
            $myresult .= "<div class='image'>";
    
    
            if($row['pic'] != "")
            {
                    $myresult .= "<a href='mobile_rating.php?brand=". $row['property1'] . "&model=".$row['property2'] . "'><img src='picdisplay.php?brand=". $row['property1'] . "&model=".$row['property2']                                . "'</img></a>";
            }
                                 else
            {
                    $myresult .= "<a href='mobile_rating.php?brand=". $row['property1'] . "&model=".$row['property2'] . "'><img src='images/no-image.gif' width='50' height='70'</img></a>";
            }
            $myresult .= "</div>";
    
            $myresult .= "<div class='rating'>";
                    $i++;
                    $myresult .= "<td>";
                    for($k = 1; $k <= 10; $k++)
                    {
                            if($rating['overall_rating'] == $k)
                            {
                                    $chk ="checked";
                            }
                            else
                            {
                                    $chk = "";
                            }
                            $myresult .= "<input class='star' type='radio' name='rating".$i."' id='rating".$i."' value='". $k ."' $chk title='". $k." out of 10 ' disabled/>&nbsp;&nbsp;";
                    }
            $myresult .= "</div>";
            $myresult .= "<div class='price'>";
            $myresult .= "Price Range : <a href='product_display.php?price=". $row['property4']."'>".$row['property4'] . "</a>";
            $myresult .= "</div>";
            $myresult .= "<div class='Type'>";
            $myresult .= "Type &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <a href='product_display.php?type=". $row['property3']."'>". $row['property3']."</a>";
            $myresult .= "</div>";
            $myresult .= "<div class='os'>";
            $myresult .= "Os  &nbsp;&nbsp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :&nbsp;<a href='product_display.php?os=". $row['property6']."'>". $row['property6']."</a>";
            $myresult .= "</div>";
    
            $myresult .= "<div  class='product-name'>";
            $myresult .= $row['property1']."&nbsp;".$row['property2'];
            $myresult .="</div>";
            $myresult .="</div>";
    
    }
    }
    for ($k=0; $k<$pages; $k++) {
            if ($k != $_REQUEST[page]) {
             $lynx1 .= " <a href='http://192.168.2.213/rating1/product_display.php?page=".$k."&brand=".$_POST['brand']."&type=".$_POST['type']."&os=".$_POST['os']."&price=".$_POST['price']."&sort=".$_POST['sort']."&sort_order=".$_POST['sort_order']."'>".($k+1)."</a>";
    
    //         $lynx1 .= " <a href=http://192.168.2.213/rating1/product_display.php"."?page=$k>".($k+1)."</a>";
            } else {
             $lynx1 .= " <b>--".($k+1)."--</b>";
            }
    }
    $myresult .= "<p align='right'>";
    $myresult .= $lynx1;
    $myresult .= "</p>";
    
    $myresult .="</div>";
    echo $myresult;
    
    ?>
    1
  • Canabeez
    New Member
    • Jul 2009
    • 126

    #2
    Hey, you are using $.post jQuery method and have a lot of $_GET in your php code, you sure that is not the case?

    About coding standards you might want to check out this, or this.

    Comment

    • pradeepjain
      Contributor
      • Jul 2007
      • 563

      #3
      i am new to jquery ..so lots of loose ends in codes are there !!! say when the result form DB is paginated like (1,2,3) when the user clicks on 1 or 2 or 3 . i did not know how to use jquery here so i passed the values through url when paginating and stuff!! so i posted the whole code !!!!

      Comment

      • Canabeez
        New Member
        • Jul 2009
        • 126

        #4
        try using $.get instead $.post or change the $_GET to $_POST

        Comment

        • pradeepjain
          Contributor
          • Jul 2007
          • 563

          #5
          okie!!thanks for the help!!Will look into it !!!

          Comment

          Working...