PHP/MYSQL:如何创建在每次页面刷新/加载期间旋转/更改的图像库


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

我正在建立一个网站,该网站将只包含单个页面上的图像。此页面上有 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>

照原样,上面的代码(回显$row语句)不起作用。我能够从数据库中提取数据的唯一方法是将 echo 语句放在"while 循环"下,就像这样。

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

如果我在代码中保持 ORDER BY RAND() 限制 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++;
}