Jump to content

image gallery works when reading from directory but not from database..why??


php_beginner_83

Recommended Posts

Hi Everyone

 

I'm working on a photo gallery for my website and it's driving me mental.

I have code and it works perfectly when I pass it a directory on my computer, however, when I try and use the same code and read info from my database into it, it won't work.  It will display the thumbnails and the first image but when I click on a thumbnail or the 'next' or 'previous' buttons it doesn't work.  Can anyone tell me why this is???  I'm ready to break the computer at this point..but I know it's my fault...arghhh!!

 

This is the code that works....

 

<?php	
$photoDir = "images";
$thumbDir = "thumbs";

$previousLinkText = "<";
$nextLinkText = ">";
$firstLinkText = "«";
$lastLinkText = "»";

$handle = opendir($photoDir);
$counter = 0;
$imagePath = array();

while($file = readdir($handle))
{
// check $file is a directory
if (!is_dir($file))
{
	// Set $imageSize
	$imageSize = getimagesize($photoDir . "/" . $file);

	// If it's an image...
	if ($imageSize[3] != "") 
	{
		// put file name into array
		$imagePath[$counter] = $file;
		$counter++;
	}
}
}

closedir($handle);

$imgIndex = $_GET['image'];
if (!isset($imagePath[$imgIndex]))
{
$imgIndex = 0;
}

echo "<div id=thumbs>\n";

// loop through array of images
for($i=0; $i<sizeof($imagePath); $i++)
{
//$thumbSize = getimagesize($thumbDir . "/" . $imagePath[$i]);
//$thumbWidth = $thumbSize[0];
//$thumbHeight = $thumbSize[1];

echo "   <a href=\"?image=" . $i . "\">\n";
echo "    <img src=\"" . $thumbDir . "/" . $imagePath[$i] . "\" alt=\"" . $imagePath[$i] . "\" />\n";

		echo "   </a>\n";

}

echo "</div>";
echo "  <div id=photo>";

if ($imgIndex == "0")
{
// display first image
echo "   <img src=\"" . $photoDir . "/" . $imagePath[$imgIndex] . "\" title=\"" . $imagePath[$imgIndex] . "\"/>\n";

}
else
{
echo "   <img src=\"" . $photoDir . "/" . $imagePath[$imgIndex] . "\" title=\"" . $imagePath[$imgIndex] . "\"/>\n";
echo "</div>";
}	
echo "<div id=notes>";
echo "   <a href=\"?image=0\" title=\"First\">" . $firstLinkText . "</a>\n";
echo "   <a href=\"?image=";
	if ($imgIndex > 0) echo ($imgIndex-1);
		else 
	echo ($counter-1);
echo "\" title=\"Previous\">" . $previousLinkText . "</a>\n";

echo "   <a href=\"?image=";
		if ($imgIndex != sizeof($imagePath)-1) echo ($imgIndex+1);
		else echo "0";
		echo "\" title=\"Next\">" . $nextLinkText . "</a>\n";
		// Last image link
		echo "   <a href=\"?image=" . ($counter-1) . "\" title=\"Last\">" . $lastLinkText . "</a>\n";
echo "</div>";

?>

 

THis is the code that doesn't work (I use this file as an include, would that make a difference??)..

 

<?php
...code to connection to database...

//arrays to hold the titles, descriptions and paths separately
$desc = array();
$imagePat = array();

$previousLinkText = "<";
$nextLinkText = ">";
$firstLinkText = "«";
$lastLinkText = "»";

$cnt = 0;
//fetch tha data from the database
while ($row = mysql_fetch_array($result , MYSQL_NUM ))
{

   $desc[$cnt] = $row[1];
   $imagePath[$cnt] = substr(strrchr($row[2],92),1);

   $cnt++;
   
}

$imgIndex = $_GET['image'];

if (!isset($imagePath[$imgIndex]))
{
$imgIndex = 0;
}

echo "<div id=thumbs>\n";
$thumbDir = "thumbs";
$photoDir = "images";

// loop through array of images
for($i=0; $i<sizeof($imagePath); $i++)
{

echo "   <a href=\"?image=" . $i . "\">\n";
echo "    <img src=\"" . $thumbDir . "/" . $imagePath[$i] . "\" alt=\"" . $imagePath[$i] . "\" />\n";
		echo "   </a>\n";

}

echo "</div>";

echo "  <div id=photo>";

if ($imgIndex == "0")
{
// display first image
echo "   <img src=\"" . $photoDir . "/" . $imagePath[$imgIndex] . "\" title=\"" . $imagePath[$imgIndex] . "\"/>\n";

}
else
{
echo "   <img src=\"" . $photoDir . "/" . $imagePath[$imgIndex] . "\" title=\"" . $imagePath[$imgIndex] . "\"/>\n";
echo "</div>";
}	
echo "<div id=notes>";
echo "   <a href=\"?image=0\" title=\"First\">" . $firstLinkText . "</a>\n";
echo "   <a href=\"?image=";
	if ($imgIndex > 0) echo ($imgIndex-1);
		else 
	echo ($cnt-1);
echo "\" title=\"Previous\">" . $previousLinkText . "</a>\n";

echo "   <a href=\"?image=";
		if ($imgIndex != sizeof($imagePath)-1) echo ($imgIndex+1);
		else echo "0";
		echo "\" title=\"Next\">" . $nextLinkText . "</a>\n";
		// Last image link
		echo "   <a href=\"?image=" . ($cnt-1) . "\" title=\"Last\">" . $lastLinkText . "</a>\n";
echo "</div>";

?>

 

I'd really appreciate any help.

This is the output I get when I print_r($imagePath).....

 

Array ( [0] => alcatraz1.jpg [1] => alcatraz2.jpg [2] => alcatraz3.jpg [3] => alcatraz4.jpg [4] => alcatraz5.jpg [5] => alcatraz6.jpg [6] => alcatraz7.jpg [7] => alcatraz8.jpg [8] => alcatraz9.jpg [9] => alcatraz10.jpg [10] => alcatraz11.jpg [11] => alcatraz12.jpg [12] => alcatraz13.jpg [13] => alcatraz14.jpg [14] => alcatraz15.jpg )

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.