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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助