PHP & MYSQL - 根据照片上传日期显示数据库中的 user-uploaded 张照片
PHP & MYSQL - Display user-uploaded photos from Database according to date photos were uploaded
我创建了照片上传表单。不同的用户可以一次上传多张照片,照片、用户的详细信息和输入会存储在数据库中,如下图所示:
我希望能够根据照片上传的日期在我的 index.php 页面中显示用户及其照片。我希望 index.php 页面看起来像这样
我尝试在我的 SQL 语句中使用 GROUP_CONCAT() 和 GROUP BY,希望实现类似于下面的 table 的东西,这可能允许我循环遍历 table 数据,以便与用户一起显示照片
我的代码无效:
$sql = "SELECT id, unique_id, GROUP_CONCAT(fileName) as fileName, title,date GROUP BY unique_id FROM images";
$result = $conn->query($sql);
if ($result->num_rows > 0) { // This is the line 22 in my code.
while($row = $result->fetch_assoc()) {
echo $row['fileName']; //I wanted to know if echo $row['fileName']; would print something like cow1.jpg, cow2,jpg etc.
}
}
但是我得到如下错误:
Notice: Trying to get property of non-object in C:\xampp\htdocs\display.php on line 22
有人向我推荐了 GROUP_CONCAT()
。我进行了一些搜索,但所有教程都给出了 table 使用 GROUP_CONCAT()
后如何处理的示例,但没有说明如何从最终 table 中获取数据以在 index.php
中使用.我是 GROUP_CONCAT()
的新手,我真的不知道如何使用它。
试试这个:
$sql = "SELECT id, unique_id, GROUP_CONCAT(fileName) AS filenames, title, date FROM images GROUP BY unique_id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row['filenames'];
}
}
删除之前的问题,在评论中对你进行了一番斥责,现在让我们尝试一下"teaching" ;-)
正如我在对先前问题的评论中所说,这是我不会尝试和喜欢 GROUP_CONCAT 的地方。最重要的是因为它的缺点是 a) 它是 MySQL 特定的,所以使用它有点阻碍代码的可移植性,b) 更严重的是,你可以 assemble 得到的字符串的长度是有限的.文件名(加上路径)很容易变长,如果您为不同的用户设置不同的上传目录或在 URL 中包含其他参数,那么这将限制一个组可以包含的照片数量,在此之前 "explodes"。 ..
我提到的control break是一个非常基本的编程原则。您遍历一组已排序(!)的数据,并比较一个或多个条件以查看它们是否在前一行和当前行之间发生了变化。
您的示例需要一个简单的一级控制中断 - 日期是此处唯一的分组标准。但同样的原则可以扩展到多个级别——比如说,如果你有一个涵盖几年的信用卡购买清单,你想先按年分组输出它们,然后再按月分组。
以下示例循环遍历与您的示例数据大致匹配的数组;但是遍历数据库查询结果这几乎是一样的。
但请记住,在任何情况下,输入数据必须已经按中断标准(或标准,用于多级控制中断;在这种情况下,也 "ordered in the right order",意思是上面的例子,你的 CC 购买先按年,然后按月。)
从这里开始,带有注释的示例应该是不言自明的,所以:
<?php
// example data - I left out a few of your columns; the important part is that
// it is sorted by the control group break criterion, in this case the date
$data = [
['id' => '1', 'name' => 'John', 'file' => 'cow1.jpg', 'date' => '07-12-2017 01:05'],
['id' => '2', 'name' => 'John', 'file' => 'cow2.jpg', 'date' => '07-12-2017 01:05'],
['id' => '3', 'name' => 'Dan', 'file' => 'cat1.jpg', 'date' => '08-12-2107 02:09'],
['id' => '4', 'name' => 'Dan', 'file' => 'cat2.jpg', 'date' => '08-12-2107 02:09'],
['id' => '5', 'name' => 'John', 'file' => 'dog.jpg', 'date' => '09-12-2107 03:10'],
];
echo '<ul>'; // open unordered list
echo '<li>'; // opening list item tag for first grouped set of data
$previousDate = null;
foreach($data as $row) {
// check if group break has occurred - is the date of this row different
// from that of the previous one? (since we initialized $previousDate as null,
// this will be true for the first row in any case)
if($row['date'] != $previousDate) {
// break has occured
if(null != $previousDate) {
// if this is not the first row, then we have created output before, so
// now we need to close the previous list item, and open an new one.
echo '</li>';
echo '<li>';
}
// now we output whatever is needed at the start of a new group - in this case,
// user name and date, since we want those only once
echo 'User: ' . $row['name'] . '<br>';
echo 'Date: ' . $row['date'] . '<br>';
}
// now, handle the output of the actual "per-row" data - in this case, the individual image
echo '[img src="' . $row['file'] . '"]<br>'; // pseudo image tag in lack of real image files
// remember current row's date as the new previous one, so that we can compare against
// that in the next loop iteration
$previousDate = $row['date'];
}
echo '</li>'; // closing list item tag for last grouped set of data
echo '</ul>'; // close unordered list
如果你想要不同于无序列表的东西(我知道这里没有使用 有序 列表有点讽刺,但我不想破坏示例输出导致的默认项目编号,这可能会造成混淆),您当然也可以在其他结构中输出数据 - 例如,table 而不是列表。只需要稍微考虑一下要输出什么, where/when 就在每种情况下。假设您想要一个包含三列名称、日期和图像的 table,并在每一行中将名称和日期留空,但它们仍然与前一列匹配 - 那么您仍然需要输出 empty table 单元格的名称和日期,以免弄乱所需的基本 HTML table 结构。因此逻辑可能需要稍微调整,具体取决于所需的特定输出。
我创建了照片上传表单。不同的用户可以一次上传多张照片,照片、用户的详细信息和输入会存储在数据库中,如下图所示:
我希望能够根据照片上传的日期在我的 index.php 页面中显示用户及其照片。我希望 index.php 页面看起来像这样
我尝试在我的 SQL 语句中使用 GROUP_CONCAT() 和 GROUP BY,希望实现类似于下面的 table 的东西,这可能允许我循环遍历 table 数据,以便与用户一起显示照片
我的代码无效:
$sql = "SELECT id, unique_id, GROUP_CONCAT(fileName) as fileName, title,date GROUP BY unique_id FROM images";
$result = $conn->query($sql);
if ($result->num_rows > 0) { // This is the line 22 in my code.
while($row = $result->fetch_assoc()) {
echo $row['fileName']; //I wanted to know if echo $row['fileName']; would print something like cow1.jpg, cow2,jpg etc.
}
}
但是我得到如下错误:
Notice: Trying to get property of non-object in C:\xampp\htdocs\display.php on line 22
有人向我推荐了 GROUP_CONCAT()
。我进行了一些搜索,但所有教程都给出了 table 使用 GROUP_CONCAT()
后如何处理的示例,但没有说明如何从最终 table 中获取数据以在 index.php
中使用.我是 GROUP_CONCAT()
的新手,我真的不知道如何使用它。
试试这个:
$sql = "SELECT id, unique_id, GROUP_CONCAT(fileName) AS filenames, title, date FROM images GROUP BY unique_id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row['filenames'];
}
}
删除之前的问题,在评论中对你进行了一番斥责,现在让我们尝试一下"teaching" ;-)
正如我在对先前问题的评论中所说,这是我不会尝试和喜欢 GROUP_CONCAT 的地方。最重要的是因为它的缺点是 a) 它是 MySQL 特定的,所以使用它有点阻碍代码的可移植性,b) 更严重的是,你可以 assemble 得到的字符串的长度是有限的.文件名(加上路径)很容易变长,如果您为不同的用户设置不同的上传目录或在 URL 中包含其他参数,那么这将限制一个组可以包含的照片数量,在此之前 "explodes"。 ..
我提到的control break是一个非常基本的编程原则。您遍历一组已排序(!)的数据,并比较一个或多个条件以查看它们是否在前一行和当前行之间发生了变化。
您的示例需要一个简单的一级控制中断 - 日期是此处唯一的分组标准。但同样的原则可以扩展到多个级别——比如说,如果你有一个涵盖几年的信用卡购买清单,你想先按年分组输出它们,然后再按月分组。
以下示例循环遍历与您的示例数据大致匹配的数组;但是遍历数据库查询结果这几乎是一样的。
但请记住,在任何情况下,输入数据必须已经按中断标准(或标准,用于多级控制中断;在这种情况下,也 "ordered in the right order",意思是上面的例子,你的 CC 购买先按年,然后按月。)
从这里开始,带有注释的示例应该是不言自明的,所以:
<?php
// example data - I left out a few of your columns; the important part is that
// it is sorted by the control group break criterion, in this case the date
$data = [
['id' => '1', 'name' => 'John', 'file' => 'cow1.jpg', 'date' => '07-12-2017 01:05'],
['id' => '2', 'name' => 'John', 'file' => 'cow2.jpg', 'date' => '07-12-2017 01:05'],
['id' => '3', 'name' => 'Dan', 'file' => 'cat1.jpg', 'date' => '08-12-2107 02:09'],
['id' => '4', 'name' => 'Dan', 'file' => 'cat2.jpg', 'date' => '08-12-2107 02:09'],
['id' => '5', 'name' => 'John', 'file' => 'dog.jpg', 'date' => '09-12-2107 03:10'],
];
echo '<ul>'; // open unordered list
echo '<li>'; // opening list item tag for first grouped set of data
$previousDate = null;
foreach($data as $row) {
// check if group break has occurred - is the date of this row different
// from that of the previous one? (since we initialized $previousDate as null,
// this will be true for the first row in any case)
if($row['date'] != $previousDate) {
// break has occured
if(null != $previousDate) {
// if this is not the first row, then we have created output before, so
// now we need to close the previous list item, and open an new one.
echo '</li>';
echo '<li>';
}
// now we output whatever is needed at the start of a new group - in this case,
// user name and date, since we want those only once
echo 'User: ' . $row['name'] . '<br>';
echo 'Date: ' . $row['date'] . '<br>';
}
// now, handle the output of the actual "per-row" data - in this case, the individual image
echo '[img src="' . $row['file'] . '"]<br>'; // pseudo image tag in lack of real image files
// remember current row's date as the new previous one, so that we can compare against
// that in the next loop iteration
$previousDate = $row['date'];
}
echo '</li>'; // closing list item tag for last grouped set of data
echo '</ul>'; // close unordered list
如果你想要不同于无序列表的东西(我知道这里没有使用 有序 列表有点讽刺,但我不想破坏示例输出导致的默认项目编号,这可能会造成混淆),您当然也可以在其他结构中输出数据 - 例如,table 而不是列表。只需要稍微考虑一下要输出什么, where/when 就在每种情况下。假设您想要一个包含三列名称、日期和图像的 table,并在每一行中将名称和日期留空,但它们仍然与前一列匹配 - 那么您仍然需要输出 empty table 单元格的名称和日期,以免弄乱所需的基本 HTML table 结构。因此逻辑可能需要稍微调整,具体取决于所需的特定输出。