here is my page which reads product files from a directory and shows them on dynamic created multiple pages.
Some info: $xml is my class which reads xml product files, included 'general*.php" files are the header, footer and some functions (classes declarations etc.).
If you do not understand some of my code (because of uncommented parts or of the poor optimization please tell me).
<?php
require_once('cookie.php');
?>
<html>
<head>
<?php
require_once('general1.php');
?>
<title>
Welcome to ProArtist - Where Art Meets You
</title>
<?php
require_once('general2.php');
?>
</head>
<body>
<?php
require_once('general3.php');
echo "<tr><td colspan=\"6\" align=\"left\" valign=\"top\">";
$handle = opendir(PRODUCTS_DIR);
while(false != ($file = readdir($handle)))
{
if(strstr($file, '.gz'))
{
$filename[] = PRODUCTS_DIR.$file;
}
}
closedir($handle);
if(!isset($_SESSION["product_list"]))
{
while($file = array_shift($filename))
{
//opens and reads data file in $src
$fp = gzopen($file, 'rb');
if(!$fp)
{
ShowMessageBox('FILE='.__FILE__.'\nLine='.__LINE__.'\nUnable to open '.$file);
exit(0);
}
$src = gzread($fp, MAX_FILESIZE);
if(!$src)
{
ShowMessageBox('FILE='.__FILE__.'\nLine='.__LINE__.'\nUnable to read from '.$file);
exit(0);
}
gzclose($fp);
//creates and parses an XML
$xml = new XML;
$result = $xml->Create($src, NULL);
if($result != 'ok')
{
ShowMessageBox('FILE='.__FILE__.'\nLine='.__LINE__.'\nxml.Create() error -> '.$result);
exit(0);
}
//reads the artist
$result = $xml->SetCurrentNode($xml->root, 'artist', 0);
if($result != 'ok')
{
ShowMessageBox('FILE='.__FILE__.'\nLine='.__LINE__.'\nxml.SetCurrentNode() error -> '.$result);
exit(0);
}
$artist = $xml->GetNodeContent();
//reads the category
$result = $xml->SetCurrentNode($xml->root, 'category', 0);
if($result != 'ok')
{
ShowMessageBox('FILE='.__FILE__.'\nLine='.__LINE__.'\nxml.SetCurrentNode() error -> '.$result);
exit(0);
}
$category = $xml->GetNodeContent();
//checks for the category and artist and skips the product if necessary
if(($artist != $_SESSION["current_artist"]) && ($category != 'New Testament'))
{
unset($xml);
continue;
}
//reads the title
$result = $xml->SetCurrentNode($xml->root, 'title', 0);
if($result != 'ok')
{
ShowMessageBox('FILE='.__FILE__.'\nLine='.__LINE__.'\nxml.SetCurrentNode() error -> '.$result);
exit(0);
}
$title = $xml->GetNodeContent();
//reads the id
$result = $xml->SetCurrentNode($xml->root, 'id', 0);
if($result != 'ok')
{
ShowMessageBox('FILE='.__FILE__.'\nLine='.__LINE__.'\nxml.SetCurrentNode() error -> '.$result);
exit(0);
}
$id = $xml->GetNodeContent();
//reads the imagename
$result = $xml->SetCurrentNode($xml->root, 'image', 0);
if($result != 'ok')
{
ShowMessageBox('FILE='.__FILE__.'\nLine='.__LINE__.'\nxml.SetCurrentNode() error -> '.$result);
exit(0);
}
$imagename = $xml->GetNodeContent();
$imagename = 'images/products/'.basename($imagename, '.jpg').'low.jpg';
//deletes $xml object
unset($xml);
$_SESSION["product_list"][] = array("title" => $title, "id" => $id, "imagename" => $imagename);
}
sort($_SESSION["product_list"]);
$current_type = '';
for($i=0; $i<sizeof($_SESSION["product_list"]); $i++)
{
$value = $_SESSION["product_list"][$i];
$type = explode(" - Version No.", $value["title"]);
$type = $type[0];
if($current_type != $type)
{
$_SESSION["index_themes"][] = $i;
$current_type = $type;
}
}
$_SESSION["index_themes"][] = $i;
}
if(!isset($_SESSION["high_theme"]))
{
$_SESSION["low_theme"] = 0;
$_SESSION["high_theme"] = MAX_THEMES_PER_PAGE-1;
}
if(isset($_POST["previous"]))
{
$_SESSION["low_theme"] -= MAX_THEMES_PER_PAGE;
$_SESSION["high_theme"] -= MAX_THEMES_PER_PAGE;
unset($_POST["previous"]);
}
else if(isset($_POST["next"]))
{
$_SESSION["low_theme"] += MAX_THEMES_PER_PAGE;
$_SESSION["high_theme"] += MAX_THEMES_PER_PAGE;
unset($_POST["next"]);
}
/*
if(isset($_GET["btn_page"]))
{
if($_GET["btn_page"] == "next")
{
$_SESSION["low_theme"] += MAX_THEMES_PER_PAGE;
$_SESSION["high_theme"] += MAX_THEMES_PER_PAGE;
}
else if($_GET["btn_page"] == "previous")
{
$_SESSION["low_theme"] -= MAX_THEMES_PER_PAGE;
$_SESSION["high_theme"] -= MAX_THEMES_PER_PAGE;
}
unset($_GET["btn_page"]);
}
*/
echo $_SESSION["low_theme"]." , ".$_SESSION["high_theme"]."<br>";
echo "<h3>Click on a image for a full description</h3>";
echo "<table border=\"0\">";
for($i=0; $i<sizeof($_SESSION["index_themes"])-1; $i++)
{
if($i < $_SESSION["low_theme"])
{
continue;
}
if($i > $_SESSION["high_theme"])
{
break;
}
$value = $_SESSION["product_list"][$_SESSION["index_themes"][$i]];
$title = explode(" - Version No.", $value["title"]);
$title = $title[0];
$nr_cells = 0;
echo "<tr><td align=\"center\" colspan=\"6\"><h2>".($i+1).". ".$title."</h2></td></tr>";
echo "<tr align=\"center\" valign=\"middle\">";
for($j=0; $j<($_SESSION["index_themes"][$i+1]-$_SESSION["index_themes"][$i]); $j++)
{
$nr_cells++;
if(($nr_cells % (MAX_VERSIONS_PER_ROW+1)) == 0)
{
echo "</tr>";
echo "<tr align=\"center\" valign=\"middle\">";
}
$value = $_SESSION["product_list"][$_SESSION["index_themes"][$i]+$j];
echo "<td width=\"190\" height=\"230\">";
echo "<a href=\"product.php?id=".$value["id"]."\">";
echo "<img alt=\"".$value["title"]."\" border=\"0\" name=\"button".$value["id"]."\"
onmouseover=\"window.status = '".$value["title"]."'; document.button".$value["id"].".border='2'; return true;\"
onmouseout=\"window.status = ''; document.button".$value["id"].".border='0'; return true;\"
src=\"".$value["imagename"]."\"/></a>";
echo "</td>";
}
echo "</tr>";
echo "<tr><td colspan=\"10\"><hr size=\"5\" noshade></td></tr>";
}
echo "</table>";
/
echo "<tr>";
echo "<td align=\"left\" colspan=\"3\">";
if($SESSION["low_theme"] != 0)
echo "<a href=\"".$SERVER['PHP_SELF']."?btn_page=previous\">Previous Page</a>";
echo "</td>";
echo "<td align=\"right\" colspan=\"3\">";
if($SESSION["high_theme"] != (sizeof($SESSION["index_themes"])-1))
echo "<a href=\"".$_SERVER['PHP_SELF']."?btn_page=next\">Next Page</a>";
echo "</td>";
echo "</tr>";
/
echo "<tr>";
echo "<form name=\"pageform\" action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">";
echo "<td align=\"left\" colspan=\"3\">";
if($_SESSION["low_theme"] != 0)
echo "<input type=\"submit\" name=\"previous\" value=\"Previous Page\">";
echo "</td>";
echo "<td align=\"right\" colspan=\"3\">";
if($_SESSION["high_theme"] != (sizeof($_SESSION["index_themes"])-1))
echo "<input type=\"submit\" name=\"next\" value=\"Next Page\">";
echo "</td>";
echo "</form>";
echo "</tr>";
echo "</td></tr>";
require_once('general4.php');
?>
</body>
</html>