Jump to content

ignace

Moderators
  • Posts

    6,457
  • Joined

  • Last visited

  • Days Won

    26

Everything posted by ignace

  1. If you could give us an example that we can work with instead of firstClass and secondClass I'm sure we can come up with even better approaches. Template Method for example is a design pattern specifically targeted at this.
  2. Post your (relevant) code.
  3. Have you tried overwriting the fetch_class() method on the Routing class?
  4. Pinging won't solve anything. Pinging only means that the host is reachable not that they are still chatting. So no, there is no real way except maybe using the onunload event of which I'm unsure if it triggers correctly on all browsers/platforms. What you actually need is a socket which is available as of HTML5: WebSockets, you can find a tutorial of how to use it on NetTuts+
  5. Just ask them All jokes aside! Yes, you would poll your server every n seconds to fetch the new contents and update their status, anyone where the delta between last update time and the current time exceeds the idle time is considered offline. Preferably you should return this to any active clients so they can update their list of users in the chat.
  6. Git is a great tool except it does not run out-of-the-box under Windows (which is I think your main OS), so you'll either need Cygwin or msysgit. Neither are advisable if you have never used Linux or the Windows CLI before. Subversion is only an option if you have a live SVN server running otherwise you won't even be able to do the basics: commit source code. Some server hosts provide SVN hosting but not many do. You can find a free Subversion host at Unfuddle and download TortoiseSVN to have nice GUI to work with SVN. All commits you do are against the SVN server so this may become a bottleneck if the commit is quite large since you can't use SVN until the commit is complete. Another great option on the market is Mercurial (which comes pre-bundled with TortoiseHG). Mercurial is like Git a distributed SCM and all commits are done against your local copy, which is fast and there is no need for a server. However in order to collaborate both developers have to be online at the same time so you can push your changes and pull their changes (and resolve any conflicts in the process). This is of course becomes a problem if you aren't always online when the other developers are so for that you'll still have the strengths of Mercurial but you'll also need a server to push your changes to (BitBucket being one of these). Now you can work any time you like as do the other developers and they push their changes to BitBucket and pull your changes. So Mercurial gives you a little more flex than Subversion does as you can commit while offline and push your changes whenever you get the chance to be online. Pick whatever suits you best and if you are in for adventure give Git a try
  7. You should look at the bigger picture here. Once you put your website live, you can't just go and do some live editing since that could ruin a user's experience therefor you need to have your website running locally on your computer with a development database, not the real live database. And patch the online code after you verified it works (be sure to notify your users of a scheduled maintenance and put your website "offline" till all code has been patched (uploaded)). You can put your website "offline" by using an .htaccess to redirect all traffic (expect you) to a maintenance page (be sure to return a proper HTTP status code for any spiders/crawlers on your website to come back later). An SCM (Source Code Management) tool like SVN helps you to merge conflicting files instead of having to do the process manually, in which you may even remove some work of your fellow developer. Other options you have with an SCM tool is to tag a specific release like 0.1, 0.2, .. which is nice if for example if you have a bug since v2.5 and found the root-cause in v2.9, you can then go back (checkout) to v2.5 and commit the fix and your SCM tool will internally patch all code till the latest version 2.9 (imagine doing that manually). Which you'll thank your SCM for many times if you have a customer running on an older codebase. Another great option SCM provides you with is branches. This is specifically great if for example you want to implement a new feature but you don't want to hinder bug fixes. So you can develop the new feature while bug fixes can be released on a regular basis. Once your feature is complete you merge it back to the main codebase and resolve any conflicts.
  8. function getOverdueString($dueDate, $currentDate = 'now', $timezone = 'Europe/Brussels', $formatString = '%s%dd %dh %dm') { $old_timezone = date_default_timezone_get(); date_default_timezone_set($timezone); $a = strtotime($dueDate); if ($a === FALSE || $a === -1/*<5.1*/) return FALSE; $b = strtotime($currentDate); if ($b === FALSE || $b === -1/*<5.1*/) return FALSE; date_default_timezone_set($old_timezone); $diff = $a - $b; $sign = $diff < 0 ? '-' : ''; $diff = abs($diff); $days = floor($diff / 86400); $hrs = floor(($diff % 86400) / 3600); $mnts = floor((($diff % 86400) % 3600) / 60); return sprintf($formatString, $sign, $days, $hrs, $mnts); } I have made a few adjustments: 1) added floor to $hrs and $mnts 2) added in timezone support since your customers will likely be in a different timezone so the overdue date should be calculated according to their timezone otherwise they would buy/rent your product and lose a few or lots of hours on the meter before overdue. So be sure to ask the timezone of your customer. Proof-of-Concept: // 'now' => 2011-07-31 8:16:00 print getOverdueString('2012-07-31 8:12:00').PHP_EOL; // 365d 23h 55m print getOverdueString('2011-07-31 8:12:00').PHP_EOL; // -0d 0h 4m print getOverdueString('2010-07-31 8:12:00').PHP_EOL; // -365d 0h 4m Be sure to change Europe/Brussels with yours. You can find the supported timezones on php.net
  9. Too much typing.
  10. I am not aware of any standard functions that mimic DateTime perfectly I have tested both strftime() and date() none of them got it correct date() f.e. returned 1 day 30 mins for 1800, that may be my bad either though not sure. Here's an example that works like the DateTime function did. function getOverdueString($dueDate, $currentDate = 'now', $formatString = '%s%dd %dh %dm') { $a = strtotime($dueDate); if ($a === FALSE || $a === -1/*<5.1*/) return FALSE; $b = strtotime($currentDate); if ($b === FALSE || $b === -1/*<5.1*/) return FALSE; $diff = $a - $b; $sign = $diff < 0 ? '-' : ''; $diff = abs($diff); $days = floor($diff / 86400); $hrs = ($diff % 86400) / 3600; $mnts = (($diff % 86400) % 3600) / 60; return sprintf($formatString, $sign, $days, $hrs, $mnts); } // echo getOverdueString( '2011-08-05 09:00:00', '2011-08-05 08:30:00'); // 0d 0h 30m
  11. That said you could just write: if(!mysql_num_rows($result))
  12. In mjdamato's code is a bug: if(mysql_num_rows($result)=0) Should be if(mysql_num_rows($result)==0)
  13. That's because Dreamweaver doesn't parse your PHP code. You need to have a webserver installed like XAMPP and have your files hosted in it's htdocs directory (unless you know, which I doubt, how to re-configure Apache to point to the current location of your directory). You can then see the output, if you are lucky formatted like you did in Dreamweaver since DW is a WYSIWYAG: What You See Is What You Almost Get, if you navigate your browser to either: http://localhost:80/ or http://localhost:8080/ localhost (the reserved 127.0.0.1 ip address) is not a website like www.google.com it's a loopback address or in other words it points back to your computer with a certain protcol (http) and a port (80) this will trigger your local XAMPP installation as it's listening on one of these ports with a http protocol handler (Apache).
  14. If this is for debugging purposes you should just extend your class to accept a log object and use that inside your class. $log->methodCalled(__METHOD__);
  15. The above function should be self-explanatory. It gives you complete control over the used format (the DateInterval format). Only the due date is required all other parameters are optional. So you could just write: echo getOverdueString($dueDate); If the date is invalid or it couldn't calculate a diff the output will be '' (maybe change that to FALSE?) Another option could be to just return the DateInterval object and go from there since it provides you with handy properties for even more control than format() does.
  16. function getOverdueString($dueDate, $currentDate = 'now', $formatString = '%r%dd %hh %im') { try { $a = new DateTime($currentDate); $b = new DateTime($dueDate); $diff = $a->diff($b); if ($diff instanceof DateInterval) { return $diff->format($formatString); // formats: (positive) 1d 1h 0m (negative) -1d 3h 0m } } catch (Exception $e) {} return ''; } // echo getOverdueString( '2011-08-05 09:00:00', '2011-08-05 08:30:00'); // 0d 0h 30m
  17. If you already have a class say Database, you can create a postgresql class that extends your Database object and overrides all methods. class Database {/*mysql/mssql*/} class PostGreSQL extends Database {/*override all methods of Database*/} Better would be if you made Database an interface and then create a MySQL, MSSQL, and PostGreSQL class. It wouldn't take you more than just changing the original $db = new MySQL(..); To the new: $db = new PostGreSQL(..); If you are in to Design Patterns I suggest using a Factory to get the DB driver.
  18. I know C++ but not yet Python. I also guess my C++ will be quite rusty since it's been 5+ years since I last wrote it
  19. I like Spotify. I can't wait for this to be released! Will I also get 20 hours of free video then? Is there also a HTTP/FTP back-up for those who don't have a near P2P network? Will it be able to work with mirrors (P2P+HTTP+..)?
  20. The grass is always greener... So maybe their puzzles are more interesting then ours?
  21. I did in my editing session but my internet connection went dead and didn't come back on until now (after they replaced the modem). So here goes: @OP: <table> <tr> <?php for ($i = 0, $total = 0, $number = mt_rand(1,100); $i++ < 100/*cols*/; $total += $number, $number = mt_rand(1,100)): ?> <td style="background:<?php print !($number % 3) ? '#C00' : (!($number % 2) ? '#0C0' : '#FFF'); ?>"><?php print $number; ?></td> <?php endfor; ?> </tr> </table> Average: <?php print $total / 100/*cols*/; ?> A more expanded example would be: <table> <tr> <?php $total = 0; $number = mt_rand(1,100); // get a starting random number for ($i = 0; $i < 100/*number of cells you want to generate*/; ++$i) { $color = '#FFF'; // default color if (($number % 3) == 0) { // same as !($number % 3) $color = '#C00'; // red } else if (($number % 2) == 0) { $color = '#0C0'; // blue } print '<td style="background:' . $color . '">' . $number . '</td>'; $total += $number; $number = mt_rand(1,100); // new random number to feed the next iteration } ?> </tr> <caption>Average: <?php print $total / 100; ?></caption> </table> This will create 100 columns, if you want to create a specific set of rows/cols you should amend the code. The above examples should give you a good understanding of how you can generate the different cells with the numbers and calculate the average at the end.
  22. Each of the cells in the table will hold a random number between 1 and 100 (hint: look at mt_rand() function). If the number is a multiple of 3, display the cell with a red background color; if not a multiple of 3 but a multiple of 2, display the cell with a blue background color. Finally, display the average for all cells. <table> <tr> <?php for ($i = 0, $total = 0, $number = mt_rand(1,100); $i++ < 100/*cols*/; $number = mt_rand(1,100), $total += $number): ?> <td style="background:<?php print !($number % 3) ? '#C00' : (!($number % 2) ? '#0C0' : '#FFF'); ?>"><?php print $number; ?></td> <?php endfor; ?> </tr> </table> Average: <?php print $total / 100/*cols*/; ?>
  23. ignace

    Classes

    In procedural you would write something like: function login($username, $password) { global $user; if (login successfull) { $user = $userdata; } } In OO you would write: class User { private $data; public function login($username, $password) { if (login successfull) { $this->data = $userdata; } } } The difference is that in procedural: 1. You do not control access to the $user variable which allows name collissions with 3rd party scripts and thus unexpected/uncontrolled behavior in your code. 2. Your function becomes highly dependent on the context it is used in (only works if a $user variable exists). This is also why they advice you not to use global variables inside your functions.
  24. ignace

    Classes

    Like thorpe said classes encapsulate behavior and state. Without classes you would be looking at global variables and functions that manipulate those variables. However you do not control access to those global variables so the variables your functions depend on can be modified by any 3rd party script you introduce since both you and the 3rd party script happen to have a $user variable. To avoid any name collisions you would use a class or now with 5.3 namespaces (although namespaces are not entirely safe).
  25. This topic has been moved to PHP Freelancing. http://www.phpfreaks.com/forums/index.php?topic=339119.0
×
×
  • 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.