Jump to content

Notice: Undefined variable:


FUNKAM35

Recommended Posts

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

 

Link to comment
Share on other sites

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 by WebStyles
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.