Hello. I need some help understanding if/else and variables in PHP.
I have an index.php file that uses (probably common code)
if(is_null($_GET["page"])) {
$page = "#";
}else{
$page = $_GET["page"];
}
to access all other pages. Works great, no problems.
My pages use jqGrid, a jquery API to render SQL databases in an excel like view. It works wonderfully. The problem I am having is with passing around _GET variables. The problem with this jqGrid is you need to set a URL variable that is the same as the page it is found in. So you might have a "units" page called "units.php" and you need to set the variable:
$grid->setUrl('units.php');
So even though you loaded from index.php, now you've set the URL to units.php.
This really screws you over if you want to allow users to generate links to pass GET variables. Even though the GET variables will be passed around, because you are re-setting the URL when you load that page, it essentially becomes useless (why? I don't know..). To make up for that I made this code which works:
Imagine the user clicked a link such as website.com/index.php?page=units.php&UnitsVal=1234
$grid->setUrl('units.php'); // Set the URL as required by the API
$value1 = $_GET['UnitsVal']; // GET the UnitsVal if it was passed in a link
$grid->setUrl('units.php?val='. $value1 .''); //Change the URL to include the new values
$value2 = $_GET['val']; // Get the value from the new URL
$grid->SelectCommand = 'SELECT id, Serial, Type FROM inventory WHERE Serial = "'. $value2 .'"';
For some reason, I cannot simply pass $value1 to the selectCommand, even though it will echo perfectly. Why? I have no idea.
So fine. What I wanted to do next was say, hey, if there was no UnitsVal passed in a URL just load all the units:
$grid->setUrl('units.php'); // Set the URL as required by the API
$value1 = $_GET['UnitsVal']; // GET the UnitsVal if it was passed in a link
$grid->setUrl('units.php?val='. $value1 .''); //Change the URL to include the new values
$value2 = $_GET['val']; // Get the value from the new URL
if (!empty($value2)){
$grid->SelectCommand = 'SELECT id, Serial, Type FROM inventory WHERE Serial = "'. $value2 .'"';
}else{
$grid->SelectCommand = 'SELECT id, Serial, Type FROM inventory';
}
And guess what, it works! Except it shouldn't. If I remove the contents of else, and just replace it with let's say, break;, then it breaks! How can it be running both if and else at the same time, but load the contents of (if), but not work if I remove (else)!?!?!
How how how!?!
Appreciate any help. Thanks.