How to Build a CMS

A CMS or Content Management System is a type of web site that allows the user to edit content without having to know HTML. Standard blogging software like WordPress is basically a CMS that displays the content in reverse chronological order. On that note here is a very cool technique using PHP, HTML, JavaScript and a very cool, free, text editor called TinyMCE. This technique will show you how to build a CMS or content management system for a website that stores the content in text files.There are definitely better ways to do this but I am keeping it as simple as possible to show the technique in its most basic form. Continue reading “How to Build a CMS”

Dynamically Display Images From a Directory

Here is a nifty PHP script I use on occasion. It reads a specified directory that you have populated with images, in this case .gif images then spits them out onto a nice 4 column table.

I used this for a page that displays customer logos. To add a new logo all the client has to do is size the image, make sure it’s a gif then drop it into the proper directory, done. To remove an image, just delete it from the directory. No code.

The client wanted to order 10 of the logos first and the rest alphabetically so I came up with the easiest solution I could think of witch was to put a sort on the array of file names before writing them to HTML then coming up with a file naming scheme that they could follow. I named the logos by the company name and just put a four digit number in front of the filenames they wanted to be ordered first. They started with 0010_name.gif then 0020_name.gif etc… This way if a new one came in that they wanted first they could name it 0015_name.gif.

<table cellpadding="0" cellspacing="0" border="0">
<tr>
<?
  $dir = 'path to directory';
  $dirHandle = opendir($dir);
  $image = "";
  while ($file = readdir($dirHandle)) {
    if(strpos($file, '.gif')>0) {
      $count++;
      $image[$count] = array ($file);
    }
  }
  sort($image);
  closedir($dirHandle);
  $arrays = count($image) -1;
  $loop = -1;
  $c = 0;
  while ($loop < $arrays) {
    $loop++;
    if ($c == 4) {
      echo '</tr><tr><td><img src="'.$dir.'/'.$image[$loop][0].'"></td>';
      $c = 0;
    } else {
      echo '<td><img src="'.$dir.'/'.$image[$loop][0].'"></td>';
    }
    $c++;
  }
  if ($c==1) {echo '<td colspan="3"></td>';}
  if ($c==2) {echo '<td colspan="2"></td>';}
  if ($c==3) {echo '<td></td>';}
  if ($c==4) {echo '';}
?>
</tr>
</table>