Jump to content

keeps21

Members
  • Posts

    91
  • Joined

  • Last visited

Everything posted by keeps21

  1. Hi A really basic question, with regards to best practice, but I can't find an answer anywhere. Should the charset in my website be set to match the charset used by my database? E.g. Database is utf-8, so website should be set to utf-8?
  2. I have an application which allows users to make a booking for a room. user picks the date, start time and end time. What I want to do is create a colour coded 'table of availability'. Basically draw a table, showing 'blocks' of 1 hour intervals, if that hour has been booked it is unavailable, color red, otherwise color green. The bookings are stored in the database table 'bookings' 'booking_id | date | start_time | end_time | user_id | room_id | ------------------------------------------------------------------- 1 | 2009-09-30 | 16:00:00 | 17:00:00 | 1 | 1 | 2 | 2009-09-30 | 12:00:00 | 14:00:00 | 1 | 1 | I'm pulling the data from the database for that room and date into an array SELECT * FROM bookings WHERE date= 2009-09-30 would give an array: array( '0' => array('booking_id' => '1', 'date' => '2009-09-30', 'start_time' => '16:00:00', 'end_time' => '17:00:00', 'user_id' => '1', 'room_id => '1', ); '1' => array('booking_id' => '2', 'date' => '2009-09-30', 'start_time' => '12:00:00', 'end_time' => '14:00:00', 'user_id' => '1', 'room_id => '1', ); ) ; what i need to do is write out a table where each cell represents an hour in time and color cells in which represent hours between start_time, and end_time in the array using a loop. I just can't get my head around the loop required. Any help would be greatly appreciated. Thanks
  3. That would be the better option. But you could store all the keywords in one field. Then when pulling the keywords out of the database "explode" that field to get the individual keywords.
  4. As per the title. I need to remove the text between the first <h1></h1> tags in the input string - but leave any further <h1></h1> tags in the string as they are. Any help would be appreciated. Thanks.
  5. You're using $cid in your query on line 26 but $cid hasn't been definied anywhere, in other words $cid doesn't exist.
  6. Try using this function function clean_data($data) { $code_entities_match = array(' ','--','"','!','@','#','$','%','^','&','*','(',')','_','+','{','}','|',':','"','<','>','?','[',']','\\',';',"'",',','.','/','*','+','~','`','='); $code_entities_replace = array('','','','','','','','','','','','','','','','','','','','','','','','','',''); $clean = str_replace($code_entities_match, $code_entities_replace, $data); return $clean; } // You would use it like follows $clean_data = clean_data($_POST['find']);
  7. Further amendments. <?php # Set default timezone date_default_timezone_set('Europe/London'); ini_set('display_errors', 1); error_reporting(E_ALL|E_STRICT); // Convert date function get_date($date) { return date('Y-m-d H:i:s', strtotime($date)); } // Convert date to timestamp function convert_to_timestamp($date) { return strtotime($date); } // Parse feed function parse_feed($feed) { $rss = simplexml_load_file($feed); if ($rss) { // Feed is valid and well formed $newsfeed = array(); $i=0; foreach ($rss->channel->item as $item) { $newsfeed[$i]['title'] = $item->title; $newsfeed[$i]['pubDate'] = get_date($item->pubDate); $newsfeed[$i]['timestamp'] = convert_to_timestamp($item->pubDate); $newsfeed[$i]['description'] = $item->description; $newsfeed[$i]['link'] = $item->link; $i++; } return $newsfeed; } } // $feeds array will be populated from the database in the future $feeds = array('google' => 'http://news.google.co.uk/news?um=1&ned=uk&hl=en&q=football&output=rss', 'bbc' => 'http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/front_page/rss.xml' ); $merged = array(); foreach ($feeds as $feed) : $fe = parse_feed($feed); foreach ($fe as $f): $merged[] = $f; endforeach; endforeach; // Sort the data with volume descending, edition ascending // Add $data as the last parameter, to sort by the common key foreach ($merged as $key => $row) { $pubdate[$key] = $row['pubDate']; } array_multisort($pubdate, SORT_DESC, SORT_STRING, $merged); // Output array foreach($merged as $m) : if (time() - $m['timestamp'] <= 3600) { // Last Hour if (!isset($last)) { echo '<h1>Last Hour</h1>'; $last = 1; } echo date( 'd-m-Y H:i', $m['timestamp']) . ' - ' . $m['title'] . '<br />'; } elseif (time() - $m['timestamp'] <= 7200 && time() - $m['timestamp'] > 3600 ) { // between 1 and 2 hours if (!isset($onetotwo)) { echo '<h1>1-2 Hours Old</h1>'; $onetotwo = 1; } echo date( 'd-m-Y H:i', $m['timestamp']) . ' - ' . $m['title'] . '<br />'; } elseif (time() - $m['timestamp'] <= 14400 && time() - $m['timestamp'] > 7200 ) { // between 2 and 4 hours if (!isset($twotofour)) { echo '<h1>2-4 Hours Old</h1>'; $twotofour = 1; } echo date( 'd-m-Y H:i', $m['timestamp']) . ' - ' . $m['title'] . '<br />'; } else { // over 4 hours old if (!isset($overfour)) { echo '<h1>Over 4 Hours Old</h1>'; $overfour = 1; } echo date( 'd-m-Y H:i', $m['timestamp']) . ' - ' . $m['title'] . '<br />'; } endforeach;
  8. Now amended to show items in groups. Last Hour 1-2 Hours Old 2-4 Hours Old Over 4 Hours Old Code is shown below - I'd be very grateful for any suggestions,improvements or advice. <?php // Convert date function get_date($date) { return date('Y-m-d H:i:s', strtotime($date)); } // Convert date to timestamp function convert_to_timestamp($date) { return strtotime($date); } // Parse feed function parse_feed($feed='') { $rss = simplexml_load_file($feed); if ($rss) { // Feed is valid and well formed $newsfeed = array(); $i=0; foreach ($rss->channel->item as $item) { $newsfeed[$i]['title'] = $item->title; $newsfeed[$i]['pubDate'] = get_date($item->pubDate); $newsfeed[$i]['timestamp'] = convert_to_timestamp($item->pubDate); $newsfeed[$i]['description'] = $item->description; $newsfeed[$i]['link'] = $item->link; $i++; } return $newsfeed; } } // Feeds to parse $google = parse_feed('http://news.google.co.uk/news?um=1&ned=uk&hl=en&q=football&output=rss'); $bbc = parse_feed('http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/front_page/rss.xml'); // Array to store feed data in $merged = array(); // Add feed 1 data to array foreach($google as $data) : $merged[] = $data; endforeach; // Add feed 2 data to array foreach($bbc as $data) : $merged[] = $data; endforeach; // Sort the data with volume descending, edition ascending // Add $data as the last parameter, to sort by the common key foreach ($merged as $key => $row) { $pubdate[$key] = $row['pubDate']; } array_multisort($pubdate, SORT_DESC, SORT_STRING, $merged); // Output array foreach($merged as $m) : if (time() - $m['timestamp'] <= 3600) { // Last Hour if ($last != 1) { echo '<h1>Last Hour</h1>'; $last = 1; } echo $m['pubDate'] . ' - ' . $m['title'] . '<br />'; } elseif (time() - $m['timestamp'] <= 7200 && time() - $m['timestamp'] > 3600 ) { // between 1 and 2 hours if ($onetotwo != 1) { echo '<h1>1-2 Hours Old</h1>'; $onetotwo = 1; } echo $m['pubDate'] . ' - ' . $m['title'] . '<br />'; } elseif (time() - $m['timestamp'] <= 14400 && time() - $m['timestamp'] > 7200 ) { // between 2 and 4 hours if ($twotofour != 1) { echo '<h1>2-4 Hours Old</h1>'; $twotofour = 1; } echo $m['pubDate'] . ' - ' . $m['title'] . '<br />'; } else { // over 4 hours old if ($overfour != 1) { echo '<h1>Over 4 Hours Old</h1>'; $overfour = 1; } echo $m['pubDate'] . ' - ' . $m['title'] . '<br />'; } endforeach;
  9. This is the code I've come up with - and it seems to be working alright for me. Can anyone make any suggestions as to improvements - especially with regards to the way I'm handling the date. Cheers Here's the code. <?php // Convert date function get_date($date) { // Date is in the format Mon, 08 Jun 2009 $str = $date; $strArray = explode(' ', $str); array_shift($strArray); array_pop($strArray); switch ($strArray[1]) { case 'Jan': $strArray[1] = '01'; break; case 'Feb': $strArray[1] = '02'; break; case 'Mar': $strArray[1] = '03'; break; case 'Apr': $strArray[1] = '04'; break; case 'May': $strArray[1] = '05'; break; case 'Jun': $strArray[1] = '06'; break; case 'Jul': $strArray[1] = '07'; break; case 'Aug': $strArray[1] = '08'; break; case 'Sep': $strArray[1] = '09'; break; case 'Oct': $strArray[1] = '10'; break; case 'Nov': $strArray[1] = '11'; break; case 'Dec': $strArray[1] = '12'; break; default: break; } // re-form date $date = $strArray[2].'-'.$strArray[1].'-'.$strArray[0].' '.$strArray[3]; return $date; } // Parse feed function parse_feed($feed='') { $rss = simplexml_load_file($feed); if ($rss) { // Feed is valid and well formed $newsfeed = array(); $i=0; foreach ($rss->channel->item as $item) { $newsfeed[$i]['title'] = $item->title; $newsfeed[$i]['pubDate'] = get_date($item->pubDate); $newsfeed[$i]['description'] = $item->description; $newsfeed[$i]['link'] = $item->link; $i++; } return $newsfeed; } } // Feeds to parse $google = parse_feed('http://news.google.co.uk/news?um=1&ned=uk&hl=en&q=football&output=rss'); $bbc = parse_feed('http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/front_page/rss.xml'); // Array to store feed data in $merged = array(); // Add feed 1 data to array foreach($google as $data) : $merged[] = $data; endforeach; // Add feed 2 data to array foreach($bbc as $data) : $merged[] = $data; endforeach; // Sort the data with volume descending, edition ascending // Add $data as the last parameter, to sort by the common key foreach ($merged as $key => $row) { $pubdate[$key] = $row['pubDate']; } array_multisort($pubdate, SORT_DESC, SORT_STRING, $merged); // Output array foreach($merged as $m) : echo $m['pubDate'] . ' - ' . $m['title'] . '<br />'; endforeach;
  10. I don't think so. What I'm wanting to do is merge a number of feeds together, and output the latest x items ordered by date, most recent first. I'll be taking a feed, reading the feed data into the array, and then repeating for a number of feeds. I then want to sort the order of the array.
  11. I have an array which I want to sort by the index 'pubDate' from most recent to oldest But I'm not sure how to do it. Here is the array I'm working with Array ( [0] => Array ( [title] => SimpleXMLElement Object ( [0] => Warriors net two more signings ) [pubDate] => SimpleXMLElement Object ( [0] => Mon, 08 Jun 2009 10:10:15 GMT ) [description] => SimpleXMLElement Object ( ) [link] => SimpleXMLElement Object ( [0] => http://www.whitleywarriors.net/news/view/id/202/warriors-net-two-more-signings.html ) ) [1] => Array ( [title] => SimpleXMLElement Object ( [0] => New Website Launched ) [pubDate] => SimpleXMLElement Object ( [0] => Mon, 01 Jun 2009 11:16:50 GMT ) [description] => SimpleXMLElement Object ( ) [link] => SimpleXMLElement Object ( [0] => http://www.whitleywarriors.net/news/view/id/200/new-website-launched.html ) ) [2] => Array ( [title] => SimpleXMLElement Object ( [0] => Warrior Spike ) [pubDate] => SimpleXMLElement Object ( [0] => Wed, 27 May 2009 22:17:08 GMT ) [description] => SimpleXMLElement Object ( ) [link] => SimpleXMLElement Object ( [0] => http://www.whitleywarriors.net/news/view/id/199/warrior-spike.html ) ) [3] => Array ( [title] => SimpleXMLElement Object ( [0] => Legends return to the ice ) [pubDate] => SimpleXMLElement Object ( [0] => Wed, 27 May 2009 22:16:15 GMT ) [description] => SimpleXMLElement Object ( ) [link] => SimpleXMLElement Object ( [0] => http://www.whitleywarriors.net/news/view/id/198/legends-return-to-the-ice.html ) ) [4] => Array ( [title] => SimpleXMLElement Object ( [0] => Captain Dunn - Sample and Wilson to return ) [pubDate] => SimpleXMLElement Object ( [0] => Thu, 21 May 2009 10:46:56 GMT ) [description] => SimpleXMLElement Object ( ) [link] => SimpleXMLElement Object ( [0] => http://www.whitleywarriors.net/news/view/id/192/captain-dunn--sample-and-wilson-to-return.html ) ) [5] => Array ( [title] => SimpleXMLElement Object ( [0] => Two in, one out ) [pubDate] => SimpleXMLElement Object ( [0] => Sat, 16 May 2009 14:05:23 GMT ) [description] => SimpleXMLElement Object ( ) [link] => SimpleXMLElement Object ( [0] => http://www.whitleywarriors.net/news/view/id/180/two-in-one-out.html ) ) ) How would I do this?
  12. Just thinking about creating a website to take in multiple rss feeds, merging the data into an array and then outputting the latest 10 or so. Pseudocode # Initialise feedArray $feedArray = array(); foreach feed : # Grab and read feed # Add feed data into $feedArray - ie title, link, description, date endforeach; # sort feed array # output foreach item in feedarray : #output data endforeach; Does that make sense?
  13. When opening the page is ?cid=1 (where the id you want is 1) part of the query string in the address bar? If not then $_GET['cid'] hasn't been defined, which is why you're getting the undefined index message.
  14. I didn't actually need to use the function call_user_func_array() - changed it to $dispatch->$action; and now passing $queryString to my controller when it is constructed meaning I can access $queryString at $this->queryString in my controllers. Here is the full code if anyone is interested <?php class FrontController { private static $instance; public static function getInstance() { if (!self::$instance) { self::$instance = new self(); } return self::$instance; } private function __construct() { } function processRequest($request) { # Create urlArray and populate with request data $urlArray = array(); $urlArray = explode("/",$request); # Get controller from request $controller = $urlArray[0]; # Move onto action from request array_shift($urlArray); if (array_key_exists(0,$urlArray)) { $action = $urlArray[0]; } else { $action = 'index'; } # Move onto querystring from request array_shift($urlArray); $queryString = $urlArray; # Set controller name - append Controller to name $this->controllerName = $controller; $this->actionName = $action; $controller = $controller; $controller .= 'Controller'; $action .= 'Action'; # Create instance of requested controller $dispatch = new $controller($this->controllerName, $action, $queryString); # Call requested method on invoked controller try { if (!(int)method_exists($controller, $action)) { Throw New Exception('Method - ' . $action . ' - does not exist.'); } else { $dispatch->$action(); } } catch (Exception $e) { if (ini_get('display_errors') == 1) { echo 'System Error: ' . $e; } } } private function __clone() { } function __destruct() { } }
  15. It's my 'take' on a simple framework. FrontController takes a request and loads the requested ActionController and it's method. I want to pass the query string that is defined in the FrontController to the ActionController which is executed by the processRequest() method in the FrontController
  16. So say I've loaded my IndexController I should be able to access it in the IndexController with $this->queryString; Or am I missing something?
  17. I have a FrontController class which takes a request, explodes it into an array, the first item being the controller, second being the action and then the 3rd is the querystring. It then loads the correct controller and it's method. For example this - http://mysite.com/news/view/?id=21 Would load the news controller and view action and the querystring would be id=21 Here is the class code class FrontController { private static $instance; public static function getInstance() { if (!self::$instance) { self::$instance = new self(); } return self::$instance; } function __construct() { } function processRequest($request) { # Set default request if blank if ($request == '') { $request = 'index/index'; } # Create urlArray and populate with request data $urlArray = array(); $urlArray = explode("/",$request); # Get controller from request $controller = $urlArray[0]; # Move onto action from request array_shift($urlArray); if (array_key_exists(0,$urlArray)) { $action = $urlArray[0]; } else { $action = 'index'; } # Move onto querystring from request array_shift($urlArray); $queryString = $urlArray; # Set controller name - append Controller to name $this->controllerName = $controller; $this->actionName = $action; $controller = $controller; $controller .= 'Controller'; $action .= 'Action'; # Create instance of requested controller $dispatch = new $controller($this->controllerName,$action); # Call requested method on invoked controller try { if (!(int)method_exists($controller, $action)) { Throw New Exception('Method - ' . $action . ' - does not exist.'); } else { call_user_func_array(array($dispatch,$action),$queryString); } } catch (Exception $e) { echo 'System Error: ' . $e; } } function __destruct() { } } This code is modified a little from some I found on the internet (http://anantgarg.com/) That all works fine, the correct controller and action is loaded. The problem I'm having is with this bit call_user_func_array(array($dispatch,$action),$queryString); How do I access the $queryString variable in my controller, where am I passing it to with the above code?
  18. I have a couple of domains and one hosting account. What I want to do is to direct people who go to: http://site-one.com to be directed to the folder containing the files/website for site one and to direct people who go to: http://site-two.com to be directed to the folder containing the files/website for site two What is the best way to do this?
  19. change <input type="hidden" name="username" value="$username"> to <input type="hidden" name="username" value="<?php echo $username;?>"> [/php
  20. Could also do it this way as ... but there are probably better ways to do it. <?php # This would be your press release populated form your database $press_release = "<p>This is my press release. It is pretty generic.</p> <p>It really doesn't tell you anything interesting</p>"; # This would ideally be generated from your database $link = 'http://news.com/news?id=1'; # Explode into an array splitting the press release at </p> doing it this way will split the string BEFORE the </p> $first_para = explode('</p>', $press_release); # Remember we need to add the traling </p> tag as we've cut it off echo $first_para[0] . '<a href="' . $link . '">...Read More</a></p>';
  21. Try this - it probably isn't the best way to do it, but it should work. <?php $image_path = 'flyers/'; // Retrieve data from database $sql = "SELECT * FROM flyers WHERE uid = " . $_SESSION['id']; $result = mysql_query($sql); // Initialise count $i = 0; // Start looping rows in mysql database. while($rows = mysql_fetch_array($result)){ ?> <div class="flyerbox" style="float:left;"> <a href="viewimage.php?maxsize=640&source=<?=$image_path.$rows['flyer'] ?>" rel="facebox"> <img src="viewimage.php?maxsize=75&source=<?=$image_path.$rows['flyer'] ?>" /></a> </div> <?php if ($i <= 9) { # if count is 9 or less increment count by 1 and loop $i++; } else {# if count is 10 add 'break' and reset counter $i = 0; ?> <div style="clear:both;"></div> <?php } }
  22. See this video - http://www.killerphp.com/videos/oop_php_build_objects/build_objects_php_oop.html - it explains $this quiite well. But keep in mind that the video is using PHP4 syntax.
  23. The $this is a built-in variable (built into all objects) which points to the current object. Or in other words, $this is a special self-referencing variable. You use $this to access properties and to call other methods of the current class.
×
×
  • 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.