scottybwoy Posted December 18, 2006 Share Posted December 18, 2006 Hi BillysI am trying to make an assosiative array of $_GET / $_POST values, is this poss. May it be possible to get the complete url and extract the $k = $v and put them into an associative array? Could someonr point me in the right direction of functions to use, or if my method asked above is viable. Thanx Quote Link to comment https://forums.phpfreaks.com/topic/31100-solved-get-assoc-array-and-validation/ Share on other sites More sharing options...
HuggieBear Posted December 18, 2006 Share Posted December 18, 2006 $_GET and $_POST are associative arrays. You can use them as they are.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31100-solved-get-assoc-array-and-validation/#findComment-143593 Share on other sites More sharing options...
redbullmarky Posted December 18, 2006 Share Posted December 18, 2006 unless i'm missing the point - they already ARE associative arrays... Quote Link to comment https://forums.phpfreaks.com/topic/31100-solved-get-assoc-array-and-validation/#findComment-143594 Share on other sites More sharing options...
HuggieBear Posted December 18, 2006 Share Posted December 18, 2006 You can assign them to a different array if you want...[code=php:0]$myAssocGetArray = $_GET;[/code] RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31100-solved-get-assoc-array-and-validation/#findComment-143595 Share on other sites More sharing options...
scottybwoy Posted December 19, 2006 Author Share Posted December 19, 2006 Thanks Fellas,Sorry I was very tired yesterday, had 4hrs sleep over the w/e! and was also busy at work. Just letting you know cos with a fresh head this morning, I noticed that I had confused myself by looking at a wrong function that I had already completed and tried to make it do something else, lol, sorry for taking your time.However let me go on to what I am actually trying to do. I want to create server side validation for my form. I already have client side validation via JavaScript. Am I going the right way about it, cos it seems a bit long, here's what I have so far :[code]<?php function insertCust($data_array) { global $company, $custId, $data_array, $template; $company = $data_array['company']; foreach ($data_array as $v) { //take out whitespace str_replace('/^\s+|\s+$/g', '', $v); } if (empty($company)) { echo "You forgot to insert customer name!"; $template = CUST_PAGE; break; } else if (!empty($company)) { $data_array['custId'] = $this->makeCustId($company); } if (empty($data_array['tel'])) { echo "You need to insert at least a main telephone number!"; $data_array['custId'] = $this->makeCustId($company); $template = CUST_PAGE; break; } else if (!empty($data_array['tel'])) { ('/^\+?[0-9 ()-]+[0-9]$/'); } if (empty($data_array['addLine1'])) { echo "You need to insert at least a building name and street!"; $data_array['custId'] = $this->makeCustId($company); $template = CUST_PAGE; break; } if (empty($data_array['postcode'])) { echo "C'mon how are we meant to know where they are without a postcode?"; $data_array['custId'] = $this->makeCustId($company); $template = CUST_PAGE; break; } if (empty($data_array['type'])) { echo "It'll make your life a whole lot easier if we know what type of company they are, don't you think?"; $data_array['custId'] = $this->makeCustId($company); $template = CUST_PAGE; break; } if (empty($data_array['pay'])) { echo "Now how we gonna pay you if they don't pay us?"; $data_array['custId'] = $this->makeCustId($company); $template = CUST_PAGE; break; } foreach ($data_array as $k => $v) { $_GET[$k]; } } function addClient() { global $clientId, $custId, $data_array, $template; $custId = $_GET['custId']; $company = $_GET['company']; if (empty($company) && empty($custId)) { echo "Dying"; die("You forgot to select a company!"); } else { $this->makeClientId($custId); $data_array = array("company" => $company, "clientId" => $clientId); $template = CLIENT_PAGE; } }?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31100-solved-get-assoc-array-and-validation/#findComment-144616 Share on other sites More sharing options...
scottybwoy Posted December 20, 2006 Author Share Posted December 20, 2006 Hmm, I tidied this up a bit today, but it's still rather long and it doesn't work, when calling it, it just returns with the enties back in it and no validation messages at all. Here's the validation code (not quite complete) :[code]<?php function insertCust($data_array) { global $company, $custId, $data_array, $template; foreach ($data_array as $k => $v) { //take out trailing whitespace str_replace('/^\s+|\s+$/g', '', $v); if (empty($data_array[$k])) { $validate[$k] = FALSE; } else { $validate[$k] = TRUE; } return $validate; } if ($validate['company'] == FALSE) { $invalid = array(1 => "You forgot to insert customer name!"); } else if ($validate['company'] == TRUE) { $company = $data_array['company']; $data_array['custId'] = $this->makeCustId($company); } if ($validate['tel'] == FALSE) { $invalid = array("You need to insert at least a main telephone number!"); } else if ($validate['tel'] == TRUE) { $email = $data_array['email']; preg_match('/^\+?[0-9 ()-]+[0-9]$/', $email); } if ($validate['addLine1'] == FALSE) { $invalid = array("You need to insert at least a building name and street!"); } else if ($validate['addLine1'] == TRUE) { $addLine1 = preg_replace('|[^a-z0-9 ]|i', '', $addLine1); } if ($validate['postcode'] == FALSE) { $invalid = array("C'mon how are we meant to know where they are without a postcode?"); } if ($validate['type'] == FALSE) { $invalid = array("It'll make your life a whole lot easier if we know what type of company they are, don't you think?"); } elseif ($validate['type'] == TRUE) { $type = $data_array['type']; $valid = $this->recordCheck('cusType', 'tpName', $type); if ($valid == FALSE) { $invalid = array("Sorry you have selected an incorect Type of Customer"); } } if ($validate['pay'] == FALSE) { $invalid = array("Now how we gonna pay you if they don't pay us?"); } elseif ($validate['pay'] == TRUE) { $type = $data_array['pay']; $valid = $this->recordCheck('cusPay', 'pyName', $type); if ($valid == FALSE) { $invalid = array("Sorry you have selected an incorect Payment Type for Customer"); } } if (isset($invalid)) { print_r($invalid); $template = CUST_PAGE; } else { //insert $data_array into table echo "info is correct"; $template = CUST_PAGE; } }?>[/code]Thanks for taking a look ;) Quote Link to comment https://forums.phpfreaks.com/topic/31100-solved-get-assoc-array-and-validation/#findComment-145102 Share on other sites More sharing options...
Orio Posted December 20, 2006 Share Posted December 20, 2006 Inside your first foreach loop, you got "return $validate;". This will cause the function to stop working a return an array. I think you miss-placed it, it doesnt need be there at all.Orio. Quote Link to comment https://forums.phpfreaks.com/topic/31100-solved-get-assoc-array-and-validation/#findComment-145105 Share on other sites More sharing options...
scottybwoy Posted December 20, 2006 Author Share Posted December 20, 2006 Thanks Orio, that got me allot further. Now how would I get my $invalid array to display a list of stuff that failed? At the mo, it just displays the last failed entryThanks again Quote Link to comment https://forums.phpfreaks.com/topic/31100-solved-get-assoc-array-and-validation/#findComment-145141 Share on other sites More sharing options...
Orio Posted December 20, 2006 Share Posted December 20, 2006 1) At the begining of the function, write $invalid=array();2) Instead of writing every time: "$invalid = array("Some error here");", write "$invalid[] = "Some error here";".3) Instead of "if (isset($invalid))" write "if (empty($invalid))".I think everything will be fine this way :)Orio. Quote Link to comment https://forums.phpfreaks.com/topic/31100-solved-get-assoc-array-and-validation/#findComment-145146 Share on other sites More sharing options...
scottybwoy Posted December 20, 2006 Author Share Posted December 20, 2006 Thanks Orio, got it sorted now, is validation usually that long? Quote Link to comment https://forums.phpfreaks.com/topic/31100-solved-get-assoc-array-and-validation/#findComment-145165 Share on other sites More sharing options...
Orio Posted December 20, 2006 Share Posted December 20, 2006 Depends how deeply you do it. But it doesnt really matter how long it is- what's most important is that it works well, and the code is simple and clear (then it's easier to fix/upgrade etc').Orio. Quote Link to comment https://forums.phpfreaks.com/topic/31100-solved-get-assoc-array-and-validation/#findComment-145172 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.