how to change PHP code
-
Hi everybody, I was implementing an image gallery from one of tutorials. In this tutorial PHP opens a directory and loops through it and outputs all image files. I am looking for code solution how to change this loop to output only those images that belongs to particular image album. There should be way to run SQL query and select files and then output them, problem is I have no idea how. When I upload image I store some data to following table:
CREATE TABLE tbl_image (
im_id INT NOT NULL AUTO_INCREMENT,
im_user_name VARCHAR(25) NOT NULL,
im_album_id INT NOT NULL,
im_title VARCHAR(64) NOT NULL,
im_description TEXT NOT NULL,
im_type VARCHAR(30) NOT NULL,
im_image VARCHAR(60) NOT NULL,
im_date DATETIME NOT NULL,
PRIMARY KEY(im_id)
);here is the code piece that should be changed:
<?php
/* Configuration Start */
$thumb_directory = 'img/thumbs';
$orig_directory = 'img/original';$stage_width=600; // How big is the area the images are scattered on
$stage_height=400;/* Configuration end */
$allowed_types=array('jpg','jpeg','gif','png');
$file_parts=array();
$ext='';
$title='';
$i=0;/* Opening the thumbnail directory and looping through all the thumbs: */
$dir_handle = @opendir($thumb_directory) or die("There is an error with your image directory!");
$i=1;
while ($file = readdir($dir_handle))
{
/* Skipping the system files: */
if($file=='.' || $file == '..') continue;$file\_parts = explode('.',$file); $ext = strtolower(array\_pop($file\_parts)); /\* Using the file name (withouth the extension) as a image title: \*/ $title = implode('.',$file\_parts); $title = htmlspecialchars($title); /\* If the file extension is allowed: \*/ if(in\_array($ext,$allowed\_types)) { /\* Generating random values for the position and rotation: \*/ $left=rand(0,$stage\_width); $top=rand(0,400); $rot = rand(-40,40); if($top>$stage\_height-130 && $left > $stage\_width-230) { /\* Prevent the images from hiding the drop box \*/ $top-=120+130; $left-=230; } /\* Outputting each image: \*/ echo ' <div id="pic-'.($i++).'" class="pic" style="top:'.$top.'px;left:'.$left.'px;background:url('.$thumb\_directory.'/'.$file.') no-repeat 50% 50%; -moz-transform:rotate('.$rot.'deg); -webkit-transform:rotate('.$rot.'deg);"> <a class="fancybox" rel="fncbx" href="'.$orig\_directory.'/'.$file.'" target="\_blank">'.$title.'</a> </div>'; }
}
/* Closing the directory */
closedir($dir_ha -
Hi everybody, I was implementing an image gallery from one of tutorials. In this tutorial PHP opens a directory and loops through it and outputs all image files. I am looking for code solution how to change this loop to output only those images that belongs to particular image album. There should be way to run SQL query and select files and then output them, problem is I have no idea how. When I upload image I store some data to following table:
CREATE TABLE tbl_image (
im_id INT NOT NULL AUTO_INCREMENT,
im_user_name VARCHAR(25) NOT NULL,
im_album_id INT NOT NULL,
im_title VARCHAR(64) NOT NULL,
im_description TEXT NOT NULL,
im_type VARCHAR(30) NOT NULL,
im_image VARCHAR(60) NOT NULL,
im_date DATETIME NOT NULL,
PRIMARY KEY(im_id)
);here is the code piece that should be changed:
<?php
/* Configuration Start */
$thumb_directory = 'img/thumbs';
$orig_directory = 'img/original';$stage_width=600; // How big is the area the images are scattered on
$stage_height=400;/* Configuration end */
$allowed_types=array('jpg','jpeg','gif','png');
$file_parts=array();
$ext='';
$title='';
$i=0;/* Opening the thumbnail directory and looping through all the thumbs: */
$dir_handle = @opendir($thumb_directory) or die("There is an error with your image directory!");
$i=1;
while ($file = readdir($dir_handle))
{
/* Skipping the system files: */
if($file=='.' || $file == '..') continue;$file\_parts = explode('.',$file); $ext = strtolower(array\_pop($file\_parts)); /\* Using the file name (withouth the extension) as a image title: \*/ $title = implode('.',$file\_parts); $title = htmlspecialchars($title); /\* If the file extension is allowed: \*/ if(in\_array($ext,$allowed\_types)) { /\* Generating random values for the position and rotation: \*/ $left=rand(0,$stage\_width); $top=rand(0,400); $rot = rand(-40,40); if($top>$stage\_height-130 && $left > $stage\_width-230) { /\* Prevent the images from hiding the drop box \*/ $top-=120+130; $left-=230; } /\* Outputting each image: \*/ echo ' <div id="pic-'.($i++).'" class="pic" style="top:'.$top.'px;left:'.$left.'px;background:url('.$thumb\_directory.'/'.$file.') no-repeat 50% 50%; -moz-transform:rotate('.$rot.'deg); -webkit-transform:rotate('.$rot.'deg);"> <a class="fancybox" rel="fncbx" href="'.$orig\_directory.'/'.$file.'" target="\_blank">'.$title.'</a> </div>'; }
}
/* Closing the directory */
closedir($dir_haYou need to read image names from database instead of scanning the directory, using a query like:
SELECT * FROM tbl_image WHERE im_album_id = XXX
Reading a basic PHP/MySQL Tutorial[^] will help.
Hesham A. Amin My blog twitter: @HeshamAmin
-
You need to read image names from database instead of scanning the directory, using a query like:
SELECT * FROM tbl_image WHERE im_album_id = XXX
Reading a basic PHP/MySQL Tutorial[^] will help.
Hesham A. Amin My blog twitter: @HeshamAmin
I do understand that bit, but how to output query results the same way as they are in while loop?
-
I do understand that bit, but how to output query results the same way as they are in while loop?
-
wartotojas wrote:
how to output query results the same way as they are in while loop?
Many of the most basic tutorials demonstrate this. This Tizag Tutorial[^] is a good example.
If at first you don't succeed, you're not Chuck Norris.
Problem solved, thanks for suggestions everybody :)