2012-05-26 5 views
-2

사진 세트를 목록에서 무작위로 추출한 따옴표 목록과 페어링하고 고유 한 URL을 할당하고 싶습니다. The Nietzsche Family Circus과 매우 비슷합니다.무작위 이미지를 무작위 따옴표와 쌍으로 만들고 고유 한 URL을 할당하십시오.

나는 그가 php를 사용하여 두 목록을 무작위로 추출하고 그림과 함께 URL을 작성하고 고유 식별자를 인용한다는 것을 확신합니다.

이 개념을 복제하는 데 도움을 줄 수있는 사람이 있습니까? 감사.

+1

특히 어떤 부분이 문제입니까? –

답변

0

항상 데이터베이스를 사용할 수 있습니다. 그런 다음

$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'])); 
} 

: 당신이 인용 텍스트의 쌍, 이미지 URL에 대한 하나 개의 컬럼을 식별하는 고유 한 URL에 대한 하나의 열 한 열이 데이터베이스에서 테이블을 작성하는 경우에는 다음과 같은 뭔가를 할 수 단순히 (이 autoincremented되고 있었기 때문에 고유의 것)에 전달 ID를 걸릴 것이 최대 (images.php)를 제공 할

$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 

페이지를 데이터베이스에 쿼리 : 임의의 URL을 얻을 수 있습니다 해당 ID와 연관된 행 (따라서 따옴표 및 이미지)을 가져옵니다.

관련 문제