810 Posted August 15, 2009 Share Posted August 15, 2009 Hi I am a newbie to PHP. i am having a problem to add a hyperlink on the street name. i am creating an overview off the incidents for a local Fire Department. I have a few problems or questions. [*]1. Data in the hyperlink doesn't working. ($straat) [*]2. I want only the link at the street name here can you see my mysql table. and you can see here the calls http://www.joomlawebdesigns.nl/p2000/enkhuizen2008.html Is it passible to do it. the street name is in the "melding" the third word (always in BOLD), after prio 1 streetname or prio 2 streetname ANY HELP WOULD BE MUCH APRECIATED. Thanks a lot regards Jelle <?php //-- maak eerst de connectie met de database! $user = "*"; $pass = "*"; $host = "*"; $dbdb = "*"; if (!mysql_select_db($dbdb, mysql_connect($host, $user, $pass))) { echo "Kan geen verbinding maken met de database."; exit(); } $sql = "SELECT id,timestamp,capcode,melding,label FROM alarmeringen ORDER BY id DESC"; error_reporting(0); $res = mysql_query($sql); if (mysql_num_rows($res) >= 1) { $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_array($result)) { $row['timestamp'] = substr($row['timestamp'], 0, 30); // Timestamp is datum en tijd van de melding en melding is de melding bv: Prio 2 BRW Kleine inzet SCHOONMAKEN WEGDEK preg_match('/[A-Z]+.[0-9]+.[0-9]{4}[A-Z]{2}.:.[A-Z]+/',$row['melding'],$matches); if (count($matches) != 0) { $data = explode(' ',str_replace(':','',$matches[0])); $straat = $data[0]; $stad = $data[4]; } $output = str_replace($matches[0],$matches[0].'</a>',$row['melding']) ; echo $row['timestamp'] . " " . $output . " " . '<a href=http://maps.google.nl/maps?f=q&hl=nl&geocode=&q='.$straat.'%20'. 'ENKHUIZEN%20NETHERLANDS'. ">"; echo "<br><br>" ; } } else { echo "ER ZIJN GEEN MELDINGEN BESCHIKBAAR VOOR BRANDWEER ENKHUIZEN."; } ?> Quote Link to comment Share on other sites More sharing options...
810 Posted August 19, 2009 Author Share Posted August 19, 2009 Nobody? ps you can see here the url. http://www.newf1.nl/p2000/googlemaps.php Quote Link to comment Share on other sites More sharing options...
Catfish Posted August 19, 2009 Share Posted August 19, 2009 check your values in $data array. i think your preg_match is not working as you expect. use: print_r($data) to output all the values you get in $data. From a quick glance, I think your data formatting in the melding field in the database table is not very strict. You say that the street names are always in the 3 place (third word) and are in BOLD (uppercase) but that is not what I'm seeing on the picture. I don't know what your regexp is doing (whether its looking for the 3rd word or for uppercase letters) so I can't really help there. Debug the $data and check your preg_match is working. Also, I looked at the source of your page and you are not constructing the <a> tags correctly. There is not even a single </a> tag in there! You need something like: $output = '<a href="http://somewhere.com/q=".$phpValue."&etc=blahBlahBlah">'.$phpStreetName.'</a><br />'; Quote Link to comment Share on other sites More sharing options...
810 Posted August 19, 2009 Author Share Posted August 19, 2009 When i add the </a> the hyperlinks isn't showing anymore. the </a> is in the code.: $output = str_replace($matches[0],$matches[0].'</a>',$row['melding']) ; And where must i add print_r($data Quote Link to comment Share on other sites More sharing options...
Catfish Posted August 19, 2009 Share Posted August 19, 2009 <?php $data = explode(' ',str_replace(':','',$matches[0])); print_r($data); $output = str_replace($matches[0],$matches[0].'</a>',$row['melding']) ; echo $row['timestamp'] . " " . $output . " " . '<a href=http://maps.google.nl/maps?f=q&hl=nl&geocode=&q='.$straat.'%20'. 'ENKHUIZEN%20NETHERLANDS'. ">"; ?> the $output = line is very wrong. theres a , instead of . after '</a>'. Quote Link to comment Share on other sites More sharing options...
Catfish Posted August 19, 2009 Share Posted August 19, 2009 sorry i made a mistake there. (had a screaming baby on my lap at the time.) the str_replace call is working as is, but you are doing this: creating $output to be: $matches[0].'</a>' so i assume you have something like: "Content from melding field</a>" in $output (I don't know exactly what is in $matches[0]) then you are using echo to output: Timestamp Content from medling field</a> <a href="http://maps.blah.blah/&q=$straat+etc"><br><br> you really want to echo something like: <a href="http://maps.blah.blah/&q=$straat+etc">Timestamp Content from medling field</a><br /> and also, if you want to make your output source code a lot more readable, you can place some \n to create newlines: <a href="http://maps.blah.blah/&q=$straat+etc">Timestamp Content from medling field</a><br />\n I also tried: <?php $string = "Prio 1 Verzorgingstehuis Overvest LIJSTERBESSTRAAT 4 ENKHUIZEN Rookmelder ENK645"; preg_match('/[A-Z]+.[0-9]+.[0-9]{4}[A-Z]{2}.:.[A-Z]+/',$string,$matches); print_r($matches); ?> and it outputs an empty array. I don't know what your preg_match regexp is doing but i dont think its working. Quote Link to comment Share on other sites More sharing options...
Catfish Posted August 19, 2009 Share Posted August 19, 2009 Ok, I know I am multi posting but I think I may have something to fix it for you. <?php $string = "2009-08-15 18:58:38 Prio 1 (NB=: intrekken_alarmering) MOLENWEG 48 ENKHUIZEN Binnenbrand"; preg_match('/(.*\s)([A-Z]*)(\s[0-9]*\sENKHUIZEN)/', $string, $matches); $preText = $matches[1]; $streetName = $matches[2]; $postText = $matches[3]; $output = '<a href="http://maps.google.nl/maps?f=q&hl=nl&geocode=&q='.$streetName.'%20ENKHUIZEN%20NETHERLANDS">'.$string.'</a><br />'."\n"; print_r($matches); print("\n\n".'<br />'); print($output); ?> $string is a value from database melding field I got from your page source. The preg_match is doing this: (.*\s) <- look for any number of any character up to a whitespace ([A-Z]*) <- look for any number of upper case characters (\s[0-9]*\sENKHUIZEN) <- look for "whitespace, any number of digit characters, whitespace, the string 'ENKHUIZEN'" in $string (value from melding) and put matches in $matches it then outputs all the values in an anchor link to google maps. I tried it on my server and it works for me. The only thing is the regexp expects the word "ENKHUIZEN" in the melding field's string, otherwise it won't find a street name. I noticed some of your data doesn't include that... i'll leave you to work on that I guess. Hopefully this will get you on your way. And if you want to look up the street number on google maps as well, you just change the regexp to: preg_match('/(.*\s)([A-Z]*\s[0-9]*)(\sENKHUIZEN)/', $string, $matches); Quote Link to comment Share on other sites More sharing options...
810 Posted August 19, 2009 Author Share Posted August 19, 2009 Thanks, almost done, how do i remove the array text. and how add i the other strings. because now i see the same strings. thanks alot Quote Link to comment Share on other sites More sharing options...
Catfish Posted August 19, 2009 Share Posted August 19, 2009 to remove the array output you can just comment out the print_r lines or remove them completely. adding the other strings to the output can be done by using your other variables, like $row['timestamp'] etc. Quote Link to comment Share on other sites More sharing options...
810 Posted August 19, 2009 Author Share Posted August 19, 2009 Ok, thanks. it works. But only 2 questions left, how to add "new window " and we have just a car fire at donkerstraat with no number is it passible to change the code if there is no number it go to the street. Thanks for your help. Quote Link to comment Share on other sites More sharing options...
Catfish Posted August 20, 2009 Share Posted August 20, 2009 if you want the links to open in a new window, do: <a href="http://www.somewhere.com/" target="_blank">Link Text</a> also check out www.w3schools.org for more HTML etc. stuff. Dropping to street only if there is no number is harder to do, it changes the regexp values. I have to go away for the weekend now, but i can quickly suggest to you to check the data that the preg_match finds, if it doesn't suit your needs, you can make your code try a different regexp to find, for example, when there is no number in the address. I wont be back until Sunday night (3 days) though so I can't help you more until then. Quote Link to comment Share on other sites More sharing options...
810 Posted August 22, 2009 Author Share Posted August 22, 2009 Thank you vey much, its working. i edit the code and it working perfect. i changed the code to: $string = $row['timestamp']." ".$row['melding']; preg_match('/(.*\s)([A-Z]+.[0-9]*)(\s.[A-Z]*\s)/', $string, $matches); $preText = $matches[1]; $streetName = $matches[2]; $postText = $matches[4]; $city = $matches[3]; $output = '<a target="_blank" href="http://maps.google.nl/maps?f=q&hl=nl&geocode=&q='.$streetName.'%20'.$city.'%20NETHERLANDS">'.$string.'</a><br />'."\n"; print("\n\n".'<br />'); print($output); 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.