我想将一组图片与每个从列表中随机抽取的引号列表配对,并为该对分配一个唯一的URL。很像The Nietzsche Family Circus。
我很确定他正在使用php为两个列表随机抽取并创建带有图片和引号唯一标识符的网址。
有没有人可以帮助我开始复制这个概念?感谢。
答案 0 :(得分:0)
您始终可以使用数据库。如果您在数据库中创建一个表,其中一列用于标识该对的唯一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相关联。