Jump to content

My code is looping weirdly


SilverBlade86

Recommended Posts

Basically, I have a piece of code which would display all directories and subdirectories in it.

 

However, it is displaying the folders in the wrong order! I have different directories for different years in my "photos" folder, but it seems to be displaying them in the wrong order.

 

Here is my code:

$year = $_GET["year"];
$event = $_GET["event"];

$columnlength = 3;

//Original directory
$path = "/photos" ;

//if year is empty
if (!$year) {
echo "Which year do you wish to view? <br> <br>";
}
else {
$path = $path . "/" . $year;


if (!$event) {
echo "Which event do you wish to view? <br> <br>";
}
else {
$path = $path . "/" . $event;
}

}

if (($year) && ($event)) {
$path = $path. "/" . "images";
echo "Year: $year <BR> Event: $event <BR> <BR>";
echo "<TABLE BORDER=\"0\" ALIGN=\"CENTER\"> <TR>";
}

$dir_handle = @opendir($path) or die("Unable to open $path");

$increment = 1;

// Loop through the files
while (false !== ($file = readdir($dir_handle))) {
// do not display current file

if (($file != ".") && ($file != "..")) {

if (is_dir($path."/".$file)) {

if (!$year) {
echo "<a href = \"?page=photos&year=$file\">$file</a> <br>";
}
elseif (!$event) {
echo "<a href = \"?page=photos&year=$year&event=$file\">$file</a> <br>";
}
}

//if both year and event variables present, display thumbnails, which link to the main images
if (($year) && ($event)) {

$extension = substr($file, strrpos($file, "."));

if (($extension == ".jpg") || ($extension == ".jpeg")) {

$photourl = "/photos/$year/$event/images/$file";
$thumburl = "/photos/$year/$event/thumbs/$file";

echo "<TD>";

echo "<a href=\"$photourl\" target=\"_blank\"><img src=\"$thumburl\" alt=\"Picture\"></a> <br>";

echo "</TD>";

if ($increment == $columnlength) {
echo "</TR>";
echo "<TR>";
$increment = 0;
}

$increment = $increment + 1;
}

}
}
}

echo "</TABLE>";

// Close directory
closedir($dir_handle);

 

I would appreciate it if someone could point out the problem to me.

 

The weird part is that it works perfectly fine on my computer (I have installed Apache and SQL and whatnot) but it messes up when it is on the web server.

Link to comment
https://forums.phpfreaks.com/topic/92681-my-code-is-looping-weirdly/
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.