DBookatay Posted February 8, 2008 Share Posted February 8, 2008 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...) Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 8, 2008 Share Posted February 8, 2008 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 Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted February 8, 2008 Share Posted February 8, 2008 $_REQUEST can get slopy as a GET var could overwrite a POST var and the values could become contaimenated Quote Link to comment Share on other sites More sharing options...
DBookatay Posted February 8, 2008 Author Share Posted February 8, 2008 What about adding other clauses, line $_POST['body'] or $_GET['body']? How do I add it to this: switch (whichway('trans')) { Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 8, 2008 Share Posted February 8, 2008 The function I created returns the value of whichever $_POST or $_GET is set, so you could use it like <?php $body = whichway('body'); ?> Ken Quote Link to comment Share on other sites More sharing options...
DBookatay Posted February 8, 2008 Author Share Posted February 8, 2008 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; } Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 8, 2008 Share Posted February 8, 2008 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 Quote Link to comment 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.