Bookcafe

Saturday, October 16, 2010

PHP Simple Image Gallery - The Success (Images Sorted In Table Manner)

As refer to my previous posting mission, I finally manage to create a simple image gallery and the most significant thing is that, images are sorted in table manner.

I used the arithmetics function MOD logic to help me to sort the images. In PHP, MOD can be represented as % and I used the MOD of $i+ % 5 since it will render incremental numbers i.e 1,2,3,4,5 and so on.
Number 5 will determine the number of column that you will get in your table. You can change this to other number such as 7 as long as the outcomes are 1,2,3,4,5 and so on.

The PHP codes are


$images = glob("" . $directory . "*.jpg");
//Count the number of files in the directory so we can use the for loop 
$kira = count(glob("" . $directory . "*.jpg"));
//Print the total number of files 
echo "Jumlah Gambar: $kira";

 echo "<table width='100' align='center'>";
 echo "<tr>";
 for($i=0; $i<$kira; $i=$i+1)
 {
  if($i % 5 >= 1 && $i % 5 <= 3 )
  {
 
   echo "<td style='background-color:blue'>";  
   echo $images[$i];
   echo "<br>";
   echo "<img border=\"0\" src=\"$images[$i]\" width=\"150\" height=\"100\">";
   echo "<br>";
   echo "</td>";
   
  }
  
         if($i % 5 >= 4 && $i % 5 <= 6)
  {
   
   echo "<td style='background-color:red'>";   
   echo $images[$i];
   echo "<br>";
   echo "<img border=\"0\" src=\"$images[$i]\" width=\"150\" height=\"100\">";
   echo "<br>";
   echo "</td><tr>";
   
  }
     if($i % 5 == 0)
  {
   
   echo "<td style='background-color:green'>";   
   echo $images[$i];
   echo "<br>";
   echo "<img border=\"0\" src=\"$images[$i]\" width=\"150\" height=\"100\">";
   echo "<br>";
   echo "</td>";
   
  }

 }
 echo "</table>";
?>

No comments:

Post a Comment