Jump to content

[SOLVED] Combining $_GET and $_POST statement


DBookatay

Recommended Posts

Some of my forms use $_POST data, and some of my site relies on links ($_GET) and all point to one page where the functions are completed. Is there a way to combine the 2, instead of having this:

	if ($_GET['trans'] == "Auto") {
		$trans_type = "Automatic"; 
		$where[] = "trans_type = 'Automatic' or trans_type = 'Tiptronic'";
		$trnsLnk = "&trans=Auto";
	} elseif ($_GET['trans'] == "Manual") {
		$trans_type = "Manual"; 
		$where[] = "trans_type = 'Manual'";
		$trnsLnk = "&trans=Manual";
	}
	if ($_POST['trans'] == "Auto") {
		$trans_type = "Automatic"; 
		$where[] = "trans_type = 'Automatic' or trans_type = 'Tiptronic'";
		$trnsLnk = "&trans=Auto";
	} elseif ($_POST['trans'] == "Manual") {
		$trans_type = "Manual"; 
		$where[] = "trans_type = 'Manual'";
		$trnsLnk = "&trans=Manual";
	}

for each $_GET and $_POST (about 30 all togather...)

You could use the $_REQUEST array, which is the combination of $_GET, $_POST, and $_COOKIE.

 

You could write a function that returns the value of which ever is set and use the returned value in the "if" statments:

<?php
function whichway($key) {
    $val = '';
    if (isset($_GET[$key])) $val = $_GET[$key];
    if (isset($_POST[$key]) && $val == '') $val = $_POST[$key];
    return($val);
}

switch (whichway('trans')) {
    case 'Auto':
         $trans_type = "Automatic"; 
         $where[] = "trans_type = 'Automatic' or trans_type = 'Tiptronic'";
         $trnsLnk = "&trans=Auto";
         break;
    case 'Manual':
         $trans_type = "Manual"; 
         $where[] = "trans_type = 'Manual'";
         $trnsLnk = "&trans=Manual";
         break;
}
?>

 

Ken

 

 

So, do this:

	function whichway($key) {
		$val = '';
		if (isset($_GET[$key])) $val = $_GET[$key];
		if (isset($_POST[$key]) && $val == '') $val = $_POST[$key];
		return($val);
	}

	switch (whichway('type')) {
		case 'Plow':
			$pTitle = 'Search Results: Plow Trucks';
			$where[] = "feature_SP = 'x'";
			$typeLnk = "&type={$_GET['type']}";
		break;

		case 'Hybrid':
			$pTitle = 'Search Results: Hybrid Vehicles';
			$where[] = "fuel = 'Hybrid'";
			$typeLnk = "&type={$_GET['type']}";
		break;
	}
	switch (whichway('trans')) {
		case 'Auto':
			$where[] = "trans_type = 'Automatic' or trans_type = 'Tiptronic'";
			$trnsLnk = "&trans=Auto";
		break;

	    case 'Manual':
			$where[] = "trans_type = 'Manual'";
			$trnsLnk = "&trans=Manual";
		break;
	}

Almost, except where ever you used $_GET or $_POST explicitly, you need to use whichway():

<?php
	switch (whichway('type')) {
		case 'Plow':
			$pTitle = 'Search Results: Plow Trucks';
			$where[] = "feature_SP = 'x'";
			$typeLnk = "&type=" . whichway('type');
		break;

		case 'Hybrid':
			$pTitle = 'Search Results: Hybrid Vehicles';
			$where[] = "fuel = 'Hybrid'";
			$typeLnk = "&type=" . whichway('type')";
		break;
?>

 

Ken

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.