Archive for December, 2008

Random image from folder – rotating banner in php

So the other day someone was looking for a quick php script that would load a random image from a folder (rotating banner script) and display it on their website. There are already a ton of scripts out there that do this but of course, this was an excuse to build something for fun. So, I decided to create a script that would do just this but with the least amount of code lines still keeping it efficient (again, just for fun). This is what I came up with:

<?php
$files = @opendir($dir=trim($_REQUEST[‘dir’])) or die(‘Not Valid’);
while($file = readdir($files)) preg_match(‘/(.png)|(.gif)|(.jpg)/i’,$file) ?
        $images[] = array($file,substr($file,strlen($file)-3)) : null;
count($images) ? header(‘Content-type: image/’.$images[$id=rand(0,count($images)-1)][1]) :
        die(‘Not Valid’);
print file_get_contents($dir.(substr($dir,strlen($dir)-1)!=‘/’?‘/’:null).$images[$id][0]);
?>

Read the rest of this entry »

Tags: , , , , , , ,

Getting list of members from an event in meetup.com with PHP

So, last week I attended our monthly Atlanta PHP meet and had a good time. At the end of the session some prizes were awarded “randomly”. Why “randomly”? Because a member of the group wrote down two names on a piece of paper and started asking the other members for a number. The two to get the numbers correct won an award. Now, this was a quick last minute thing for fun, so I am definitely over engineering this – but hey, that’s what makes programming fun!

The problems were:

  • (and I apologize for my honesty in case any member that attended the event reads this) I could see the persons hand movement while writing down the numbers so I already knew one of the numbers before starting. 
  • The paper was lifted at some point before he started calling out for numbers and I could see the second number written on the paper.
  • It’s not truly random

Read the rest of this entry »

Tags: , , , , , , , ,

ob_start(), ob_flush(), flush(), set_time_limit() – Give user feedback during execution

So every once in a while I want/need to write some code that may take some time to execute. The problem is because it takes so long to execute, I need to make sure to give the user some feedback during this process so they know the page is working. If I hit submit in a page and nothing changes in my screen after 20-30 seconds I assume something is wrong. For example, I wrote a php page that would query multiple users in one db, then for each user load their xml data from another db and parse out the information to return a report for all users in the date range. Depending on the date range, this could take a few minutes to complete. This is where the php output buffer comes into play. Read the rest of this entry »

Tags: , , , , , ,

Atlanta PHP December Meeting – Singleton Design Pattern and Moodle

So I attended the Atlanta PHP meeting last week and it was fun just like the previous time. I find this particular group fun and knowledgeable making me look forward to the next meeting. The topics covered this time were the singleton design pattern and the Moodle course management system. It seems like we are going to start covering a design pattern plus another topic during each future meeting which I think is a great idea. You can never know enough about design patterns.

Singleton Pattern

So there wasn’t much said that was new to me about the singleton pattern. It’s pretty standard, the objective of this pattern is to prevent creating multiple instances of a class. The code example was something like this: Read the rest of this entry »

Tags: , , , , , ,