0

Actually I am working on an application that is in php. And I'm good with ASP.NET but not with PHP so require some help out here.

My application is an image gallery, and what I am doing here is, I am storing images in particular directories created at runtime, with a multi upload plugin and while creating a new album, the user will be prompted for just the name of the album, and then he selects all the images to be stored in that gallery. The data stored in the database will be the name of the gallery and the the relative path of the folder in which the images have been uploaded etc, while album art image and other images details are not added to the database.

On the other side I am looking for the output in such a way that the listing of the albums will be displayed from the database and one of the image from every image folder will be displayed as album art at random. When user clicks on that album art, the path of that album that was in the database will be passed as querystring and then he will be directed to the page displaying all the images for that relevant album. Here the server will scan all the images of the path taken from the query string and display all the images from that particular folder.

My codes:

$sql="Select ID, GalleryName, Path, CreateDate from imageGallery where IsActive=true";
$result=mysql_query($sql);

if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}


    while($albums_row = mysql_fetch_assoc($result)){
    $albums[] = array(
        'id'            => $albums_row['ID'],
        'name'          => $albums_row['GalleryName'],
        'path'          => $albums_row['Path'],
        'dates'         => $albums_row['CreateDate']
    );
}

foreach($albums as $album){
// please check if this thing is correct?
echo '<a href="showImage.php?galPath=' . $album["path"] . '"><img src="something"/>    </a>';
// or any function that can work in this loop to call random image for each folder.

}

I have been successful in doing all the uploading and database adding things, and also displaying all the images of a particular folder on a web page. But my problem is to display a random image as an album art when I am displaying all the currently available albums. The data is fetched from the database, and server will scan for the "path" and get one image out of that path and display as album art.

Cyberpks
  • 1,401
  • 6
  • 21
  • 51
  • pick one from db at random `SELECT * FROM tbl ORDER BY RAND() LIMIT 1` or pick one from 'result set' `$img = $albums[rand(0, count($albums)]` – T I Aug 08 '12 at 09:55
  • @MrAnderson, I think the list of files is not stored inside his database and he needs `readdir()` or equivalent to retrieve the list of files. It it were in the database, though, my answer on [selecting a random row efficiently](http://stackoverflow.com/a/4740561/324969) would interest him in case he has large albums. – Suzanne Soy Aug 08 '12 at 10:04
  • _Georges Dupéron_, is correct. I want to retrieve a single image out of the list of files read from the path, that is stored in the database. I am not storing any information of any specific image at all in the database. I am storing the relative path of the folder for every album, that will allow me to list the files present in that folder, and then retrieve any one to display as album art. – Cyberpks Aug 08 '12 at 10:56

3 Answers3

1

Accordingly with this question

function random_pic($dir = 'uploads') 
{ 
    $files = glob($dir . '/*.*'); 
    $file = array_rand($files); 
    return $files[$file]; 
}
Community
  • 1
  • 1
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
  • Can you please explain how do I print image from this function. will simply echo random_pic(album['path']) work. – Cyberpks Aug 08 '12 at 10:09
  • Yes, if you echo it you should get the name of the file – Mihai Matei Aug 08 '12 at 10:18
  • It seems that this thing is not working for me. You could see for the updated code above. The foreach loop may work here, how do I implement this method here?. – Cyberpks Aug 08 '12 at 10:36
  • Did you changed the `$dir` variable? this should be the path of the album, relative to the path of your php script, so if your script is at `/scripts/php/script.php` and your album folder is at `/upload/album/` then `$dir` variable should be `../../upload/album` – Mihai Matei Aug 08 '12 at 10:41
  • Yes, I have already done this. I am passing the path where the function is being called. The function will pass the value of `album['path']` from the `foreach` loop, every time a new record is read. This value can be something like "/savefiles/20120808_05_52_53/" – Cyberpks Aug 08 '12 at 10:44
1

THe php.net documentation is always a good source of short code snippets:

HeadRoom on 08-May-2009 12:08 posted this code:

<?php
$image_dir = 'images';
$count = 0;
if ($handle = opendir($image_dir)) {
    $retval = array();
    while (false !== ($file = readdir($handle))) {
        if (($file <> ".") && ($file <> "..")) {
            $retval[$count] = $file;
            $count = $count + 1;
        }
    }

    closedir($handle);
}
shuffle($retval);
$current_image = $retval[0];
?>

Note that you can easily filter just pictures by adding a condition to the inner if, for example && strripos($file, ".jpg", 0) == strlen($file) - strlen(".jpg") to get only .jpg files (case-insensitive thanks to strripos).

Suzanne Soy
  • 3,027
  • 6
  • 38
  • 56
0

Solved!!! Thanks @Matei Mihai.....

<?php
include("dbpath.php");

$sql="Select ID, GalleryName, Path, CreateDate from imageGallery where IsActive=true";
$result=mysql_query($sql);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message  = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}

while($albums_row = mysql_fetch_assoc($result)){
    $albums[] = array(
        'id'            => $albums_row['ID'],
        'name'          => $albums_row['GalleryName'],
        'path'          => $albums_row['Path'],
        'dates'         => $albums_row['CreateDate']
    );
}
$i=0;

foreach($albums as $album){
//echo $album['path'] . "<br/>";
$imgs =  str_replace(" ", "%20" , random_pic($album['path']));
$aPath= $album['path'];
echo "<a class='imgBox' href=" . "imageListMethod.php?p=" . $aPath .  "><img src=" . $imgs .   "></a>";
}
function random_pic($dir)
{
$files = glob($dir . '/*.jpg*');
$file = array_rand($files);
return $files[$file];
}
?>
Cyberpks
  • 1,401
  • 6
  • 21
  • 51