CMP Web Developer - Web Enthusiast - Web Loco

 

Posts for Category: PHP

  • This is inspired by a brief conversation I had with someone a few weeks back. Part of the discussion was about Ruby on Rails and why this company is moving from a PHP based solution to something that is Ruby on Rails based. I understand that our industry is in a constant state of flux and that the latest is always the greatest (for the most part) even though it might not actually be true. We hear buzz words and your clients or boss thinks it’s a must have. Just like AJAX. I hated when everyone was throwing out the word AJAX when they had absolutely no idea what it was or what it did. Rounded corners? Oh it’s AJAX! Sliding image galleries? It’s AJAX baby!! WooooooooT!!! Not really… Let me get back on track. So for the past few years everyone is captivated by using Ruby on Rails as their back-end solution. Let us remember that Rails is simply a framework for the Ruby language. Ruby itself has been around probably longer than I’ve been alive but the Rails framework is relatively ‘new’. What is great about Rails is that it does take care of a lot of things that are common to mostly all web applications ie: database connection/access, web form validation, publishing, etc… Our industry has been moving in this direction for some time now and it’s nothing new. Rails is essentially what .NET is to VB/C#; what the Zend/CakePHP frameworks are to PHP. A great group of folks got together to make web developer’s day to day tasks and responsibilities that much easier to create and maintain. So now here’s the real question. WHY move from PHP to Ruby? I personally do not like the Ruby language. There is something about the syntax that doesn’t appeal to me but again, its just my preference. So here are my top key thoughts as to why I’d stick to a PHP based solution:

    • With PHP you have the largest community of developers. This can be both good and bad because essentially you have all the worlds developers having a say in how to code a PHP solution. You’ll really have to research to find out where your good resources are. I love www.php.net
    • In regard to finding a reliable web hosting facility, you’ll find that the vast majority of hosts support PHP where Ruby support takes more time to find. It’s always better to have more options right?
    • The Rails framework noticeably performs slower than other interpreted languages from the articles that I’ve read.
    • IDE’s: Since PHP is such an established language there is a wide array of products at your disposal to choose from.
    • Ruby still isn’t 100% stable when integrating into Apache web server.
    So in conclusion, my choice is apparent: PHP. PHP is more available; it has a great user base and you can easily find developers for it.

  • Task: You need to display a calendar on your page(s) to use with any date driven funcitonality you may have. ie: TV Schedule by date Solution: PHP Calendar Functions to the rescue! Back in December of 2008 (from what I remember) PHP was given it’s own native functions to render popular calendar requests. resource link: Click here to read the documentation on PHP’s built in calendar functions: http://us3.php.net/manual/en/ref.calendar.php Implementation: I’ll state the obvious: Have a web server (Apache or IIS – yes…IIS) and PHP5 installed. Copy and paste the following PHP Calendar code into a new php file and save it to your host. <?php session_start(); function days_in_month($month, $year) { return $month 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31); } error_reporting(E_ALL); ini_set('display_errors', '0'); $date = time(); $realMonth = date('m', $date); $day = date('d', $date); $adjustedYear = ''; if (!isset($_SESSION['offSet'])) { $_SESSION['offSet'] = '0'; } if (!isset($_SESSION['year'])) { $realYear = date('Y', $date); $_SESSION['year'] = $realYear; $year = $realYear; } else { $year = $_SESSION['year']; } if (!isset($_SESSION['month'])) { $month = date('m', $date); $_SESSION['month'] = $month; } if (isset($_GET['changeOffset'])) { $adjustMonth = $_GET['changeOffset']; $adjustMonth = trim($adjustMonth); if ($adjustMonth ‘add’) { if ($_SESSION[‘month’] >= ‘12’) { $_SESSION[‘month’] = ‘1’; $_SESSION[‘year’] = 1; } else { $_SESSION[‘month’] += ‘1’; } $_SESSION[‘offSet’] += ‘1’; } elseif ($adjustMonth 'sub') { if ($_SESSION['month'] ‘1’) { $_SESSION[‘month’] = ‘12’; $_SESSION[‘year’] = 1; } else { $_SESSION[‘month’] -= ‘1’; } $_SESSION[‘offSet’] -= ‘1’; } } $month = $_SESSION[‘month’]; $year = $_SESSION[‘year’]; $monthOffset = $_SESSION[‘offSet’]; $specificMonth = $monthOffset; $first_day = mktime(0, 0, 0, $month, 1, $year); $title = date(‘F’, $first_day); $day_of_week = date(‘D’, $first_day); $today = date(‘d’); $currentMonth = date(‘F’, mktime(0, 0, 0, $month, $day, $year)); switch ($day_of_week) { case “Sun”: $blank = 0; break; case “Mon”: $blank = 1; break; case “Tue”: $blank = 2; break; case “Wed”: $blank = 3; break; case “Thu”: $blank = 4; break; case “Fri”: $blank = 5; break; case “Sat”: $blank = 6; break; } $days_in_month = days_in_month($month, $year); ?> <div class=“mini-calendar”> <div class=“mini-calender-show-hide”> <div class=“month-option clearFix”> <div class=“month-properties” id=“month-properties”></div> <span class=“previous-month” id=“previous-month”><a href=”?changeOffset=sub” title=”?changeOffset=sub”>previous</a></span> <span class=“current-month” id=“current-month”><?php echo $currentMonth . ‘ ‘ . $year; ?></span> <span class=“next-month” id=“next-month”><a href=”?changeOffset=add” title=”?changeOffset=add”>next</a></span> </div> <span class=“time-zones clearFix”><span class=“header”>Time Zone:</span><span><a href=”?tz=ET”>ET</a></span><span><a href=”?tz=CT”>CT</a></span><span><a href=”?tz=MT”>MT</a></span><span><a href=”?tz=PT”>PT</a></span></span> <div class=“day-header-row clearFix”><div class=“day-data”><span>S</span></div><div class=“day-data”><span>M</span></div><div class=“day-data”><span>T</span></div><div class=“day-data”><span>W</span></div><div class=“day-data”><span>T</span></div><div class=“day-data”><span>F</span></div><div class=“day-data”><span>S</span></div> </div> <div class=“days-container” id=“days-container”> <?php $miniCalDayCount = 1; echo PHP_EOL . ‘<div class=“row clearFix”>’ . PHP_EOL; while ($blank > 0) { $defaultDayBlock = ‘<div class=“empty-day”>&#160;</div>’ . PHP_EOL; $defaultLastDays = ‘<div class=“last-day”>&#160;</div>’ . PHP_EOL; echo $defaultDayBlock; $blank = $blank 1; $miniCalDayCount+; } $day_num = 1; while ($day_num <= $days_in_month) { $dayCheck = mktime(0, 0, 0, $month, $day_num, $year); $dayCheckFinal = date(‘D’, $dayCheck); if ($dayCheckFinal == ‘Sun’) { $borderStyle = ‘border-left’; } else { $borderStyle = ‘’; } $smallCal = ‘<div class=“day-data”><span><a href=”?view=day&day=’ . $day_num . ‘”>’ . $day_num . ‘</a></span></div>’ . PHP_EOL; echo $smallCal; $day_num++; $miniCalDayCount++; if ($miniCalDayCount > 7) { echo ‘</div>’ . PHP_EOL . ‘<div class=“row clearFix”>’ . PHP_EOL; $miniCalDayCount = 1; } } while ($miniCalDayCount > 1 && $miniCalDayCount <= 7) { echo $defaultLastDays; $miniCalDayCount++; } ?> </div> </div> </div> </div> Here’s a working example of the following code: example

  • Task: You want to easily display your favorite RSS or Atom feed on your site (or any site for that matter). Solution: PHP + JavaScript Click here to view the complete source code for this solution. Parsing files on the back-end is always a faster approach. Parsing documents with JavaScript always puts it on the client and performance varies entirely too much for me to use only a front-end approach. (Thanks Will for that point). Implementation: Create a new <script> node/tag within the DOM and point it’s source to the PHP script. ie: <script type=“text/javascript” src=“http://www.workofcesar.com/AMC-TV/Film-Critic/rss-reader/rss-reader.php”></script> Next you’ll create another <script> node/tag and instantiate an instance of the ‘CMP_RSS’ object. This object expects a few values: * feedURL = target rss/atom feed URL ie: <a href="http://rss.news.yahoo.com/rss/topstories" target="_blank">http://rss.news.yahoo.com/rss/topstories</a> * entryLimit = number of rss entries you’d like to display on your page * targetElement = the node from the DOM which you'd like to append the entries to * cssClassName = the css class for the rss entries container * @optionalCSS = will inject a new <link>

    node into the DOM when provided with a URL ie: http://my-server-name.com/css-foler/my-css-file.css
    <script type=“text/javascript”> varmyNewRSSReader = new CMP_RSS(‘http://rss.news.yahoo.com/rss/tech’, ‘4’, ‘test’, ‘cmp’,‘http://www.cmpcreations.com/css/some-css-file.css’); </script>
    Click here to see a working example By the way, yes, you can define a <script> src attribute to another file type other than ‘.js’. It could be src=‘my-file-name.whateverTheHellYouWant’ if you wanted to.

Copyright © 2004 - 2012 CMP Creations, LLC All rights reserved! Don't copy or steal stuff!