<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">
<title>Galeria obrazów</title>
</head>
<body>
<table align="center" border="0">
<tr>

<?php
/*Skrypt pochodzi z ksi±¿ki "PHP. 101 praktycznych skryptów"
  autor: Marcin Lis                    http://marcinlis.com*/

$imgDir = "./images";
$thumbDir = "./thumbnails";
$thOnPage = 4;

if(isSet($_GET['iid']) && isSet($_GET['pid'])){
  $iId = $_GET['iid'];
  $pId = $_GET['pid'];
}
else{
  $iId = 0;
  $pId = 0;
}

$dir = scandir($thumbDir);
array_shift($dir);
array_shift($dir);

$count = count($dir);
$pages = ceil($count / $thOnPage);

if(!is_numeric($iId) || !is_numeric($pId)){
  $iId = 0;
  $pId = 0;
}

if($iId < 0 || $iId >= $count || $pId < 0 || $pId > $pages){
  $iId = 0;
  $pId = 0;
}

for($i = 0; $i < $thOnPage; $i++){
  $imgNo = $pId * $thOnPage + $i;
  if($imgNo >= $count) break;
  $imgName = $dir[$imgNo];
  $imgTag = "<img src=\"$thumbDir/$imgName\" alt=\"$imgName\" />\n";
  $aHead = "<a href=\"./galeria.php?pid=$pId&amp;iid=$imgNo\">";
  $aFoot = "</a>";
  echo "<td>$aHead $imgTag $aFoot</td>\n";
}
$currThCount = $i;
?>

</tr>
<tr>
  <td colspan="<?php echo "$currThCount" ?>" align="center">
    Id¼ do strony: 
    <?php
    for($i = 0; $i < $pages; $i++){
      $link = "<a href=\"./galeria.php?pid=$i&amp;iid=$iId\">$i</a>&nbsp;";
      echo $link;
    }
    ?>
  </td>
</tr>

<tr>
  <td colspan="<?php echo "$currThCount" ?>" align="center">
    <?php
    $imgName = $dir[$iId];
    $imgTag = "<img src=\"$imgDir/$imgName\" alt=\"$imgName\" />\n";
    echo $imgTag;
    ?>
  </td>
</tr>
</table>

</body>
</html>
