Mysql Database-
id | username | folder_name |
1 | ashly | Folder |
2 | ashly | |
3 | ashly | |
4 | ashly | Folder |
5 | ashly | Folder |
6 | ashly | Folder 1 |
7 | ashly | Folder 2 |
$sql = mysql_query("SELECT `id`, `username`, `title`, LEFT(`description`, 90) as `description`, `file`, `code`, `folder_name`, `type`, `size`, `date` FROM `files` WHERE `username` = '$userfile' AND `folder_name` > '' ORDER BY `id` DESC LIMIT 1, 5");
while ($query_row = mysql_fetch_array($sql2)) {
$filefolder = $query_row['folder_name'];
$filetitle = $query_row['title'];
$filecode = $query_row['code'];
$filedesc = $query_row['description'];
$filefile = $query_row['file'];
$filesize = $query_row['size'];
$filedate = $query_row['date'];
$filetype = $query_row['type'];
//在这里我循环寻找结果
if (empty($filefolder) === false) {
echo "" . $filefolder . "<div id='imageshowsearch'><div id='linkstyle'><strong><a href='http://localhost/edu/1111111111111/userdownload.php?code=". $filecode . " '><img src='files/thumbs/" . $filecode . "/" . $filefile . "' alt=" . $filetitle . ">" . $filetitle . "</strong></div></a></div>";
//here print result
}
}
我只能显示一次
folder_name
。文件夹中有5张图片。当我循环它的mysql结果。 Result:
folder | folder | folder | folder 1 | folder 2
here is image | here is image | here is image | here is image | here is image
I wonna get this result:
folder |
here is image | here is image | here is image |
folder 1 |
here is image |
folder 2 |
here is image |
我不知道如何仅显示一次文件夹名称,并且在文件夹名称下如何显示某个文件夹中的最多5张图像。只是我不愿意在每个图像上都重复文件夹名称。我必须将文件夹名称和文件夹图像与用户图像分组。我的问题仅在于重复这些文件夹名称...感谢您的支持,我正在写,因为网站不允许我发布问题“更多代码不解释”
最佳答案
您可以使用以下方式。希望它对您有用。
$sql = mysql_query("SELECT DISTINCT `folder_name` FROM `files` WHERE `username` = '$userfile' AND `folder_name` > ''");
while ($row = mysql_fetch_array($sql)) {
$folder== $row['folder_name'];
$sql2 = mysql_query("SELECT `id`, `username`, `title`, LEFT(`description`, 90) as `description`, `file`, `code`, `type`, `size`, `date` FROM `files` WHERE folder_name`='$folder' ORDER BY `id` DESC LIMIT 1, 5");
//Here you can echo $folder i.e, folder name and then all its image
while ($query_row = mysql_fetch_array($sql2)) {
$filetitle = $query_row['title'];
$filecode = $query_row['code'];
$filedesc = $query_row['description'];
$filefile = $query_row['file'];
$filesize = $query_row['size'];
$filedate = $query_row['date'];
$filetype = $query_row['type'];
}
}
祝好运。
关于php - PHP的MySQL选择只匹配列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14909090/