php mysql选择仅匹配列

时间:2013-02-16 10:04:46

标签: php mysql database

          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个图像。我不想在每张图片上重复文件夹名称。我必须使用用户图像将文件夹名称与文件夹图像分组。我的问题只是对于这些文件夹名称重复...感谢您的支持我写作因为网站不允许我发布我的问题'更多代码没有解释'

1 个答案:

答案 0 :(得分:2)

您可以使用以下方式。希望它能为你效劳。

$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'];
}

}
祝你好运。