Jump to content

Too many if isset statements, better way to handle?


flambo

Recommended Posts

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.

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.

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"] : "";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.