单击创建下一张图像:上一张和下一张

发布于 2024-11-28 04:21:19 字数 200 浏览 2 评论 0原文

我有一个场景,如果我有一个缩略图网格,并且用户单击一个图像,他们可以单击下一个(以查看网格中的下一个图像)或上一个(反之亦然)。

我会在 mysql 中有一个单独的表来存储这些图像源的列表,还是会使用类似数组之类的东西,或者还有其他更好的选择吗?我认为这是错误的,如果有数百万张图像,它会减慢服务器速度吗?

您也愿意举个例子吗?

谢谢你!

I have a scenario where if I have a grid of thumbnail images and a user clicks an image, they can click on next (to see next image in line from the grid) or previous (vice versa).

Would I have a separate table in mysql for a list of those image source, or would you use something like an array, or are there any other better options? I'm thinking this in the long wrong, if there's millions of images, it will slow down the server?

Would you be willing to show an example too?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红焚 2024-12-05 04:21:19

我不确定为什么会出现问题......如果您显示网格,则意味着您已经可以访问上一张/下一张图像,对吧?根据您拥有的用户数量,您可以简单地将显示网格中的图像 ID 列表放入会话中,然后您甚至不需要访问数据库(除了拉取图像本身)。当用户滚动浏览图像时(使用上一张/下一张),如果用户开始接近结尾,只需将其添加到列表中即可。

I'm not sure why there is an issue ... if you're showing a grid, that means you already have access to the previous/next image, right? Depending on how many users you have, you could simply put a list of image ids from the displayed grid into session, then you wouldn't even need to hit the database (except to pull the image itself). As the user is scrolling through the images (using prev/next), just add to the list if the user starts getting close to the end.

醉梦枕江山 2024-12-05 04:21:19

是的,您需要桌子。
因为您不应该将所有文件存储在一个目录中 - 因为目录有文件数量的限制。因此,您不能只从文件夹中读取“下一个”文件,因为该文件可能位于另一个文件夹中。
欲了解更多信息,请查看此处:
PHP 安全性和独特的命名约定

Yes, you need the table.
Because you shouldn't store all files in one directory - because directories have limits of count of files. So you can't just read "next" file from folder, because that file can be in another folder.
For additional info, look here:
PHP security and unique naming conventions

我ぃ本無心為│何有愛 2024-12-05 04:21:19

我认为你不需要为此拥有一个多维数组。您的图像仍应具有从 1 到 n 的连续 ID,以便轻松实现 next() 和 prev() 函数。

例如,我从 MySQL 查询中得到了这个结果。

Array
(
[0] => Array
    (
        [title] => Title test
        [src] => /images/1.jpg
    )

[1] => Array
    (
        [title] => Title
        [src] => /images/12123.jpg
    )

[2] => Array
    (
        [title] => Image
        [src] => /images/32132.jpg
    )

[3] => Array
    (
        [title] => Image test
        [src] => /images/332.jpg
    )    
)

然后,您可以在视图中处理网格生成。

$width = 2; // max_width of your grid
echo "<table border=1>\n";
foreach ($main as $id => $img)
{
  if ($id % $width == 0)
    echo "<tr>\n"; // create a new row if the width has been reached
  echo "<td>".$img['src']."</td>";
}
echo '</table>';

然后你的 javascript 在图像之间切换应该更容易,因为你只需要增加和减少 ID,而不是在你有时计算它

[0][0] [0][1] [0][2]
[1][0] [1][1] [1][2]

I don't think you need to have a multi-dimensional array for this. Your images should still have a sequential ID from 1 to n so it will be easy to implement your next() and prev() function.

For example I have this result from my MySQL query.

Array
(
[0] => Array
    (
        [title] => Title test
        [src] => /images/1.jpg
    )

[1] => Array
    (
        [title] => Title
        [src] => /images/12123.jpg
    )

[2] => Array
    (
        [title] => Image
        [src] => /images/32132.jpg
    )

[3] => Array
    (
        [title] => Image test
        [src] => /images/332.jpg
    )    
)

Then you handle the grid generation in your view.

$width = 2; // max_width of your grid
echo "<table border=1>\n";
foreach ($main as $id => $img)
{
  if ($id % $width == 0)
    echo "<tr>\n"; // create a new row if the width has been reached
  echo "<td>".$img['src']."</td>";
}
echo '</table>';

Then your javascript to toggle between images should be much easier as you only need to increment and decrement the ID, instead of computing for it when you have

[0][0] [0][1] [0][2]
[1][0] [1][1] [1][2]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文