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...)

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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;
	}

Link to comment
Share on other sites

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

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.