将随机图像与随机引号配对,并为该对分配唯一的URL


Pair a random image with a random quote and assign that pair a unique url

我想将一组图片与引号列表配对,每个引号都从其列表中随机提取,并为该对分配一个唯一的URL。就像尼采家庭马戏团一样。

我很确定他正在使用 php 随机提取两个列表,并使用图片和引用唯一标识符创建 url。

有没有人可以帮助我开始复制这个概念?谢谢。

你总是可以使用数据库。如果在数据库中创建一个表,其中一列用于标识对的唯一 URL,一列用于图像 URL,一列用于引用文本,则可以执行以下操作:

$quoteArray = array(); //in reality this would be filled with your quotes
$imageArray = array(); //this would be filled with your image urls
$pairs = array();
while(count($quoteArray) > 0 && count($imageArray) > 0) {
    $quoteIndex = rand(0, count($quoteArray) - 1); //get some random indexes
    $imageIndex = rand(0, count($imageArray) - 1);
    $pairs[] = array('quote' => $quoteArray[$quoteIndex], 'image' => $imageArray[$imageIndex]);
    unset($quoteArray[$quoteIndex]); //keep us from using the same quote twice
    unset($imageArray[$imageIndex]); //same goes for the images
}
//this assumes your table has an autoincremeting id field
$stmt = $connection->prepare("INSERT INTO awesomeTable (quote, image) VALUES( :quote , :image )");
//$connection is a PDO which you would have created earlier...look in the php manual for information on setting this up
foreach($pairs as $pair) {
    $uniqueId = sha1($quote . $image);
    $stmt->execute(array(':quote' => $pair['quote'], ':image' => $pair['image']));
}

然后,要获取随机网址:

$result = $connection->query("SELECT * FROM awesomeTable ORDER BY RAND() LIMIT 1");
$record = $result->fetch();
//record will contain either an object (accessed by $row->url, $row->quote, etc) or an
//associative array (accessed by $row['url'], $row['quote'], etc) depending on how you
//set up the PDO

提供这些(图像.php)的页面将简单地获取传递给它的 id(这将是唯一的,因为它是自动递增的)并查询数据库以获取与该 id 关联的行(以及引号和图像)。