PHP - Finding The Right Array Number From A Zip Code
I have build a small form that take the zip code that user enter and if is on my list will redirect to a sub domain craigslist.
I was checking all the zip codes and is pretty hard to know to what bigger city to attribute the given zip code and for that I was thinking to a simple idea to use, I am not sure how much exact will be but I think will do the trick for the moment.
The zip code range start from 00501 to 99950.
I create a array with the biggest city shown in craigslist and the zip code that correspond.
Now the tricky part, what I will do with the rest of the zip codes and my idea is to measure the distance from one to another zip code and which zip code from the array is closer from given one to take it.
<?php $zip = $_GET['zip']; $z = array ( 36830 => array ( 'auburn.craigslist.org' ), 35242 => array ( 'bham.craigslist.org' ), 36301 => array ( 'dothan.craigslist.org' ), 35630 => array ( 'shoals.craigslist.org' ), 35901 => array ( 'gadsden.craigslist.org' ) ?> Exemple: $zip= 36870 // the code received from the user 36870 - $array= ( 'auburn.craigslist.org' ), (36830 = 40) (36301 = 569) (36911 = -41) // the minus will be excluded (....etc)After checking the zip code with the ones from array, the smaller score will attribute the sub-domain correspond from the array. I hope will don t be the case when will get the same result from two zip codes:) Now I am using this rule: $zip0 = (isset($z[$zip][0]) && $z[$zip][0] != '') ? $z[$zip][0] : 'craigslist.org/about/sites#US';and I was thinking to redirect the user for a page to enter the state and the city. But I wish to make it more precises and not to involved the user so much. But if there are any other suggestions I will be glad to hear them. Similar TutorialsHi folks, i have a complex problem (for me anyway) but i am sure with your help this can be sorted. Problem number 1: I have a simple while loop, but its only showing 4 results when i know there are 5 I am missing something very simple i know: $sql = ("SELECT * FROM postcode WHERE unid='1'")or die ('<p>There seems to be a problem, please try again soon.</p>'); $result = mysql_query($sql,$con); $row = @mysql_fetch_array($result); $i=0; while ($row = @mysql_fetch_assoc($result)){ echo "newpoints[". $i++ ."] = new Array({$row['lat']}, {$row['long']}, icon0, '{$row['postcodename']}', '{$row['postcodename']}'); \n"; } What am i missing here? Problem number 2: I have a bunch of latitude and longitude codes stored in the database. On each page there will be a varied amount of these codes used, but with a max of 20. I need to find the highest number and the lowest number of each lat and long then come up with a code that is in the middle, the idea of this is to center a map. So for example there will be an array like this: 50.852293 -1.76088 51.252938 -0.76128 51.259583 -0.727168 51.274 -0.837 51.123106 -0.970657 First i need to find the highest number of the first code (51.274) then the lowest number (50.852293) and then the highest number of the second code (-1.76088) then the lowest number (-0.727168) Then work out an inbetween code using the highest and lowest codes for each. Does anyone have a clue how to go about this? I am not the worlds best array technition or mathematician I am designing a mmorpg guild site, and decided to make a coordinate database. A user will enter their own coords, x and y, and the 10 closest coords to them will be displayed. I need help doing this. My table is: id coordx coordy alliance region I am using $_GET to retrieve the user's input. so $_GET['mex'] $_GET['mey'] Thanks in advance for your help. I have a row in a MySQL table called id, which auto-increments. I was wondering if anyone knows a PHP solution to find out what the highest number in the row is? I am thinking perhaps execute a MySQL query that will list the highest number, and then echo the result, like $row[id] in PHP? I am trying to locate an array in an array of arrays:
$card_pos = array_search($target, $_SESSION['leitner']['boxes'][($box - 1)]); if ($card_pos !== false) { // Do my stuff here }echo '<pre> oh no -- didn\'t find '; print_r($target); echo "in ";print_r ($_SESSION['leitner']['boxes'][($box + 1)]);echo '<br>leitner';print_r ($_SESSION['leitner']); echo '</pre>';but I don't find the target in the array though it is clearly there as you can see in the output: oh no -- didn't find Array ( [pos] => v5g [tense] => agerimasu ) in Array ( [0] => Array ( [pos] => v5g [tense] => agerimasu ) ) leitnerArray ( [boxes] => Array ( [1] => Array ( ) [2] => Array ( ) [3] => Array ( [0] => Array ( [pos] => v5g [tense] => agaru ) [1] => Array ( [pos] => v5g [tense] => agarimasu ) [2] => Array ( [pos] => v5g [tense] => agarimasen ) [3] => Array ( [pos] => v5g [tense] => agaranai ) [4] => Array ( [pos] => v5g [tense] => ageru ) [5] => Array ( [pos] => v5g [tense] => ageranai ) [6] => Array ( [pos] => v5g [tense] => agerimasen ) [7] => Array ( [pos] => v5g [tense] => agerimasend ) [8] => Array ( [pos] => v5g [tense] => agereba ) [9] => Array ( [pos] => v5g [tense] => agareba ) [10] => Array ( [pos] => v5g [tense] => agatte ) [11] => Array ( [pos] => v5g [tense] => agette ) ) [4] => Array ( [0] => Array ( [pos] => v5g [tense] => agerimasu ) ) [5] => Array ( ) ) [denom_total] => Array ( [1] => 0 [2] => 0 [3] => 36 [4] => 40 [5] => 40 ) [denom_max] => 40 ) Good day, Is there a simple way to find and list duplicates i an array and insert these to a new one? I have: $array[0] = "asd" $array[1] = "ter" $array[2] = "asd" $array[3] = "asd" $array[4] = "xfh" From that, I need this: $newarray[0] = "0" $newarray[1] = "2" $newarray[2] = "3" I have poked around "array_not_unique" and "in_array" a bit, but can't wrap my head around it enough for it to work... Any and all help are much appreciated. Thanks in advance! Hi i have two arrays, activity and feed.. contains 100 arrays, $activity = array(0,1,2....,100); contains 5 arrays $feed = array(100,50,25,12,75); I have 7 ranges, (0 -15),(15-30),(30-45),...(90-105) now i need to compare feed array with activity array to find in which range it was... Example, $feed[0] = 100, then it is in (90-105) range $feed[3] = 12, then it is in (0-15) range.. ... how to find this range?? please help me.. Hi PHPfreaks, I've got a problem Ive been faced with a few days now, and for the life of me Im unable to solve it. What I have is a multidimensional array, that I need to parse and retreive every possible outcome possible Below is a basic example of how the array would look Code: [Select] Colour Black White Blue Finish Matt Gloss What I need to be able to do is, as I said, Show all the possible combinations there is. below is what i'd have achieved after one of you helpful souls points me in the right direction. Code: [Select] --> Color: Black --> Color: White --> Color: Blue --> Finish: Matt --> Finish: Matt --> Color: Black, Finish: Matt --> Color: White, Finish: Matt --> Color: Blue, Finish: Matt --> Color: Black, Finish: Gloss --> Color: White, Finish: Gloss --> Color: Blue, Finish: Gloss As you can see, Only ONE (or no) value is picked from each section. Just to confirm: the number of option groups WILL change...... So I could have colour, finish, size, weight and many more. the number of values in the groups WILL change....... so under colour, I could also have pink, green, indigo, gray. Below is how this array is structured Code: [Select] Array ( [0] => Array ( [parent] => Colour [values] => Array ( [0] => Array ( [name] => Black ) [1] => Array ( [name] => White ) ) ) [1] => Array ( [parent] => Finish [values] => Array ( [0] => Array ( [name] => Matt ) [1] => Array ( [name] => Gloss ) ) ) ) Would anyone have any suggestion on how this can be done? I've pulled out every single strand of hair I've got now, so thought i'd admit defeat and let someone else have a go Many thanks inadvance. Hi, I have a Comma Delimited File called "houses.txt" with the contents of: HA11QS, 200, house1.jpg, 4 HA22BR, 280, house2.jpg, 10 HA33AB, 390, house3.jpg, 3 HA44CD, 320, house4.jpg, 8 I have a web form "form2.html": Code: [Select] <html> <head> <title>Untitled Document</title> </head> <body> <form action="any.php" method="post"> Please Enter Anything <input type="text" name="any"> <input type="submit" value="Submit"> </form> </body> </html> and PHP code of "any.php": <?php if (isset($_POST['any'])) { $filename = "houses.txt"; $fileOpen = fopen($filename, "r"); $max = $_POST['any']; $rowsArr = file ($filename); foreach ($rowsArr as $row) { $lineDetails = $row; $item_array = explode (",", $row); if (in_array($max,$item_array)) { echo("Post Code - " . $item_array[0]. "<br>"); echo("Price - " . $item_array[1]. ",000 <br>"); echo("Picture - " . $item_array[2]. "<br>"); echo("Number of Visits - " . $item_array[3]. "<br>"); echo("<br>"); } } fclose($fileOpen); } ?> What i need is for the user to input anything they wish for example: 4, which would search the array and find that the first house has had 4 visits or HA44CD, to find the last house on the list etc ... however unfortunatley its not working for, if anyone can help me i would grateful Thank You hi there all i need the code that adds the url to a page like in the new forums cheers.
you get a image of that website and the url of the page
can anyone help please
thank you
like if i type www,google.com <<< you get a image and link automaticly..
Edited by php-real-degree, 15 September 2014 - 05:07 PM. I am having a go at writing my own simple gallery. As the I want to display an image from a folder in a directory of all the images... for example, I have 10 folders each containing images. I want my code to take image 01.jpg out of each folder and display it on my page, so that when I add folders to my main images folder, they will automatically be added to the page... but I only want the last 10 folders to display... so once I have 20 folders, it only shows 10. I also want the images to be 5 across and then make a new row. My folders will be numbered in order... 001, 002, 003, etc... and I need them sorted in reverse order, so the folder added last is displayed first. So... this is where I am at with the code : Code: [Select] $dir = "gallery/"; if (is_dir($dir)) { [color=red] foreach(array_keys($file) as $n) {[/color] if ($n+1 == 5) { echo "<td><img src=\"" . $dir . $file . "/01.jpg\" width=\"140px\" height=\"93px\" alt=\"\" /></td></tr><tr>"; } elseif ($n+1 > 10) { echo ""; } else { echo "<td><img src=\"" . $dir . $file . "/01.jpg\" width=\"140px\" height=\"93px\" alt=\"\" /></td>"; } closedir($dh); } } For some reason, I am getting the following error on the page : Warning: First argument to array_keys() should be an array in /path/to/gallery.php on line 8 Warning: Invalid argument supplied for foreach() in /path/to/gallery.php on line 8 line 8 is the one in red above. So... I have a couple of questions... 1. What have I done wrong so far? I cannot see it 2. How would I then go about putting the galleries in reverse order? I know it must be rsort, but not to sure if I need to do this first or after... ? Any help greatly appreciated, and I would also be greatful f an idiot's explanation, as I am really trying to understand all this! I'm getting there... slowly! Does anyone know a way of to find the 'coordinates' of a deeply nested key in a multidimensional array whilst using array_walk_recursive? I am using array_walk_recursive() to stripslashes() of all values of an array. However I need to be able to get the full coordinates of a key if the said key is of a certain value. The function key() only seems to get the highest level key. As a test I have the following array which has three occurrences of a key named 'date'. Upon each occurrence I want to be able to know exactly where in the array it is so i can call is directly later. <?php $pages = array(array("variation"=>"xccsdc", "items"=>array(array("nm"=>"ss", "type"=>"ta", "kkl"=>"Prodion." ), array("nm"=>"nm", "type"=>"nwsfd", "kkl"=>"nws fd", "nws"=>array(array("date"=>"1328982639", "kkl"=>"f nfgfgnfgn", "title"=>"nfgn fgn fhn " ), array("date"=>"1328982632", "kkl"=>"fgn fgn", "title"=>" fgn" ) ) ), array("nm"=>"nm", "type"=>"nwsfd", "kkl"=>"nws fd", "nws"=>array(array("date"=>"1328982655", "kkl"=>" gh fgh","title"=>"n fgh n" ) ) ) ) ) ); function test_print(&$item, $key, $pages) { if($key == 'date') { //strip the slashes $item = stripslashes($item); //if the key is 'date' which is deeply nested, return the key. echo key(current($pages)); echo '<br/>'; } } array_walk_recursive($pages, 'test_print', $pages); ?> What is returned from the 'test_print' function is: Quote variation variation variation Is this because array_walk_recursive() does not move the internal pointer pointer along? I found this thread on stackoverflow: http://stackoverflow.com/questions/3133054/php-find-key-position-in-multidimensional-array which has a great function for finding the key, however i'm struggling to get my head around how I can apply something like this whilst using array_walk_recursive(). Does anyone have any ideas? I have a list of IP addresses in an very very very large array in random order. I am trying to find a way to determine if there are at least 8 IP addresses that are consecutive. I was thinking some sort of for loop after a sort but that seems very time consuming and I'm sure PHP has a better way... Some searching online revealed this: http://bytes.com/topic/php/answers/12143-flagging-consecutive-numbers-data-set Is this the way to go? Any tips? Thanks! Carmen I am trying to insert into my database and it's just not having non of it. No errors are comming up at all, but no data is getting passed through. Code: [Select] if(isset($_POST['sub'])) { include("../scrips/php/cms/database.insert.class.php"); $table = "blog_posts"; $title = $_POST['ArticleTitle']; $img = "2"; $post = $_POST['ArticleBody']; $aurthor_id = "1"; $category_id = "4"; $date_posted = "NOW()"; $values = array("$title","$img","$post","$aurthor_id","$category_id","$date_posted"); $fields = array('title','img','post','aurthor_id','category_id','date_posted'); //echo $values['0']."<br/>".$fields['0']; //$obj->ArticleInsert($table,$fields,$tvalues); $values_imploded = implode("','",$values); $fields_imploded = implode(",",$fields); $i = "INSERT INTO $table ($values_imploded) VALUES ($fields_imploded)"; mysql_query($i) or die( "<br>Query string: <br>Produced error: " . mysql_error() );; } I'm getting the dreaded " Invalid parameter number: number of bound variables does not match number of tokens" error and I've looked at this for days. Here is what my table looks like:
| id | int(4) | NO | PRI | NULL | auto_increment | | user_id | int(4) | NO | | NULL | | | recipient | varchar(30) | NO | | NULL | | | subject | varchar(25) | YES | | NULL | | | cc_email | varchar(30) | YES | | NULL | | | reply | varchar(20) | YES | | NULL | | | location | varchar(50) | YES | | NULL | | | stationery | varchar(40) | YES | | NULL | | | ink_color | varchar(12) | YES | | NULL | | | fontchosen | varchar(30) | YES | | NULL | | | message | varchar(500) | NO | | NULL | | | attachment | varchar(40) | YES | | NULL | | | messageDate | datetime | YES | | NULL |Here are my params: $params = array( ':user_id' => $userid, ':recipient' => $this->message_vars['recipient'], ':subject' => $this->message_vars['subject'], ':cc_email' => $this->message_vars['cc_email'], ':reply' => $this->message_vars['reply'], ':location' => $this->message_vars['location'], ':stationery' => $this->message_vars['stationery'], ':ink_color' => $this->message_vars['ink_color'], ':fontchosen' => $this->message_vars['fontchosen'], ':message' => $messageInput, ':attachment' => $this->message_vars['attachment'], ':messageDate' => $date );Here is my sql: $sql = "INSERT INTO messages (user_id,recipient, subject, cc_email, reply, location,stationery, ink_color, fontchosen, message,attachment) VALUES( $userid, :recipient, :subject, :cc_email, :reply, :location, :stationery, :ink_color, :fontchosen, $messageInput, :attachment, $date);"; And lastly, here is how I am calling it: $dbh = parent::$dbh; $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); if (empty($dbh)) return false; $stmt = $dbh->prepare($sql); $stmt->execute($params) or die(print_r($stmt->errorInfo(), true)); if (!$stmt) { print_r($dbh->errorInfo()); }I know my userid is valid and and the date is set above (I've echo'd these out to make sure). Since the id is auto_increment, I do not put that in my sql (though I've tried that too), nor in my params (tried that too). What am I missing? I feel certain it is something small, but I have spent days checking commas, semi-colons and spelling. Can anyone see what I'm doing wrong? I have these numbers in an array: 14042 11130 10100 10100 9506 How can I add them all together? Was thinking about a while loop. Derp. >array_sum solved! Hello, i retrieve an array from the database that looks something like this: $var=a:1:{s:2:"cc";a:1:{i:22;s:11:"TESTING";}}; I need to retrieve the number corresponding to the character i: and print it(11 in the first case). For example , If, $var=a:1:{s:2:"cc";a:1:{i:15;s:11:"TESTING";}}; I would need to print 15. $var=a:1:{s:2:"cc";a:1:{i:3;s:11:"TESTING";}}; I would need to print 3. Thanks in advance. Hi! I need a little bit coding help here. I have a table: Numbers, Active Under numbers can be 10000, 10001, 10002, 10003, 10004, 10005 and active can be 1 or 0, depends on is this row active or not. Now, i need to check, is there any numbers lost, i mean, (for example) in table i have numbers 10002, 10000 and 10005 Can somebody make script, what finds those lost numbers, in this case those a 10001, 10003 and 10004. All the best: Mart Hey folks! Happy what ever you are celebrating I am trying to limit the number of characters to 200 that this array echos. I have tried a few things... I have tried using "substr($arr_tem, 0, 200)" but obviously it is not working... but I think I am close-ish. Any assistance would be great! Code: [Select] ###print array### echo (implode(", ", array_keys($arr_tem))); Thanks! Suppose I have a series of numbers being stored in an SQL table, which are not uniform and constantly growing. Once the numbers in the table (in sql) hit 99,999,999, they start back at 1, and do it all over again continuously until the end of time.
If I am performing calculations in PHP on these numbers (stop date-start date), is there a function that can take a look at the next cell after 99,999,999 and acknowledge that the 1 is really 100,000,000.
In the below example, instead of using 99,999,999, we used 4 as the last number before numerical reset. Once the numbers hit 4, they reset to 1. continuously.
Example1. If I were to subtract Jan 1(start) from jan 4(stop) I would have Stopdate -Startdate = 4-1 =3.
Example2. If I were to subtract Jan 4(start) from jan 7(stop) I would have Stopdate-Startdate = 3-4 =-1 but should be 7-4 for the sake of the calculation only.
Example3. If I were to subtract Jan 8(start) from jan 11(stop), I would have Stopdate-Startdate = 3-4=-1 but should be 11-8=3 but for ease of not having to be a continuous repeating thing, can be represented at 7-4. There is no need to keep a running memory towards infinity. It just needs to know that any number after some number ending in 4 is 5, instead of reseting to 1.
jan 1 2014.......1
jan 2 2014.......2
jan 3 2014.......3
jan 4 2014.......4
jan 5 2014.......1
jan 6 2014.......2
jan 7 2014.......3
jan 8 2014.......4
jan 9 2014.......1
jan10 2014......2
jan11 2014......3
Hope this is not that confusing.
Long story short, I can't do a stopdate-startdate = somenumber, if that some number keeps resetting to 1 once it reached 99,999,999. As a result, my code will work perfectly until I reach 99,999,999, then I am in trouble because 1-99,999,999 is not equal to 1, where 100,000,000 -99,999,999 = 1
thanks.
Edited by Butterbean, 24 October 2014 - 02:24 PM. |