Jump to content

PHP image gallery


servvs

Recommended Posts

Ok so heres the deal, I want a lightweight image gallery that reads from a specified directory. I have started working on one but I just can't get past one thing. I am not really sure what I am doing wrong. I have it list the pictures correctly but when I try to modify them into a table or div so I can start working on a simple template it screws up horribly. No matter what I make the width of the table or div the pictures just stay in one line and go all the way across the screen. Here is my script w/o the div/table

 

<?php

$d = dir("./");
$pattern="(\.gif$)|(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)";

while (false !== ($entry = $d->read()))
{
if(eregi($pattern, $entry))
   echo '<a border="0" href="index.php?image='.$entry.'"><img border="0" width="100" height="100" src="'.$entry.'"</a>';
}

if (is_null($HTTP_GET_VARS['image'])){ 
$image = "<img src='1-dna005.jpg'> ";
} else {
$image = "<img src='$HTTP_GET_VARS[image]'><br>{$HTTP_GET_VARS['image']}";
}

echo "$image";

$d->close();


?>

 

and here it is with a div/table

 

<?php

$d = dir("./");
$pattern="(\.gif$)|(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)";

echo '<table width="500"><tr><td>';

while (false !== ($entry = $d->read()))
{
if(eregi($pattern, $entry))
   echo '<a border="0" href="index.php?image='.$entry.'"><img border="0" width="100" height="100" src="'.$entry.'"></a>&nbsp';
}

echo '</td></tr></table>';

if (is_null($HTTP_GET_VARS['image'])){ 
$image = "<img src='1-dna005.jpg'> ";
} else {
$image = "<img src='$HTTP_GET_VARS[image]'><br>{$HTTP_GET_VARS['image']}";
}

echo "$image";

$d->close();


?>

Link to comment
https://forums.phpfreaks.com/topic/107878-php-image-gallery/
Share on other sites

Well, I'm too ADD to look through your code much, but when you say that you are trying to list it in a table (also something I'm working on), and that they just shoot horizontally accross the screen, it leads me to believe that you have forgotten that you need to start a new row (<tr>) and a new data cell into it (<td>). This is all assuming that you are trying to get it to go vertically (the listings).

 

FlyingIsFun1217

Link to comment
https://forums.phpfreaks.com/topic/107878-php-image-gallery/#findComment-553010
Share on other sites

Here is what I use to make a dynamic table:

 

columnLimit is how many columns across you want obviously.

 

<?php
$table = "<table width=\"0\" border=\"0\" cellspacing=\"5\" cellpadding=\"5\">\n";
$table .= "<tr>\n";
$count = 0;
$columnLimit = 5;
while($row = mysql_fetch_array($sql)) {
$count += 1;
if($count == $columnLimit) {
	//if your at the last columd, add a new row
	$table .= "<td>your image</td>\n";
	$table .= "</tr>\n<tr>\n";
	$count = 0;
} else {
	//else just add a new cell
	$table .= "<td>your image</td>\n";
}
}
if($count != $columnLimit) {
$table .= "</tr>\n";
}
$table .= "</table>\n";
echo $table;
?>

Link to comment
https://forums.phpfreaks.com/topic/107878-php-image-gallery/#findComment-553020
Share on other sites

you can use this to count how many files are in your folder:

<?php 
$dir = '/path/to/directory/'; 
$filecount = 0; 
$d = dir($dir); 
while ($f = $d->read()) { 
if(($f!= ".") && ($f!= "..")) { 
	if(!is_dir($f))
	$filecount++;
} 
} 
echo 'there are ',$filecount,' files in this folder'; 
?>

 

now $filecount is the variable you use for your for loop so like this:

<?php
$dir = '/path/to/directory/'; 
$filecount = 0; 
$d = dir($dir); 
while ($f = $d->read()) { 
if(($f!= ".") && ($f!= "..")) { 
	if(!is_dir($f))
	$filecount++;
} 
} 
$table = "<table width=\"0\" border=\"0\" cellspacing=\"5\" cellpadding=\"5\">\n";
$table .= "<tr>\n";
$count = 0;
$columnLimit = 5;
for($i = 0; $i<=$filecount; $i++) {
$count += 1;
if($count == $columnLimit) {
	//if your at the last columd, add a new row
	$table .= "<td>your image</td>\n";
	$table .= "</tr>\n<tr>\n";
	$count = 0;
} else {
	//else just add a new cell
	$table .= "<td>your image</td>\n";
}
}
if($count != $columnLimit) {
$table .= "</tr>\n";
}
$table .= "</table>\n";
echo $table;
?>

Link to comment
https://forums.phpfreaks.com/topic/107878-php-image-gallery/#findComment-553218
Share on other sites

so it should look something like this

 

<?php
$dir = './'; 
$filecount = 0; 
$d = dir($dir); 
while ($f = $d->read()) { 
if(($f!= ".") && ($f!= "..")) { 
	if(!is_dir($f))
	$filecount++;
} 
} 
$table = "<table width=\"0\" border=\"0\" cellspacing=\"5\" cellpadding=\"5\">\n";
$table .= "<tr>\n";
$count = 0;
$columnLimit = 5;
for($i = 0; $i<=$filecount; $i++) {
$count += 1;
if($count == $columnLimit) {
	//if your at the last columd, add a new row
	$table .= "<td>$f</td>\n";
	$table .= "</tr>\n<tr>\n";
	$count = 0;
} else {
	//else just add a new cell
	$table .= "<td>$f</td>\n";
}
}
if($count != $columnLimit) {
$table .= "</tr>\n";
}
$table .= "</table>\n";
echo $table;
?>

 

Im still not getting it, lol

Link to comment
https://forums.phpfreaks.com/topic/107878-php-image-gallery/#findComment-553249
Share on other sites

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.