我想将一组图片与从其列表中随机抽取的引用列表配对,并为该对分配一个唯一的 url。就像尼采家庭马戏团一样。
我很确定他正在使用 php 随机提取两个列表,并使用图片和引号创建唯一标识符。
有没有人可以帮助我开始复制这个概念?谢谢。
你总是可以使用数据库。如果您在数据库中创建一个表,其中一列用于标识该对的唯一 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
您的页面将提供这些服务(images.php)只需将一个 id 传递给它(这将是唯一的,因为它是自动递增的)并查询数据库以获取与该关联的行(以及因此报价和图像) ID。