简体   繁体   English

如何获取下一个存储在php数据库中的图像

[英]How to get the next image stored in a database in php

I have initialized a variable called lets say a = 0; 我已经初始化了一个名为a = 0的变量; when the user clicks the button for next picture, a is incremented by one and there for i need to call the picture with the id of the value of 'a'. 当用户单击下一张图片的按钮时,a会增加一个,因此我需要使用ID为“ a”的图片来调用图片。

Would the best way to do this be to run a query every time the button is clicked, or rather store all the images in an array and call the 'a-th' value of the array on click? 最好的方法是在每次单击按钮时运行查询,或者将所有图像存储在数组中,并在单击时调用数组的“ a-th”值吗?

Please Help, Much Appreciated, Thanks! 请帮助,非常感谢,谢谢!

You can use regular SQL, for example in MySQL 您可以使用常规SQL,例如在MySQL中

SELECT * FROM images WHERE [here your where-clause] LIMIT 1 OFFSET $a;

As a sidenote: Don't save the whole image in the database, but only the path to it. 附带说明:不要将整个图像保存在数据库中,而只保存其路径。

There's cons to both situations. 两种情况都有弊端。

  1. if you load all images and save to array that 1st picture might take time to load and the user might no even be interested in going to the rest. 如果您加载所有图像并将其保存到数组,则第一张图像可能需要花费一些时间才能加载,并且用户甚至可能对其余的图像都不感兴趣。

  2. if you load as they click next then it might take a while for each image loads. 如果您在他们单击下一步时加载它们,则每个图像加载可能需要一段时间。

when uploading images 上传图片时

    $filename = "";

            if(!empty($_FILES["file"]))
            {
                if ((($_FILES["file"]["type"] == "image/gif")
                    || ($_FILES["file"]["type"] == "image/jpeg")
                    || ($_FILES["file"]["type"] == "image/pjpeg")))
                {
                    if ($_FILES["file"]["error"] > 0)
                        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
                    else
                    {
                        $filename = $_FILES["file"]["name"];
                        if (file_exists("../img/noticias/" . $_FILES["file"]["name"]))
                            echo "Image " .$_FILES["file"]["name"] . " exists.";
                        else
                        {
                            move_uploaded_file($_FILES["file"]["tmp_name"],
                            "your server path" .  $_FILES["file"]["name"]);

                        }
                    }
                }
            }

then save $filename to your table To view them in gallery 然后将$ filename保存到表中以在图库中查看它们

    while($row = $db->fetch_array($query)){  
        echo '<img src="path/to/images/' . $row['filename'] . '" width="219" height="117" />';

    }  

and using something like Lightbox you can use jquery to make a popup to view larger or add a hyperlink to each image so they can view in a seperate page the full size image. 并使用Lightbox之类的工具,可以使用jquery弹出一个窗口以查看更大的图像或向每个图像添加超链接,以便他们可以在单独的页面中查看完整尺寸的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM