Mysql 如何从数据库获取的图像数组中一次显示一个图像?

Mysql 如何从数据库获取的图像数组中一次显示一个图像?,mysql,node.js,ejs,Mysql,Node.js,Ejs,在这个应用程序中,我将图像上传到本地上传文件夹,并在MySQL数据库中存储图像的名称。如何从数据库获取的图像数组中一次显示一个图像?目前,我使用EJS显示了所有上传的图像。如何设置单个显示图像“src”每10秒左右更改一次 <% for(i=0; i<images.length; i++) { %> <img src="/uploads/<%= images[i].name %>" alt=""> <% }; %> 我对EJS不太了

在这个应用程序中,我将图像上传到本地上传文件夹,并在
MySQL数据库中存储图像的名称。如何从
数据库
获取的图像数组中一次显示一个图像?目前,我使用
EJS
显示了所有上传的图像。如何设置单个显示图像“src”每10秒左右更改一次

<% for(i=0; i<images.length; i++) { %>
    <img src="/uploads/<%= images[i].name %>" alt="">
<% }; %>

我对EJS不太了解,所以如果有语法错误,请在您这边更改它。首先为所有图像和相同的类名创建一个唯一的id

<% for(i=0; i<images.length; i++) { %>
    <% if(i == 0) { %>
        <img class="allImage" src="/uploads/<%= images[i].name %>" alt="" id="image_<%= i %>">
    <% }; %>
    <% if(i != 0) { %>
        <img class="allImage" src="/uploads/<%= images[i].name %>" alt="" image_id="test<%= i %>" style="display:none;">
    <% }; %>
<% }; %>
创建一个函数,每10秒调用一次

window.setInterval(function(){
    myFuction();
}, 10000);
检查图像的长度,以便在上次显示图像时,我们可以再次从0开始

myFuction(){
    a = a + 1; // increment global variable to show next image
    var length = $('.allImage').length;
    if(length == a || length > a){
        a = 0;
    }
    $('.allImage').hide(); // First hide all image with same class
    $('#image_'+a).show(); //  Show next image
}

希望这将帮助您获得您的输出。这对我的php工作,所以我希望这也将为你工作。如果有任何语法错误,请在末尾进行更改。谢谢

我对EJS不太了解,所以如果有任何语法错误,请在您这边更改它。首先为所有图像和相同的类名创建一个唯一的id

<% for(i=0; i<images.length; i++) { %>
    <% if(i == 0) { %>
        <img class="allImage" src="/uploads/<%= images[i].name %>" alt="" id="image_<%= i %>">
    <% }; %>
    <% if(i != 0) { %>
        <img class="allImage" src="/uploads/<%= images[i].name %>" alt="" image_id="test<%= i %>" style="display:none;">
    <% }; %>
<% }; %>
创建一个函数,每10秒调用一次

window.setInterval(function(){
    myFuction();
}, 10000);
检查图像的长度,以便在上次显示图像时,我们可以再次从0开始

myFuction(){
    a = a + 1; // increment global variable to show next image
    var length = $('.allImage').length;
    if(length == a || length > a){
        a = 0;
    }
    $('.allImage').hide(); // First hide all image with same class
    $('#image_'+a).show(); //  Show next image
}

希望这将帮助您获得您的输出。这对我的php工作,所以我希望这也将为你工作。如果有任何语法错误,请在末尾进行更改。谢谢

我以前没有用过EJS,但我想会用一些东西,因为你在一段艰难的时间里想要更改图像。 下面是一个例子: 如果您想让它在没有随机性的情况下循环,那么您只需要保留一个索引即可。 我继续,并添加了一个链接到没有随机性的版本。由于EJS只是JavaScript,我认为这应该很容易转换,以满足您的需要。如果你需要更多的帮助,请告诉我

并且,为了显示代码而无需前往其他站点:

// Selects the DOM node with the unique id, 'switch'.
const img = document.querySelector('#switch');

const images = [
    "https://picsum.photos/350",
    "https://picsum.photos/300?blur",
    "https://picsum.photos/300?grayscale&blur=2"
];

function changeImage() {
    // This'll give us a floating point number that's between 0 and the length of the images.
    const rand = Math.random() * images.length;
    // This'll give us an integer between 0 and the last index of our images.
    const index = Math.floor(rand);

    img.src = images[index];

    // Notice that the function calls itself with our desired time, in this case 5000 milliseconds, or 5 seconds.
    setTimeout(changeImage, 5000);
}

// We have to call it for the first change!
setTimeout(changeImage, 5000);

我以前没有使用过EJS,但我想应该使用一些东西,因为您在一段艰难的时间里希望更改图像。 下面是一个例子: 如果您想让它在没有随机性的情况下循环,那么您只需要保留一个索引即可。 我继续,并添加了一个链接到没有随机性的版本。由于EJS只是JavaScript,我认为这应该很容易转换,以满足您的需要。如果你需要更多的帮助,请告诉我

并且,为了显示代码而无需前往其他站点:

// Selects the DOM node with the unique id, 'switch'.
const img = document.querySelector('#switch');

const images = [
    "https://picsum.photos/350",
    "https://picsum.photos/300?blur",
    "https://picsum.photos/300?grayscale&blur=2"
];

function changeImage() {
    // This'll give us a floating point number that's between 0 and the length of the images.
    const rand = Math.random() * images.length;
    // This'll give us an integer between 0 and the last index of our images.
    const index = Math.floor(rand);

    img.src = images[index];

    // Notice that the function calls itself with our desired time, in this case 5000 milliseconds, or 5 seconds.
    setTimeout(changeImage, 5000);
}

// We have to call it for the first change!
setTimeout(changeImage, 5000);

是否只显示一个图像?哪一个是第一个还是最后一个?@Yogendra我当时只想显示一个图像,但我想循环所有图像。我不知道EJS,但我试图帮助你。你想只显示一个图像吗?哪一个是第一个还是最后一个?@Yogendra我当时只想显示一个图像,但我想循环所有图像。我不知道EJS,但我现在试图帮助您看到另一个答案,setInterval可能更好,这样您就不必在函数中调用setTimeout(尽管这是在模拟)。不管怎样,我认为这种方法非常简单。现在看到另一个答案,setInterval可能更好,这样您就不必在函数中调用setTimeout(尽管这是在模拟)。不管怎样,我认为这种方式很简单。