0

实际上我正在开发一个 php 中的应用程序。我很擅长 ASP.NET,但不擅长 PHP,所以在这里需要一些帮助。

我的应用程序是一个图片库,我在这里所做的是,我将图像存储在运行时创建的特定目录中,使用多上传插件,在创建新专辑时,将提示用户只输入专辑的名称,然后他选择要存储在该图库中的所有图像。存储在数据库中的数据将是画廊的名称和已上传图像的文件夹的相对路径等,而专辑封面图像和其他图像详细信息不会添加到数据库中。

另一方面,我正在以这样一种方式查找输出,即专辑列表将从数据库中显示,并且每个图像文件夹中的一张图像将随机显示为专辑封面。当用户单击该专辑封面时,该专辑在数据库中的路径将作为查询字符串传递,然后他将被定向到显示该相关专辑的所有图像的页面。在这里,服务器将扫描从查询字符串中获取的路径的所有图像,并显示该特定文件夹中的所有图像。

我的代码:

$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.

}

我已经成功地完成了所有的上传和数据库添加操作,并且还在网页上显示了特定文件夹的所有图像。但是我的问题是在显示所有当前可用的专辑时将随机图像显示为专辑封面。从数据库中获取数据,服务器将扫描“路径”并从该路径中获取一张图像并显示为专辑封面。

4

3 回答 3

1

php.net 文档始终是简短代码片段的良好来源:

HeadRoom 于 2009 年 5 月 8 日 12:08 发布了此代码

<?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];
?>

请注意,您可以通过向内部添加条件来轻松过滤图片if,例如&& strripos($file, ".jpg", 0) == strlen($file) - strlen(".jpg")仅获取.jpg文件(不区分大小写,感谢strripos)。

于 2012-08-08T09:57:09.187 回答
1

因此与这个问题

function random_pic($dir = 'uploads') 
{ 
    $files = glob($dir . '/*.*'); 
    $file = array_rand($files); 
    return $files[$file]; 
}
于 2012-08-08T09:52:47.093 回答
0

解决了!!!谢谢@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];
}
?>
于 2012-08-11T11:06:37.517 回答