simple php image displaying example

MadJuggla9

2[H]4U
Joined
Oct 9, 2002
Messages
3,515
what im doing is making a page with an iframe; the iframe contains a picture from a directory "/images" beneath the picture i have a previous and next button. each time one of the links are clicked, i target the iframe to load the previous/next image accordingly.

i have a very similar script in perl that returns the number of jpgs in the directory and sets up links accordingly. Im trying to do this with php. i understand the logic, im just learning php and getting used to variables in the link.

any help would be appreciated. ive searched google and there are a lot of complicated examples, im just looking for something generic with no features whatsoever.
 
actually i just did something similar not too long ago. i ended up not liking how it worked with my specific website (it has to do with how i have overall my site navigation setup) so took it out. i dug this out of my old code list, I "think" this was my final version, i may have a couple syntax errors in there.
i use MSSQL, so if you're using another DB, change the functions as needed..also, you may need to change the links to target your iframe.


Code:
//this file name is "display.php"

<?php
require("connection string");

$ID = $_GET['ID']; //get ID of image to display

$SQLstr = "SELECT * FROM pictures ORDER BY ID DESC";

$rs = mssql_query($SQLstr,$conn); //open recordset

$num_rows = mssql_num_rows($rs); //get number of records

for ($i=0; $i < $num_rows; $i++)
{
mssql_data_seek($rs,$i);//go to position of current picture
$curr_record = mssql_fetch_object($rs); //grab current picture
if ($curr_record->ID == $ID) //if we at the selected image record
{
  if (($i - 1) >= 0) //if we are not at the very first picture, show link the previous
  {
    mssql_data_seek($rs,$i-1);
    $prev_record = mssql_fetch_object($rs); //get previous picture
    echo ("<a href=\"display.php?ID=" . $prev_record->ID . "\">prev</a>"); //show link to       previous image
   }

echo ("<img src=\"/IMAGES/" . $curr_record->name . ".jpg\" />");//show current image

  if (($i + 1) <= ($num_rows - 1) ) //if there is a record after this one
  {
    mssql_data_seek($rs,$i+1); 
    $next_record = mssql_fetch_object($rs); //get net record
    echo ("<a href=\"display.php?ID=" . $next_record->ID . "\">next</a>"); //show link to next image
   }
 break; //exit loop, since we found what we wanted
}
}
?>
 
thanks for the reply, this will actually be my first script WRITTEN in php entirely. im still ignorant on databases and how they work together. i ended up writing some code that searches the directory and lists the images in the given directory, then it provides a link to each image it found.

Code:
<?php
$root_dir = "pictures/residential/49k/martin/1/images/";
$image_name = 0;
if ($dir = @opendir("pictures/residential/49k/martin/1/images/")) {
	while (($file = readdir($dir)) !== false) {
		if ( preg_match("/(\.jpg)$/", $file) ) {
			$image_name++;
			echo "<a href=$root_dir$file target=iframe>$image_name</a>&nbsp; ";
		}
	}  
	closedir($dir);
}
?>
 
Working off of your code...

Code:
<?php

$root = "/pictures/residential/49k/l/images/";
$files = glob($root . "*.jpg");

foreach ($files as $file) {
   echo "<a href=$root$file target=iframe>$image_name</a>&nbsp; ";
}

?>

Edit: syntax error... oops ;)
 
Give it a shot... it works on my webserver (FreeBSD 5-STABLE, Apache 1.3.x, PHP 5.0.x). With a different directory, of course... :p
 
Back
Top