Displaying images in multiple html or php files

images php coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
kyle04
New php-forum User
New php-forum User
Posts: 94
Joined: Sat Jul 07, 2012 1:36 pm

Tue Jul 17, 2012 1:42 pm

You'll have to reference the images from somewhere - a database table would be the most straightforward.

A table with (prikey) and 2 fields would be a starting point.

Field 1 : "pagename" - set to varchar 255
Field 2 : "image" - set to varchar 255

The image field would have just the image name (eg myimage.jpg), and could be referenced from the folder (in the example "images"), it resides in with php :

<?
error_reporting(E_ALL);

$this_page = $_SERVER['REQUEST_URI']; // returns page path/name from root
$result = mysql_query("SELECT*FROM TABLENAME WHERE pagename='$this_page'");
$row = mysql_fetch_array($result);
if($result) {
$display_image = "images/".$row['image']; // assuming image folder is in root directory

echo "<img src=\"$display_image\">";
}
?>

The script would have to be built into every page either directly, or saved in the same folder and referenced as an include :

<? include("image_script.php"); ?>

Your page naming and file paths will have to be spot on.

AndyP

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest