PHP - Help, With Postcode Splitting Function / Arrays...
Hi
I have a function that takes a postcode and splits it so i can enter into DB into two formats, full postcode and prefix. The function is: function check_form_postcode($postcode) { $postcode = strtoupper(str_replace(chr(32),'',$postcode)); $suffix = substr($postcode,-3,3); $prefix = substr($postcode,0,(strlen($postcode)-3)); if (preg_match('/(^[A-Z]{1,2}[0-9]{1,2}|^[A-Z]{1,2}[0-9]{1}[A-Z]{1})$/',$prefix) && preg_match('/^[0-9]{1}[ABD-HJLNP-UW-Z]{2}$/',$suffix)) { $postcode_syntax_check = 1; } else { $postcode_syntax_check = 0; } if($postcode_syntax_check){ $return = $prefix."::".$suffix; } return($return); } I POST the form data and in the top of the same page I get all of the form data and here is is where things go wrong: if ($b == 'go') { //Gets form data $main_event_name = my_import('main_event_name', 'P', 'TXT'); $main_event_type = my_import('main_event_type', 'P', 'TXT'); $main_event_date = my_import('main_event_date', 'P', 'TXT'); $main_event_city = my_import('main_event_city', 'P', 'TXT'); $postcode = my_import('postcode', 'P', 'TXT'); $main_event_added = my_import('main_event_added', 'P', 'INT'); $main_event_active = my_import('main_event_active', 'P', 'INT'); $postcode_array = check_form_postcode($postcode); $postcode = explode($postcode_array, "::"); $prefix = $postcode[0]; $fullpostcode = $postcode[0]." ".$postcode[1]; $valkey = md5(microtime()); $info = array( "main_event_name"=> $main_event_name, "main_event_type" => $main_event_type, "main_event_date" => $main_event_date, "main_event_city" => $main_event_city, "main_event_active" => $main_event_active, "main_event_added" => $sys['now'], "main_event_pcode" => $fullpostcode, "main_event_pcode_prefix" => $prefix, "eo_id" => $eo['id'] ); $table = $db['main_event']; $userid = my_insert($info, $table); When I enter into the DB all I get is the "::" no data. I know that the POST works as I can enter the data easily without the function going wrong. Thanks for any help, i am new to arrays go easy if I have made an obvious mistake! Cheers... Similar TutorialsHi, I am trying to split a string into an array and then seeing what sort of data it is. eg; I get the following string Quote api=somerandomkeyhere&user=someuser&password=somehashvaluehere&type=somethingelse I need to be able to put this into an array so in this case Code: [Select] myarray[type]=somethingelse myarray[api]=somerandomkeyhere myarray[user]=someuser myarray[password]=somehashvaluehere but it could include many more/less different value's separated by the & symbol. Thanks, -mme Hey guys,
I have an array of names sorted alphabetically. I'm trying to loop through them and create new arrays keyed by the letters of the alphabet containing the names which start with that letter.
so in the end i'm trying to create something like this:
$newarray = array( 'a' => array('anna','alan','andrew'),
'b' => array('bert','ben','bob'), 'c' => array('cait','carry'), etc,etc ); thanks hey guys just a quick onei having a blank moment i have an array at themoment pulling from a database. i basically want to split this array based on a min and max value. divide it equally and then give it a value on each smaller array and where each array starts... to make it clearer - say i have an array with 600 values in i want it split up into 8 smaller arrays link so Array ( => Array ( [start] => 0 [split] => 75 ) [1] => Array ( [start] => 75 [split] => 75 ) [2] => Array ( [start] => 150 [split] => 75 ) carrying on till the end ) how would i do this. I would like to be able to use a succession of php preg_match() and preg_replace() functions in one script without having to produce clutter in this match and replace php coding script and without its results showing arrayed forms. The following is a RegExp formula that I'm comfortable with that involves only a replace function. I'm curious what the best way would be to scoot a RegExp match function into a script like this one that is consistent with the same $current .= preg_replace('/a/', 'b', $current); php code that I've shown here et. al. that can be written out on one string. I've seen some php implode() functions, but my concern is that an additional function as such along with preg_mach() functions will cause a lot of deviation in a list of regular regex match and replace functions. What can I do to keep both php match and replace RegExp functions strings typed out in linear concession for the sake of keeping things organized when strictly relating to regex functions and working with them within php and working something like an implode() function into my php script so that match and replace functions can produced organized results outside of having to make use of arrays? <?php $file = 'sample.txt'; $current = file_get_contents($file); $current .= preg_replace('/a/', 'b', $current); file_put_contents($file, $current); ?> Edited May 6, 2020 by mojobadshah Okay I have a function that stores data into an Array. The function takes about 7 seconds to run and through a number of different loops it creates one final array with about 15,000 keys. I want to recall this data a number of times in different functions, however how can I have this data easily accessible without running the function each time. EX: Code: [Select] function theFunction() { for($x=0;$x,=15000;$x++) { //Run the loop and store data. $string[$x] = //output from other loops and calculations } return $string } //Then later on if I want to recall the data the only way I know how is to do the follow: $newstring = theFunction() The only problem I have with this is that it has to re-run the function every time in order to get to the data it spits out. How can I store this data into another array outside of the function without having to re-run it? I hope this makes sense. Thanks. Hello. I have a quick question. In my database I have Business's and members. I want the business to be able to advertise to members in their local area (lets say within 30 miles) for a cost. When a member within that distance of the office logs on they see this advert. Outside this distance they do not. Obviously the best way to do this is by postcode of the business compared to the member. I'm basically after some advise as I have been all over the internet looking for answers. First of all is it possible to write this script and what type of functions do I need to learn to do this? Second of all does anybody know of software that can do this out there already? I have found some but they are actual written programs with their own GUI that can not be changed to suit my needs. Thank you Hi, I have this code but i cant get it to work. I have broken it down to sections and I think the get file contents may be at fault as when i dump the array it come back bool false. The URL creates a page with CSV values of longitude and latitude from a postcode. I want to give the variables $custlat1 and $custlong1 the values from the geocoded co-ordinates. The CSV values returned from the URL input are 200,5,54.1202442,-3.2078272 Code: [Select] $pc1 = 'http://maps.google.com/maps/geo?q=la139hu,+UK&output=csv&sensor=false&key=ABQIAAAAcclaxnepdvvxx5D2PAnHtRSLcuoGw6G6HnB4VN4WqoMz7tmtKhTHGV82iKKTXvAg7YFpCFA6ptRc9g'; $data1 = @file_get_contents($pc1); $result1 = explode(",", $data1); $custlat1 = $result1[2]; $custlong1 = $result1[3]; can anyone help? or point me in the right direction Hi All, Long long long time lurker here!
A little background to understand how this fits in So I am building a local directory for my local area as part of a community project. Part of this there is the ability for local stores to sell online locally for people to have delivered or collect. So in the UK we have postcodes in the formats: AB12 3CD, A1 2BC A12 3CD, A1B 2CD
Most delivery pricing solutions only care about the full postcode as they are all about national delivery or at best only care about the first half. Due to the local nature we need more granularity to it so we have some rules: Delivery Available EH => Price EH3 => Price EH3 1 => Price EH3 1-4 => Price
Exceptions - No Delivery allowed EH => null EH3 => null EH3 1 => null EH3 1-4 => null
Can mix and match for example:
EH3 = £2.00 EH3 2 => £2.25 EH3 5 => null
This means that ALL EH3 address the delivery cost is £2 BUT If they are in EH3 2 then its £2.25 or if the are in EH3 5 then no delivery is possible.
I have came up with this monstrosity of code that for the most part works but also can throw the wrong delivery prices out due to bugs and issues that I can't seem to work out!
Main Function: function isDeliverable($postcode, $rules){ $canDeliver = false; $deliveryValue = 0.00; $found = false; list($outward, $inward) = explode(' ', $postcode); $area = substr($outward, 0, 2); $district = substr($outward, 2); $sector = substr($inward, 0, 1); $unit = substr($inward, 1,1); $rulez = json_decode($rules, true); //RULE START - EH10 9RJ $pcFound = inRule($rulez, $postcode); if($pcFound){ return ['canDeliver' => $pcFound['deliverable'], 'deliveryValue' => $pcFound['price']]; } //RULE END - EH10 9RJ //RULE START - EH10 9R $pcFound = inRule($rulez, $area.$district.' '.$sector.$unit); if($pcFound && !strpos($pcFound['postcode'], "-")){ return ['canDeliver' => $pcFound['deliverable'], 'deliveryValue' => $pcFound['price']]; } //RULE END - EH10 9R //RULE START - EH10 9A-F $pcFound = inRule($rulez, $area.$district.' '.$sector.$unit, 1); if($pcFound){ $postArray = postcodeExploder($pcFound, 1); $pcFound = inRule($postArray, $area.$district.' '.$sector.$unit); if($pcFound){ return ['canDeliver' => $pcFound['deliverable'], 'deliveryValue' => $pcFound['price']]; } } //RULE END - EH10 9A-F //RULE START - EH10 9 $pcFound = inRule($rulez, $area.$district.' '.$sector); if($pcFound && !strpos($pcFound['postcode'], "-")){ return ['canDeliver' => $pcFound['deliverable'], 'deliveryValue' => $pcFound['price']]; } //RULE END - EH10 9 //RULE START - EH10 1-4 $pcFound = inRule($rulez, $area.$district.' '.$sector, 2); if($pcFound){ $postArray = postcodeExploder($pcFound, 2); $pcFound = inRule($postArray, $area.$district.' '.$sector); if($pcFound){ return ['canDeliver' => $pcFound['deliverable'], 'deliveryValue' => $pcFound['price']]; } } //RULE END - EH10 1-4 //RULE START - EH10 $pcFound = inRule($rulez, $area.$district); if($pcFound && !strpos($pcFound['postcode'], "-")){ return ['canDeliver' => $pcFound['deliverable'], 'deliveryValue' => $pcFound['price']]; } //RULE END - EH20 //RULE START - EH1-20 $pcFound = inRule($rulez, $area.$district, 3); if($pcFound){ $postArray = postcodeExploder($pcFound, 3); $pcFound = inRule($postArray, $area.$district); if($pcFound){ return ['canDeliver' => $pcFound['deliverable'], 'deliveryValue' => $pcFound['price']]; } } //RULE END - EH1-20 //RULE START - EH $pcFound = inRule($rulez, $area); if($pcFound && !strpos($pcFound['postcode'], "-")){ return ['canDeliver' => $pcFound['deliverable'], 'deliveryValue' => $pcFound['price']]; } //RULE END - EH return ['canDeliver' => $canDeliver, 'deliveryValue' => $deliveryValue]; }
Helper Functions: function inRule($rules, $postcode, $type = null){ foreach($rules as $key => $rule){ if(substr_count($rule['postcode'], '-') !== 0 && strlen($postcode) > 2){ $pEX = postcodeExploder($rule, $type); foreach($pEX as $r){ if($r['postcode'] == $postcode){ return $rules[$key]; } //$rule['postcode'] = trim(substr($rule['postcode'], 0, strpos($rule['postcode'], "-")-1)); } } if ( $rule['postcode'] == $postcode ) return $rules[$key]; } return false; } function postcodeExploder($rule, $type){ $out = []; $r = explode(' ', $rule['postcode']); $count = count($r); //It must be in the form AB1 1C //$first = AB1 //$last = 1C-G if($count == 2){ list($first, $last) = $r; } else { $last = $r[0]; } list($left, $right) = explode('-', $last); $sec = $left[0]; $leftInward = substr($last, strpos($last, '-')-1,1); $rightInward = substr($last, strpos($last, '-')+1,1); $range = range($leftInward, $rightInward); foreach($range as $key => $ra){ if($type == 1){ $out['a'.$key] = [ 'postcode' => $first.' '.$sec.$ra, 'deliverable' => $rule['deliverable'], 'price' => $rule['price'] ]; } else if($type == 2){ $out['a'.$key] = [ 'postcode' => $first.' '.$ra, 'deliverable' => $rule['deliverable'], 'price' => $rule['price'] ]; } else { $out['a'.$key] = [ 'postcode' => preg_replace('/\PL/u', '', $left).$ra, 'deliverable' => $rule['deliverable'], 'price' => $rule['price'] ]; } } return $out; } Stores Rules: $rules = '{"a1":{"postcode":"EH9","deliverable":true,"price":"1.50"},"a2":{"postcode":"EH9 7","deliverable":true,"price":"1.60"},"a3":{"postcode":"EH9 7A","deliverable":true,"price":"1.70"},"a4":{"postcode":"EH9 7AY","deliverable":true,"price":"1.80"},"a5":{"postcode":"EH1-2","deliverable":true,"price":"1.90"},"a6":{"postcode":"EH1 2-3","deliverable":true,"price":"2.00"},"a7":{"postcode":"EH4 5A-N","deliverable":true,"price":"2.10"},"a8":{"postcode":"EH","deliverable":true,"price":"1.40"},"a9":{"postcode":"TD14","deliverable":true,"price":"2.00"},"a10":{"postcode":"TD14 5DC","deliverable":false,"price":null}}';
Hi Guys I need a PHP function to which I pass two postcodes and it works out the road distance, I dont mind using google or any other API I have managed to fing JS ways and php functions using crow route but I really really need a traffic distance in PHP Please advice, Help Thank you Hi, can anyone help me?
I am trying to create a form which asks the user to input their postcode then submit. the purpose is so if the service my site offers is not available in the location that the end user lives, then I want to display a message saying 'Sorry, not available', but if they are in area then it redirects to another page.
My form code is:
<form method="post" action="forwarder.php"> <p> Enter Post Code He <input type = "text" name = "zip" id = "zip" size = "10" maxlength = "5" "> <input type='submit'> </form>My PHP is: <?php $zip = isset ($_POST['zip']) { if ($zip=='NG15') { header('Location: https://www.bing.com'); } else { header('Location: http://www.google.co.uk') } ?>I am a bit of a newbie at this, and I know my PHP won't give me my desired result, but I was using this to try and test the concept. Help would be really appreciated, and if you can show me how to do it for multiple postcodes even better Heya guys! What's up? OK here's my problem: I got a variable, $var1, which is to have values like "groan,100,200,350,50" I need to know how to split this $var1 into $split1, $split2, $split3, $split4, $split5, where $split1 = "groan"; $split2 = "100"; $split3 = "200"; $split4 = "350"; $split5 = "50"; Thanks in advance people! P.S. In my case I am going to use some of the values for calculations. So I don't think that its easy with arrays, which are difficult for me to understand . P.S.S. When I am retrieving data from the database, many variables like $var1 that need to be split are being extracted. I hope that you understand. Thanks and Regards, Thauwa http://mobsters-fb-apache-dynamic-lb.playdom.com/mob_fb/get_hit_list?user_id=100000952273457&target_id=100000556624340&level=300&auth_key=cb07c7575b38df48d82dd53619385faa884db5a2 When going to this URL, it gives entries between <entry> and </entry>, what I am trying to do, is collect the <user_id> ONLY if the <amount> is over a certain value. Any ideas to how I could do this (I am a complete PHP noob). All help is appreciated I'm having troubling with trying to create a function to spit out a single array with the following array. I can write it in a away that looks through the arrays manually. the results i am trying to generate is that each item in generated array. Array to convert array( "account" => array( "login", "register", "logout", "edit", ), "p" => array( "report", ), "array1.0" => array( "array2.0" => array( "array3.0", "array3.1 ), "array2.1", ), generating the array will look like this
Array ( [0] => account [1] => account/login [2] => account/register [3] => account/logout [4] => account/edit [5] => p [6] => p/report [7] => array1.0 [8] => array1.0/array2.0 [9] => array1.0/array2.0/array3.0 [10] => array1.0/array2.0/array3.1 [11] => array1.0/array2.1 ) The idea is that id generates a single array with combined labels and arrays inside, etc. I just can't figure out how to create a script that will create this array even If I add a new value or array.
I have this thing that i am trying to make but i cant get it to work.. can anyone help? Code: [Select] function sql_read( $dbname,$dbusername,$dbpassword ) { $names = array(); $password = array(); $connect = @mysql_connect("mysql11.000webhost.com",$dbusername,$dbpassword) or die("Could Not Connect"); @mysql_select_db ($dbname) or die("Could not find DataBase"); $query = mysql_query("select * from users"); $numrows = mysql_num_rows($query); if ($numrows > 0){ while($row = mysql_fetch_assoc($query)){ $names[] = $row["uname"]; $password[] = $row["password"]; $id = $row["id"]; } $return = array($names,$password,$id); }else{ $return = array(); } return $return[]; } $names = array(); $names = sql_read("XXXXXX","XXXXXX","XXXXXXX")[0]; The error i get is Code: [Select] Parse error: syntax error, unexpected '[' in /home/a5480952/public_html/sql/index.php on line 28 Line 28 is "$names = sql_read("XXXXXX","XXXXXX","XXXXXXX")[0];" Please help... i REALLLLD need help with this.. ask questions if you want to know more about what i am trying to do... thanks! I have a string like this: Code: [Select] Something,Somthing Else,"keyword1,keyword2,keyword3",Yet another I need to split that string by the ,. However, I need the data that is n the quotes to remain as one even though it has commas because it is one item. I assume I need to use preg_split and have tried some code but none of it seems to work properly. If anyone has a better method or knows how I can split this up using preg_split the help is appreciated. I could use some suggestions on how to break up my HTML and PHP. Currently I have a file called "payment_form.php" It looks like this... Code: [Select] <body> <?php // HANDLE FORM. if (isset($_POST['submitted'])){ // Handle Form. // Determine if any errors. if (empty($errors)){ // Process Payment. // PHP code to send payment to Payment Gateway... // Do not return to Payment Form!!! exit(); } } ?> <!-- HTML PAYMENT FORM --> <form id="payment" action="" method="post"> <fieldset> <legend>Billing Details</legend> <ol> <!-- First Name --> <li> <label for="firstName">First Name:</label> <input id="firstName" name="firstName" class="text" type="text" maxlength="20" value="<?php echo $firstName; ?>" /> <?php if (!empty($errors['firstName'])){ echo '<span class="error">' . $errors['firstName'] . '</span>'; } ?> </li> I would like to keep the Form Validation PHP in the same file as the HTML Form so the user isn't ping-ponged between two pages if they make data entry errors. However, I am thinking it would be better to break out the Payment Processing PHP and place that in a different file. How should I do that? Maybe I could add a Redirect after this code... Code: [Select] if (empty($errors)){ // Process Payment. // PHP code to send payment to Payment Gateway... Suggestions welcome!! Thanks, Debbie I have a text field that contains the following (example text used): 1 2 3 4 5 6 As you can see the first few are split by a single space, with the last few numbers split by a double space. My aim is to put each of these numbers into a table in a MYSQL database. But I'm having trouble breaking them up. Hi guys Say for example I have a text field with value 20/10/2011 how do I define it so that $x=20, $y=10 and $z=2011 so that I have three variables Thanks Hi Guys, does anyone know how split a text into groups depending on how many words are in the text e.g $text = "If you are new to PHP and want to get some idea of how it works, try the introductory tutorial. "; I want a maximium of 5 words in each group, so the above text will be split as such $text1 = 'If you are new to'; $text2 = 'PHP and want to get'; $text3 = 'some idea of how it '; $text4 = 'works, try the introductory tutorial.'; But if I want a maximium of 10 words in each group, so the above text will then be split like this $text1 = 'If you are new to PHP and want to get'; $text2 = 'some idea of how it works, try the introductory tutorial.'; $text3 = ''; $text4 = ''; Is this possible? Please any help will be much appreciated thanks Okay I need a little bit of help on this as strings constantly confuse me and reading tutorials actually got me a bit more loss then before. I have developed this mod that has become pretty popular and I'm debugging it. What I have are two variables. $username (The user's username, no html markup in it) $musername (The user's username, with html markup in it) So for example: $musername = <b><u>$username</u></b> The php code above would be the equivolant to what $musername is equal to. What I need to do is create an array or two variables, whatever is easier and grab all that html markup. The first variable would be all the html or anything really before the $username variable, and the other would take all of that html after the $username variable. I need to store those html markups in either two seperate variables or an array so i can call upon it later in the script. I need this to fix a bug issue. |