PHP/MYSQL : How to create image galleries that rotate/change during each page refresh/load

标签 php mysql database web

我正在构建一个网站,该网站在单个页面上仅包含图像。此页有 300 张图片。主页。这些图像中的每一张都是一个链接,可以单击。我有一个数据库,该数据库包含一个表,其中每个图像都存在图像 URL 和链接/目标 URL。数据库中有 2,000 个图像,但页面上只有 300 个图像位置。

使用 PHP/MySQL 如何才能在每次加载/刷新页面时显示新图像。这样它就会随机地(但是每个图像相对于每次刷新都需要不同。页面上没有重复项)从数据库中抓取 300 个图像以显示在页面上。有点像旋转广告会起作用。每次加载页面时都会显示一个新广告。

每次刷新页面时,它不必显示 300 个不同的图像,它只是不能在页面上的任何时间显示重复的图像。

寻找我可以遵循的快速解决方案、想法或教程链接。谢谢!

编辑:

这是我目前拥有的代码..

<?php
    // Connects to the database.
    $connection = mysql_connect("localhost", "root", "");
        if (!$connection) {
            die("Connection Failed: " . mysql_error());
        }

    // Selects a database from the previously opened connection.
    $select = mysql_select_db("affiliate_links", $connection);
        if (!$select) {
            die("Connection Failed: " . mysql_error());
        }

    // Performs query to table 'affiliates' in previously opened database.
    $result = mysql_query("SELECT * FROM affiliates ORDER BY RAND() limit 1", $connection);
        if (!$result) {
            die("MySQL Query/Connection Failed: " . mysql_error());
        }

    while ($row = mysql_fetch_array($result)) {
    }
?>

** 以下代码是我希望每个新图像出现在页面上的位置。

        <a href="<?php echo $row['target_url'] ?>"><img src="<?php echo $row['thumb_url'] ?>"></a>
        <a href="<?php echo $row['target_url'] ?>"><img src="<?php echo $row['thumb_url'] ?>"></a>
        <a href="<?php echo $row['target_url'] ?>"><img src="<?php echo $row['thumb_url'] ?>"></a>
        <a href="<?php echo $row['target_url'] ?>"><img src="<?php echo $row['thumb_url'] ?>"></a>
        <a href="<?php echo $row['target_url'] ?>"><img src="<?php echo $row['thumb_url'] ?>"></a>
        <a href="<?php echo $row['target_url'] ?>"><img src="<?php echo $row['thumb_url'] ?>"></a>

照原样,上面的代码(echo $row 语句)不起作用。我能够从数据库中提取数据的唯一方法是将 echo 语句放在“while 循环”下面,如下所示。

            while ($row = mysql_fetch_array($result)) {
                echo $row['target_url']." ".$row['thumb_url']."<br />";

如果我在代码中保留 ORDER BY RAND() limit 1 ,它只会显示 1 次迭代。表的 1 行。如果我删除它,我会看到所有链接/图像链接,无论我在 while 循环下回显它。

所以基本上我现在需要的是:能够在 while 循环之外回显 $row 并将其显示在 html 内以完成链接。我需要它能够在循环的每次迭代期间显示新图像。或者是否有比 while 循环更好的方法。谢谢!

最佳答案

如何将数据库中的所有结果存储在一个可以在任何地方使用的数组中。我还更改了 SQL 查询,将结果限制为 300,并仅选择所需的列。

<?php
    // Connects to the database.
    $connection = mysql_connect("localhost", "root", "");
        if (!$connection) {
            die("Connection Failed: " . mysql_error());
        }

    // Selects a database from the previously opened connection.
    $select = mysql_select_db("affiliate_links", $connection);
        if (!$select) {
            die("Connection Failed: " . mysql_error());
        }

    // Performs query to table 'affiliates' in previously opened database.
    $result = mysql_query("SELECT target_url, thumb_url FROM affiliates ORDER BY RAND() limit 300", $connection);
        if (!$result) {
            die("MySQL Query/Connection Failed: " . mysql_error());
        }

    $images_array = array();

    while ($row = mysql_fetch_array($result)) {
        $target_url = $row['target_url'];
        $thumb_url = $row['thumb_url '];
        // you might consider changing the next line to include a class on the images for formatting purposes
        $image_link = '<a href = "' . $target_url . '"><img src = "' . $thumb_url . '"></a>';
        $images_array[] = $image_link;
    }
    // use this wherever you want to display your images
    foreach ($images_array as $current){
        echo $current . '<br>';
    }
?>

编辑 - 要在中间添加横幅文本,请将循环更改为

$counter = 0;
while ($counter < 150){
    echo $images_array[$counter];
    $counter++;
}

// put banner text here

while ($counter < 300){
    echo $images_array[$counter];
    $counter++;
}

关于PHP/MYSQL : How to create image galleries that rotate/change during each page refresh/load,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19553750/

相关文章:

php - 使用 MySQL Datetime 比较 PHP 中的时间

php - phing 与 phpunit codecoverage 结果

php - 使我的 PHP 站点免受黑客攻击

PHP POST 通过 AJAX 循环通过 javascript 发送对象文字

mysql - 如何在每个作业结束时在 talend 中实现日志记录?

php - 如何制作随时间递减的 Wilson 得分区间

python - Django 类型错误 : id() takes exactly one argument (0 given)

mysql - 如何在 MySQL 中的现有字符串末尾插入字符串类型的数据字段,有点像 CONCAT

java - 我应该如何获取文档字段并在另一个集合的另一个 map 中使用它们?

android - 在 android activity 生命周期中应该在哪里关闭数据库?