Orderby in href not working?

Discussion in 'PHP' started by ICKY74, Feb 1, 2008.

  1. #1
    I'm new to php and had to pick up another person's development :eek: Anyway, I have pasted what I think to be correct but it won't work. I am trying to get the hyperlink to sort the page by horse name but I will be adding other sorting options later after this problem is solved. Can someone help?



    //check to see if an order has been set and passed as a variable
    $orderby = $_GET['orderby'];
    if($orderby == NULL){
    $orderby = "horseName";
    }

    $order = $_GET['order'];
    if($order == NULL){
    $order = "ASC";
    }


    //get all the show horses
    //show horses have a 'horseGender' of 'c', 'f' or 'g' AND are 3 year old or over

    my_connect();

    //work out the year that horseYOB must be greater than
    $minimum_year = date("Y") - 3;

    $query = "SELECT * FROM `horses` WHERE (`horseGender` = 'c' OR `horseGender` = 'f' OR `horseGender` = 'g') AND `horseYOB` <= '$minimum_year' AND `show_me` = '1' ORDER BY CAST(replace(horseSalePrice, '$','') as unsigned int) desc";

    //printf("Query is: ".$query);

    $result = my_query($query);

    //get the number of horses which are for show
    $num_rows = mysql_num_rows($result);

    //set the 'previous_letter' variable for the first time round the loop
    $previous_letter_show = "";

    ?>


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE></TITLE>
    <META NAME="Generator" CONTENT="EditPlus">
    <META NAME="Author" CONTENT="">
    <META NAME="Keywords" CONTENT="">
    <META NAME="Description" CONTENT="">
    <link href="../br_style.css" type="text/css" rel="stylesheet" />
    <link href="../print.css" type="text/css" rel="stylesheet" media="print">

    </HEAD>

    <h4>Show Horses<div id="print"><a href="showrosterprint.php" target="_blank"><img src="images/printer1.gif" BORDER="0"><br>
    <span class="style1">Print</span></a></div></h4>

    <span class="smalltext11">Sort by:</span>
    <a href="tab_show.php?orderby=horseName&order=ASC"><img src="images/sort_up.gif" border="0"></a><a href="tab_show.php?orderby=horseName&order=ASC">Name</a>
    <a href="tab_show.php?orderby=horseName&order=DESC"><img src="images/sort_down.gif" border="0"></a>
    <!--</h1><a href="tab_show.php?orderby=horseYOB&order=ASC"><img src="images/sort_up.gif" border="0"></a><a href="tab_show.php?orderby=horseYOB&order=ASC">Age</a>
    <a href="tab_show.php?orderby=horseYOB&order=DESC"><img src="images/sort_down.gif" border="0"></a>
    <a href="tab_show.php?orderby=horseGender&order=ASC"><img src="images/sort_up.gif" border="0"></a><a href="tab_show.php?orderby=horseGender&order=ASC">Sex</a>
    <a href="tab_show.php?orderby=horseGender&order=DESC"><img src="images/sort_down.gif" border="0"></a>-->


     
    ICKY74, Feb 1, 2008 IP
  2. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You are not using the $orderby and $order in your query.

    OT: stating that your script "won't work" isn't very useful. You should explain what exactly is going wrong (post error messages, describe output/behaviour that isn't what you want). That way it's easier to find the problem.
     
    CreativeClans, Feb 1, 2008 IP
  3. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #3
    Thereshould be a link like this yourpage.php?order=horsename

    than use this code
     
    $orderby = $_GET['orderby'];
    if(isset($orderby)){// if order by value i set 
    $orderby = $orderby;
    }
    else{
    $orderby = $default_order;
    }
    
    }
    PHP:
    and remove this code
    
    //check to see if an order has been set and passed as a variable
    $orderby = $_GET['orderby'];
    if($orderby == NULL){
    $orderby = "horseName";
    }
    
    $order = $_GET['order'];
    if($order == NULL){
    $order = "ASC";
    }
    
    PHP:
    Most important use $order variable in your query which you are not doing at the moment
     
    greatlogix, Feb 1, 2008 IP
  4. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    $defaultorderby = "horseName";
    if(isset($_GET['orderby'])) { // if order by value is set
      $orderby = $_GET['orderby'];
    } else {
      $orderby = $default_orderby;
    }
    
    $defaultorder = "ASC";
    if(isset($_GET['order'])) { // if order value is set
      $order = $_GET['order'];
    } else {
      $order = $default_order;
    }
    
    PHP:
     
    CreativeClans, Feb 1, 2008 IP