duancan8382 2012-08-08 09:49
浏览 29
已采纳

PHP和MYSQL。 想要显示目录的第一个图像。 数据库中的目录路径

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() . "
";
    $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.

  • 写回答

3条回答 默认 最新

  • doumu5023 2012-08-11 11:06
    关注

    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() . "
    ";
    $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];
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换