Jump to content

Andy-H

Members
  • Posts

    2,000
  • Joined

  • Last visited

  • Days Won

    1

Everything posted by Andy-H

  1. lol fair enough, to be honest it's just some crap that I read and assumed to be true, but we all know what they say about that word. Yeah, last time I used the DOM I had to use the error suppression operator for the first time in about 4 years of coding with the loadHTML method.
  2. I know this was sarcasm, but @OP that would be pretty in-efficient. http://uk3.php.net/DOMDocument would be your best bet, google it and you'll find plenty of tutorials.
  3. http://www.webresourcesdepot.com/dynamic-dragn-drop-with-jquery-and-php/
  4. Let's find out: var_dump(ENT_COMPAT); var_dump(ENT_QUOTES); var_dump(ENT_NOQUOTES); var_dump(ENT_HTML401); var_dump(ENT_XML1); var_dump(ENT_XHTML); var_dump(ENT_HTML5); // output int(2) int(3) int(0) int(0) int(16) int(32) int(48) DOUBLE QUOTES SINGLE QUOTES ENT_NOQUOTES = 0 0 ENT_COMPAT = 0 1 ENT_QUOTES = 1 1 ENT_HTML401 = 0000 00 ENT_XML1 = 0000 10 ENT_XHTML = 0000 01 ENT_HTML5 = 0000 11 Judging from that I think I was wrong with what I previously said, it looks possible that you should only use one quotes flag and one DOCTYPE flag per call.
  5. Is your background symmetrical? If it is use the css repeat-y property, this is a common problem, most of the time it's overcome by using a tiles background with a repeat, or a gradient background with a repeat-x and a background colour (yes, we invented the language, that's how it's spelt) of the bottom-most part of the gradient. Failing that you could set overflow:auto; and use a scrollable div when necessary.
  6. Yes it will work, just looked and the documentation (html_entity_decode) states that the flags create a bitmask (see Wikipedia), this works as I previously explained.
  7. I'm pretty sure you can use as many as you want, I think it works like this: define('ENT_QUOTES', 1); // binary 10000 define('ENT_HTML5', 2); // binary 01000 define('ENT_HTML401', 4); // binary 00100 define('ENT_XML1', ; // binary 00010 define('ENT_XHTML', 16); // binary 00001 Then when you bitwise or them, it results in: ENT_QUOTES (1) | (10000) ENT_HTML5 (2) | (01000) (11000) ENT_HTML401 (4) | (00100) (11100) ENT_XML1 ( | (00010) (11110) ENT_XHTML (16) | (00001) (11111) Then in the PHP core: int flags = arg[1]; if ( flags & 1 ) // ENT_QUOTES ON if ( flags & 2 ) // ENT_HTML5 ON if ( flags & 4 ) // ENT_HTML401 ON if ( flags & 8 ) // ENT_XML1 ON if ( flags & 16 ) // ENT_XHTML ON Hope that helps
  8. I assume you're using google chart data, why not just have the column set to string type, then if there's missing data have the integer (measure) field replicate the month before but have the month (column) data say "11 - No data" So your data would look like this: Month Year Measure 5 2010 164 6 2010 31 7 2010 20 8 2010 10 9 2010 10 10 2010 10 11 No data 2010 10 12 2010 10 1 2011 10
  9. As I said numerous times, changing your function to: function check_email_address($email) { return (bool)filter_var($email, FILTER_VALIDATE_EMAIL); } would have sufficed.
  10. That is way above my level of understanding at the moment Now I have if (!preg_match("/^[^@]{1,64}@[^@]{1,255}$\", $email)) { // Email invalid because wrong number of characters // in one section or wrong number of @ symbols. return false; } // Split it into sections to make life easier $email_array = explode("@", $email); $local_array = explode(".", $email_array[0]); for ($i = 0; $i < sizeof($local_array); $i++) { if (!preg_match("/^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%& ↪'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$\", $local_array[$i])) { return false; } } And I get error "Parse error: syntax error, unexpected '@'" in line 7 of the code posted above It's way easier than regular expressions: if ( filter_var($email, FILTER_VALIDATE_EMAIL) === false ) // email invalid Judging from your code (the return statements) it's the body of a function: function email_is_valid($email) { return (bool)filter_var($email, FILTER_VALIDATE_EMAIL); }
  11. Check out filter_var with the FILTER_VALIDATE_EMAIL/FILTER_VALIDATE_URL flags.
  12. Solved (insert ignore)
  13. Just wondering, when inserting data that could be a possible duplicate into a unique field, do I need to check if the key already exists or will mysql handle that for me without error?
  14. Why don't you just use the auto-increment field? The data is already there.
  15. http://uk3.php.net/manual/en/class.splfileobject.php Never mind lol
  16. Just realised that this code really sucks, gonna re-write it later to extend a base class for file handling, will post back when I'm done.
  17. Just to explain myself a little, looking at the code I think I used static because I didn't understand it properly, from my current understanding static will be consistent through all instances of the object, my previous (mis)understanding was that static was based on the instance, and just couldn't be updated after it had been set? Since per object is per-csv I though this would prevent the delimiter and escape character from being changed. If this is correct, there's one improvement Also I see that I should change the content member variable to not be static, this means I could update the contents on a write to the file, and maybe implement a way to change the file that it acts upon? Thanks for any help.
  18. I started another topic asking for help with a class I had written and was wondering if anyone would be kind enough to point out bad practices/improvements etc. on this class I wrote quite a while back, any help greatly appreciated. <?php class parseCSV { protected $_fh; protected $_fs; protected $_fn; protected static $_contents; protected static $_delimiter; protected static $_escape; protected $_data = array(); public function __construct($filename, $delimiter = ',', $escape = '\\') { ini_set('auto_detect_line_endings', 1); if ( !file_exists($filename) ) throw new Exception($filename . ' does not exist, please check the filename for typos.'); $fh = fopen($filename, 'r+'); if ( !$fh ) throw new Exception('File ' . $filename . ' could not be read. Please try again.'); $this->_fn = $filename; $this->_fh = $fh; $this->_fs = filesize($filename); self::$_delimiter = $delimiter; self::$_escape = $escape; } public function read() { while( $cols = fgetcsv($this->_fh, $this->_max_line_length(), self::$_delimiter, '"', self::$_escape) ) { $cols = array_filter($cols); if ( empty($cols) ) continue; $this->_data[] = $cols; } } public function write($fields) { $fh = fopen($this->_fn, 'w'); foreach($fields as $field) { fputcsv($fh, $field, self::$_delimiter, '"'); } } public function get_data() { return $this->_data; } protected function _max_line_length() { $this->_get_contents(); $lines = explode("\r\n", self::$_contents); return max(array_map('strlen', $lines)); } protected function _get_enc() { $this->_get_contents(); $enc = mb_detect_encoding(self::$_contents, 'auto', true); return ( $enc != false ) ? false : 'UTF-8'; } protected function _get_contents() { if ( !self::$_contents ) { self::$_contents = fread($this->_fh, $this->_fs); rewind($this->_fh); } } public function close() { fclose($this->_fh); } } ?> Thanks
  19. I think it would be useful to have a favourite topic button and a page where you can view all your favourite topics if possible?
  20. Ahh OK, just because I'm at work, will email it to myself, cheers.
  21. Wow, thanks, I think this OO stuff is finally starting to click (at last lol). I'll implement that into the abstract class, thanks again for you help, is there any way I can "faviroute" a PHP freaks topic for future reference?
  22. Haven't studied OOP in a while, completely forgot that abstract classes even existed - not that I would have know to use one if I hadn't. I just thought that making a getcontents class, I could re-use that in anything that needed to request contents lol, why was this a bad idea, I still don't understand. Ok, so it's good practice to use an abstract class in cases where multiple classes share methods and common functionality, but it's still functionality applicable to only a small set of classes, if it's common behaviour, I should create a common base class to encapsulate this behaviour to extend from, is that right? This is the updated code, anything to expand on or improve it? <?php abstract class Geocoding { protected $_url, $_content_type; public function __construct($url, $content_type) { $this->_url = $url; $this->_content_type = $content_type; } public function get($formatted = false) { return ($formatted) ? $this->_request_formatted($this->_url) : $this->_request($this->_url); } protected function _request($url) { if(ini_get('allow_url_fopen') != 1) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FAILONERROR, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_TIMEOUT, 3); $contents = curl_exec($ch); curl_close($ch); } else { $contents = file_get_contents($url); } return $contents; } } class Geocode extends Geocoding { protected function _request_formatted($url) { $contents = $this->_request($url); if ( stristr($this->_content_type, 'xml') !== false ) { $contents = (array)simplexml_load_string($contents); $key = 'result'; } else { $contents = (array)json_decode($contents); $key = 'results'; } if ( $contents['status'] != 'OK' ) return array('status' => 'ERROR', 'message' => 'No results returned'); if ( key($contents[$key]) ) { $res = array($contents[$key]); } else { $res = $contents[$key]; } unset($contents); $results = array(); foreach($res as $result) { array_push($results, array( 'address' => (is_string($result->formatted_address) ? $result->formatted_address : current($result->formatted_address)), 'location' => (array)$result->geometry->location, 'viewport' => array( 'NE' => (array)$result->geometry->viewport->northeast, 'SW' => (array)$result->geometry->viewport->southwest ), 'bounds' => array( 'NE' => (array)$result->geometry->bounds->northeast, 'SW' => (array)$result->geometry->bounds->southwest ) )); } $results['length'] = count($results); $results['status'] = 'OK'; return $results; } } class ReverseGeocode extends Geocoding { protected function _request_formatted($url) { $contents = $this->_request($url); if ( stristr($this->_content_type, 'xml') !== false ) { $contents = (array)simplexml_load_string($contents); $key = 'result'; $comp = 'address_component'; $type = 'type'; } else { $contents = (array)json_decode($contents); $key = 'results'; $comp = 'address_components'; $type = 'types'; } if ( $contents['status'] != 'OK' ) return array('status' => 'ERROR', 'message' => 'No results returned'); if ( key($contents[$key]) ) { $result = $contents[$key]; } else { $result = current($contents[$key]); } unset($contents); $result = (array)$result; $res = array(); foreach($result[$comp] as $addr) { $addr = (array)$addr; if ( !isset($addr[$type]) ) continue; if ( is_array($addr[$type]) ) $addr[$type] = current($addr[$type]); switch(strtolower($addr[$type])) { case 'street_number' : $res['street_number'] = $addr['long_name']; break; case 'route' : $res['street_name'] = $addr['long_name']; break; case 'locality' : $res['town'] = $addr['long_name']; break; case 'country' : $res['country'] = $addr['long_name']; $res['country_code'] = $addr['short_name']; break; case 'postal_code_prefix' : $res['postcode_prefix'] = $addr['long_name']; break; } } $res['address'] = $result['formatted_address']; $res['status'] = 'OK'; return $res; } } class Geocoder { private $_settings; /** * __CONSTRUCT($type = 'json', $sensor = false, $region = null, $bounds = null, $lang = 'en') * __CONSTRUCT( * array( * 'type' => 'json', * 'sensor' => false, * 'region' => null, * 'bounds' => null, * 'lang' => 'en' * ) * ); */ public function __construct() { $this->_settings = array( 'type' => 'json', 'sensor' => false, 'region' => null, 'bounds' => null, 'lang' => 'en' ); $arguments = func_get_args(); if ( count($arguments) ) { if ( count($arguments) == 1 && is_array($arguments[0]) ) { $this->_settings = array_merge($this->_settings, $arguments[0]); return false; } foreach($this->_settings as $k => $v) { $this->_settings[$k] = current($arguments); next($arguments); } } } public function __set($name, $value) { if ( !in_array($name, array_keys($this->_settings)) ) return false; $this->_settings[$name] = $value; } public function __get($name) { return isset($this->_settings[$name]) ? $this->_settings[$name] : false; } public function geocode() { $args = func_get_args(); if ( is_array($args[0]) ) { $args = implode(',', $args[0]); } else { $args = implode(',', $args); } $url = 'http://maps.googleapis.com/maps/api/geocode/'; $url .= urlencode($this->_settings['type']) .'?'; $url .= 'address='. urlencode($args); $url .= '&sensor='. ($this->_settings['sensor'] ? 'true' : 'false'); if ( $this->_settings['region'] ) $url .= '&region='. urlencode($this->_settings['region']); if ( $this->_settings['bounds'] ) { if ( is_array($this->_settings['bounds']) ) { if ( count(current($this->_settings['bounds'])) == 4 ) { $this->_settings['bounds'] = array_chunk($this->_settings['bounds'], 2); } $this->_settings['bounds'] = array_slice($this->_settings['bounds'], 0, 2); $url .= '&bounds='. urlencode(implode(',', $this->_settings['bounds'][0])) .'|'. urlencode(implode(',', $this->_settings['bounds'][1])); } elseif ( is_string($this->_settings['bounds']) ) { $url .= '&bounds='. urlencode($this->_settings['bounds']); } } $url .= '&language='. urlencode($this->_settings['lang']); return new Geocode($url, $this->_settings['type']); } public function reverseGeocode() { $args = func_get_args(); if ( count($args) < 2 && !is_array($args[0]) ) throw new Exception('Invalid arguments passed to Geocoder::reverseGeocode (expected :- (lat, lng) || (array(lat, lng)))'); if ( is_array($args[0]) ) { $args[0] = array_filter($args[0], 'is_numeric'); $lat = current($args[0]); $lng = next($args[0]); } else { $lat = $args[0]; $lng = $args[1]; } $url = 'http://maps.googleapis.com/maps/api/geocode/'; $url .= urlencode($this->_settings['type']) .'?'; $url .= 'latlng='. urlencode($lat .','. $lng); $url .= '&sensor='. ($this->_settings['sensor'] ? 'true' : 'false'); return new ReverseGeocode($url, $this->_settings['type']); } } Implementation: $latlng = array(53.4829164, -2.0599362); include 'api/classes/Geocoder.class.php'; $Geocoder = new Geocoder(); echo '<pre>'. print_r($Geocoder->geocode('SK15 1AS')->get(true), 1); echo '<pre>'. print_r($Geocoder->reverseGeocode($latlng)->get(true), 1); Array ( [0] => Array ( [address] => Stalybridge SK15 1AS, UK [location] => Array ( [lat] => 53.4829164 [lng] => -2.0599362 ) [viewport] => Array ( [NE] => Array ( [lat] => 53.484052280292 [lng] => -2.0589575 ) [sW] => Array ( [lat] => 53.481354319709 [lng] => -2.0620962 ) ) [bounds] => Array ( [NE] => Array ( [lat] => 53.483383 [lng] => -2.0589575 ) [sW] => Array ( [lat] => 53.4820236 [lng] => -2.0620962 ) ) ) [length] => 1 [status] => OK ) Array ( [street_name] => Caroline St [town] => Stalybridge [country] => United Kingdom [country_code] => GB [postcode_prefix] => SK15 [address] => Caroline St, Stalybridge SK15, UK [status] => OK ) [pre]Thanks for your help Kevin.[/pre]
  23. You're using the PHP concatenate operator instead of javascript.You don't even need to concat this. $("<?php echo "#".$hours_difference; ?>").val(round(data.hours + data.minutes/60 - 0.5, 2));
  24. <?php interface Geocoding { public function __construct($url, $content_type); public function request($formatted); } class GetContents { protected function _request($url) { if(ini_get('allow_url_fopen') != 1) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FAILONERROR, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_TIMEOUT, 3); $contents = curl_exec($ch); curl_close($ch); } else { $contents = file_get_contents($url); } return $contents; } } class Geo extends GetContents { protected $_url, $_content_type; public function __construct($url, $content_type) { $this->_url = $url; $this->_content_type = $content_type; } public function request($formatted = false) { return ($formatted) ? $this->_request_formatted($this->_url) : $this->_request($this->_url); } } class Geocode extends Geo implements Geocoding { protected function _request_formatted($url) { $contents = $this->_request($url); if ( stristr($this->_content_type, 'xml') !== false ) { $contents = (array)simplexml_load_string($contents); $key = 'result'; } else { $contents = (array)json_decode($contents); $key = 'results'; } if ( $contents['status'] != 'OK' ) return array('status' => 'ERROR', 'message' => 'No results returned'); if ( key($contents[$key]) ) { $res = array($contents[$key]); } else { $res = $contents[$key]; } unset($contents); $results = array(); foreach($res as $result) { array_push($results, array( 'address' => (is_string($result->formatted_address) ? $result->formatted_address : current($result->formatted_address)), 'location' => (array)$result->geometry->location, 'viewport' => array( 'NE' => (array)$result->geometry->viewport->northeast, 'SW' => (array)$result->geometry->viewport->southwest ), 'bounds' => array( 'NE' => (array)$result->geometry->bounds->northeast, 'SW' => (array)$result->geometry->bounds->southwest ) )); } $results['length'] = count($results); $results['status'] = 'OK'; return $results; } } class ReverseGeocode extends Geo implements Geocoding { protected function _request_formatted($url) { $contents = $this->_request($url); if ( stristr($this->_content_type, 'xml') !== false ) { $contents = (array)simplexml_load_string($contents); $key = 'result'; $comp = 'address_component'; $type = 'type'; } else { $contents = (array)json_decode($contents); $key = 'results'; $comp = 'address_components'; $type = 'types'; } if ( $contents['status'] != 'OK' ) return array('status' => 'ERROR', 'message' => 'No results returned'); if ( key($contents[$key]) ) { $result = $contents[$key]; } else { $result = current($contents[$key]); } unset($contents); $result = (array)$result; $res = array(); foreach($result[$comp] as $addr) { $addr = (array)$addr; if ( !isset($addr[$type]) ) continue; if ( is_array($addr[$type]) ) $addr[$type] = current($addr[$type]); switch(strtolower($addr[$type])) { case 'street_number' : $res['street_number'] = $addr['long_name']; break; case 'route' : $res['street_name'] = $addr['long_name']; break; case 'locality' : $res['town'] = $addr['long_name']; break; case 'country' : $res['country'] = $addr['long_name']; $res['country_code'] = $addr['short_name']; break; case 'postal_code_prefix' : $res['postcode_prefix'] = $addr['long_name']; break; } } $res['address'] = $result['formatted_address']; $res['status'] = 'OK'; return $res; } } class Geocoder { private $_settings; /** * __CONSTRUCT($type = 'json', $sensor = false, $region = null, $bounds = null, $lang = 'en') * __CONSTRUCT( * array( * 'type' => 'json', * 'sensor' => false, * 'region' => null, * 'bounds' => null, * 'lang' => 'en' * ) * ); */ public function __construct() { $this->_settings = array( 'type' => 'json', 'sensor' => false, 'region' => null, 'bounds' => null, 'lang' => 'en' ); $arguments = func_get_args(); if ( count($arguments) ) { if ( count($arguments) == 1 && is_array($arguments[0]) ) { $this->_settings = array_merge($this->_settings, $arguments[0]); return false; } foreach($this->_settings as $k => $v) { $this->_settings[$k] = current($arguments); next($arguments); } } } public function __set($name, $value) { if ( !in_array($name, array_keys($this->_settings)) ) return false; $this->_settings[$name] = $value; } public function __get($name) { return isset($this->_settings[$name]) ? $this->_settings[$name] : false; } public function geocode() { $args = func_get_args(); if ( is_array($args[0]) ) { $args = implode(',', $args[0]); } else { $args = implode(',', $args); } $url = 'http://maps.googleapis.com/maps/api/geocode/'; $url .= urlencode($this->_settings['type']) .'?'; $url .= 'address='. urlencode($args); $url .= '&sensor='. ($this->_settings['sensor'] ? 'true' : 'false'); if ( $this->_settings['region'] ) $url .= '&region='. urlencode($this->_settings['region']); if ( $this->_settings['bounds'] ) { if ( is_array($this->_settings['bounds']) ) { if ( count(current($this->_settings['bounds'])) == 4 ) { $this->_settings['bounds'] = array_chunk($this->_settings['bounds'], 2); } $this->_settings['bounds'] = array_slice($this->_settings['bounds'], 0, 2); $url .= '&bounds='. urlencode(implode(',', $this->_settings['bounds'][0])) .'|'. urlencode(implode(',', $this->_settings['bounds'][1])); } elseif ( is_string($this->_settings['bounds']) ) { $url .= '&bounds='. urlencode($this->_settings['bounds']); } } $url .= '&language='. urlencode($this->_settings['lang']); return new Geocode($url, $this->_settings['type']); } public function reverseGeocode() { $args = func_get_args(); if ( count($args) < 2 && !is_array($args[0]) ) throw new Exception('Invalid arguments passed to Geocoder::reverseGeocode (expected :- (lat, lng) || (array(lat, lng)))'); if ( is_array($args[0]) ) { $args[0] = array_filter($args[0], 'is_numeric'); $lat = current($args[0]); $lng = next($args[0]); } else { $lat = $args[0]; $lng = $args[1]; } $url = 'http://maps.googleapis.com/maps/api/geocode/'; $url .= urlencode($this->_settings['type']) .'?'; $url .= 'latlng='. urlencode($lat .','. $lng); $url .= '&sensor='. ($this->_settings['sensor'] ? 'true' : 'false'); return new ReverseGeocode($url, $this->_settings['type']); } } Just a geocoding class I've just written, not clued-up on OOP so just looking for advice on what I could have done better, what I've done well etc. Thanks
  25. http://www.red5.org/
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.