FUNKAM35 Posted February 26, 2014 Share Posted February 26, 2014 <?php echo"<div class=\"styled-select\"> <form id=\"formorder\" action=\"$_SERVER[PHP_SELF]?country=$country\" method=\"get\"> <select name=\"order_by\" onChange=\"UpdateQty(this)\" > <option value=\"sort by\" selected=\"selected\">Sort by</option> <option value=\"rate3asc\">Lowest Price First</option> <option value=\"rate3desc\">Highest Price First</option> <option value=\"newest\">Recently Added</option> <option value=\"bedsasc\">Beds (Low to High)</option> <option value=\"bedsdesc\">Beds (High to Low)</option> </select> <input type=\"hidden\" name=\"country\" value=\"$country\"> <input type=\"submit\" name=\"submit\" id=\"button\" value=\"go\"> </form></div>\n"; if($order_by=="rate3asc"){ echo"Sorted by Lowest Price First"; } if($order_by=="rate3desc"){ echo"Sorted by Highest Price First"; } if($order_by=="newest"){ echo"Sorted by Recently Added Properties First"; } if($order_by=="bedsasc"){ echo"Sorted by Least Bedrooms First"; } if($order_by=="bedsdesc"){ echo"Sorted by Most Bedrooms First"; } ?> Notice: Undefined variable: order_by in /home/public_html/holidayrentals/order.php on line 15 Notice: Undefined variable: order_by in /home/public_html/holidayrentals/order.php on line 18 Notice: Undefined variable: order_by in /home/public_html/holidayrentals/order.php on line 21 Notice: Undefined variable: order_by in /home/public_html/holidayrentals/order.php on line 24 Notice: Undefined variable: order_by in /home/public_html/holidayrentals/order.php on line 27 Hi I am getting the notices undefined variable from the posted code, it works fine but would like to eliminate any error messages many thanks Quote Link to comment Share on other sites More sharing options...
jairathnem Posted February 26, 2014 Share Posted February 26, 2014 As the error states you donot have $order_by defined anywhere in the script.You are trying to check the value, when it doesnt hold any. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted February 26, 2014 Share Posted February 26, 2014 (edited) if the purpose is just eliminating the notices (and the script can run without the existence of that variable) then you can use something like: if( isset($order_by) && $order_by == "rate3asc" ){ instead of if( $order_by == "rate3asc" ){ But that would be redundant because you would be checking if the variable existed several times. You could wrap the whole thing in an if statement so you check just once: <?php if( isset($order_by) ){ if($order_by=="rate3asc"){ echo"Sorted by Lowest Price First"; } if($order_by=="rate3desc"){ echo"Sorted by Highest Price First"; } if($order_by=="newest"){ echo"Sorted by Recently Added Properties First"; } if($order_by=="bedsasc"){ echo"Sorted by Least Bedrooms First"; } if($order_by=="bedsdesc"){ echo"Sorted by Most Bedrooms First"; } } ?> But it's still not the best way. You could also use a switch statement, or even something like this (I would do this last one, as it allows to easily add or remove stuff from the array, and makes it easy to see all the available options): <?php $sortOrder = array( "rate3asc" => "Lowest Price First", "rate3desc" => "Highest Price First", "newest" => "Recently Added Properties First", "bedsasc" => "Least Bedrooms First", "bedsdesc" => "Most Bedrooms First" ); if( isset($order_by) ){ echo 'Sorted by '.$sortOrder[$order_by]; } ?> Hope this helps Edited February 26, 2014 by WebStyles Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.