doushan6161 2019-03-04 16:28
浏览 26
已采纳

限制显示图像从数据库中获取并显示最新图像

I am trying to limit display 3 images to a row. Only a row, however, I really don't get it how to limit the number of images to display and how to display newest image to first, it's only display newest to the last row. I am trying some way, but nothing work.

Upload

<?php
if (isset($_POST['submit'])){
    $target_dir = dirname(dirname(__FILE__)).'/store/';
    $target = $target_dir . basename($_FILES['image']['name']);

    $db =  mysqli_connect("localhost", "root", "", "image");
    $image = $_FILES['image']['name'];
    $title = $_POST['image_title'];
    $description = $_POST['description'];

    $sql = "INSERT INTO images (image, title, description) VALUE ('$image', '$title', '$description')";
    mysqli_query($db, $sql);

    if(move_uploaded_file($_FILES['image']['tmp_name'], $target))
    {
        // header("location:../../gallery/gallery.html");
        header("location: ../test.php");
    }
}
?>

Display

<?php
$db = mysqli_connect("localhost", "root", "", "image");
$sql = "SELECT * FROM images";
$result = mysqli_query($db, $sql);
$count = 0;

while($row = mysqli_fetch_array($result)){
    $listImage = $row['image'];
    echo "<img src='store/".$listImage."' width='250' height='250'>";
}
  • 写回答

1条回答 默认 最新

  • dop2144 2019-03-04 16:56
    关注

    Step 1:

    You may need to update your table with an auto_increment column or a DATETIME column that should save the current time in the table.

    Let's consider adding an auto_increment id field in images table. You need to run this command to do so:

    ALTER TABLE `images` 
      ADD `id` INT NOT NULL auto_increment first, 
      ADD PRIMARY KEY (`id`);
    

    Step 2:

    Change your SQL query to:

    // to get the latest image stored in the images table.
    $sql = "SELECT * FROM images ORDER BY id DESC LIMIT 1"; 
    

    It will do the job for you! Also, I have added LIMIT 1 - you can change it whatever count of latest images you want

    Note: If you want to get the most recent images of the last month, or you modify an image (update the date), in such cases, please consider adding a DATETIME column in the table and use that in order by clause.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100