2013-02-12 72 views
1

我目前有一張如下所示的表格。從數組中回顯特定行

ID adPlacement  filePath dateAdded adName adLink 
12 1   Test 1.png 2013-02-12 Test 1 http://www.cuad.coop 
13 1   Test 2.png 2013-02-12 Test 2 http://www.google.com 

我想隨機選擇一行並在獨立的echo語句中回顯adName,adLink和filePath。

這裏是我使用的是現在的代碼:

$query_adSpot1 = "SELECT * FROM advertisements WHERE adPlacement = 1"; 
$result = mysql_query($query_adSpot1, $server) or die(mysql_error()); 
while($row = mysql_fetch_array($result)){ 
$row = array(
    'adName' => $row['adName'], 
    'filePath' => $row['filePath'], 
    'adLink' => $row['adLink'] 
); 
$fileLocation = $row; 
$fileLocations[] = $fileLocation; 
} 
shuffle($fileLocation); 
echo $fileLocation[0]; 

現在,當我運行該腳本,它將寫入測試2.png,或測試2,或http://www.google.com

我希望能夠從一個隨機行分開回顯,但需要單獨的列等於同一行。

echo filePath 
echo adName 
echo adLink 
+2

回聲$ filelocaton [ 'INDEXNAME'] – Sedz 2013-02-12 18:38:43

回答

1

你洗牌了錯誤的數組,你應該使用:

shuffle($fileLocations); 
        ^This is the one with all your values 
var_dump($fileLocations[0]); 
// will show you an array with 3 elements from the same row in the database 

你所要做的是重新排序在SQL查詢中發現最後一排。

+0

如何單獨回聲出列?我加到最後: shuffle($ fileLocations); var_dump($ fileLocations [0]); echo $ row ['adName']; 所以,如果我想回顯出其中的一個元素。 – StevoBot 2013-02-12 19:36:13

+0

@StevoBot像'echo $ fileLocations [0] ['adName']','echo $ fileLocations [0] ['filePath']'等 – jeroen 2013-02-12 20:26:27

+0

謝謝,這幫了我的忙,讓它做我想找的東西。 – StevoBot 2013-02-27 03:06:11

0

可以使用RAND()功能MySQL

$query_adSpot1 = "SELECT * FROM advertisements WHERE adPlacement = 1 ORDER BY RAND() LIMIT 1"; 
$result = mysql_query($query_adSpot1, $server) or die(mysql_error()); 
$row = mysql_fetch_array($result); 
echo $row['filePath']; 
echo $row['adName']; 
echo $row['adLink'];