flambo Posted February 23, 2012 Share Posted February 23, 2012 Hi, New to coding so pls bear with me.... I am running into a continual issue that wonder if there is a better way to handle it. When writing code that handles GET variables that have not been set I use the isset function with if statements to handle it, but my code looks like it is getting sloppy and I am having to repeat it a lot - for example: Every time I simply want to call a class / function that I want to pass avGET i have to do the following: if (isset($_GET["filter"])){$theFilter = $pageination->filter($_GET["filter"]);} else {$theFilter = $pageination->filter();} Here is a copy of the class I have created (note sure if it works yet as not fully tested it) class pageination { private $recordLimit=100; public function filter ($filter="") { if ($filter!=""){ $theFilter=" AND ".$filter; return $theFilter; } else { return $theFilter=""; } } private function pageCnt ($pageCntRst) { $pageCnt=intval($results[$pageCntRst]); return $pageCnt; } public function pageNmLnk () { echo 'Page: <a href="?pageStartListings=0&filter='. $_GET['filter'].'">1</a>'; } //This loops through every 100 pages public function pageNmLoop ($pageCnt) { for ($n=1; $n<=$pageCnt; $n++){ if ($n > $recordLimit) { break; } if ($n*$recordLimit==intval($_GET["pageStartListings"])){ $stylePg = 'style="border: 1px solid #aaaaaa; background-color: #CCCCCC; padding:3px; "> ' . $n+1 . '</a>'; } else{ $stylePg = ''; } echo '| <a href="?pageStartListings=' . $n*$recordLimit . '&filter=' . rawurlencode($_GET["filter"]) . ' " ' . $stylePg; } } } $pageination = new pageination (); ?> I use the GET a few times in the code and I have to write a lot of code around the isset and if statements to handle it all the time - thinking there must be a better way to do this or a function that can be built to test it? Any help is grateful in advance. Quote Link to comment https://forums.phpfreaks.com/topic/257613-too-many-if-isset-statements-better-way-to-handle/ Share on other sites More sharing options...
blacknight Posted February 23, 2012 Share Posted February 23, 2012 isset is the built in function try defining the $_GET vars at the start of the function using shorter names... Quote Link to comment https://forums.phpfreaks.com/topic/257613-too-many-if-isset-statements-better-way-to-handle/#findComment-1320377 Share on other sites More sharing options...
cyberRobot Posted February 23, 2012 Share Posted February 23, 2012 Instead of duplicating the call to the filter method, you could change the code to: <?php $_GET['filter'] = (isset($_GET['filter'])) ? $_GET['filter'] : ''; $theFilter = $pageination->filter($_GET['filter']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/257613-too-many-if-isset-statements-better-way-to-handle/#findComment-1320385 Share on other sites More sharing options...
flambo Posted February 23, 2012 Author Share Posted February 23, 2012 Hi, Thank you for replying. I was about to post the below in response to the first reply but the second reply looks like a much better way to do it - thanks! Sorry if the answer seems obvious - but I have interpreted what you have said, which seems obvious to me now, to do define / test the $_GET var as a variable first - that way I can replace all the $_GET['filter'] with the variable I have just set, that way I will no longer get an error that $_GET['filter'] is not set, and so have to test it at every point. Can do something like the following if (isset($_GET["filter"])){$filter = $_GET["filter"];} else {$filter = "";} that way I can always use $filter without getting an error. Would that be right? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/257613-too-many-if-isset-statements-better-way-to-handle/#findComment-1320392 Share on other sites More sharing options...
cyberRobot Posted February 23, 2012 Share Posted February 23, 2012 Can do something like the following if (isset($_GET["filter"])){$filter = $_GET["filter"];} else {$filter = "";} that way I can always use $filter without getting an error. Would that be right? Yep, after running the above, $filter will always be set. Note that the above code is the same as: $filter = (isset($_GET["filter"])) ? $_GET["filter"] : ""; Quote Link to comment https://forums.phpfreaks.com/topic/257613-too-many-if-isset-statements-better-way-to-handle/#findComment-1320394 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.