2012-10-02 73 views
0

我已閱讀了許多關於上一個/下一個的帖子,我仍然非常困難。php當前數組的位置和上一個/下一個

我有一個頁面顯示圖像和相關數據。這是從數據庫中獲得的。這些圖像位於每個相冊文件夾中的網站文件夾中。所以圖像文件夾可能包含1,2或100個圖像。

我有一個功能,該數據如下: -

function get_images_album($artist_id) { 
    $artist_id = (int)$artist_id; 
    $images = array(); 
    $image_query = mysql_query("SELECT `image_album_id`, `member_id`, `artist_id`,  
        `albumname`, `ext`, `timestamp` FROM `album_images` 
        WHERE `artist_id`=$artist_id AND `member_id`=".$_SESSION['member_id']); 
    while ($images_row = mysql_fetch_assoc($image_query)) { 
     $images[] = array(
      'id' => $images_row['image_album_id'], 
      'album' => $images_row['artist_id'], 
      'albumname' => $images_row['albumname'], 
      'ext' => $images_row['ext'], 
      'timestamp' => $images_row['timestamp'] 
      ); 
    } 
    return $images; 
} 

頁面顯示每localhost上的網址正確的圖像和數據。網站/ album_view.php?artist_id = 4 & image_album_id = 4。

在HTML頁面的代碼是: -

$images = get_images_album($artist_id); 
if (empty($images)) { 
    echo 'There are no Images in this album.'; 
}else{ 
    foreach ($images as $key => $val) { 

    } 
} 

>

我的理解是,如果我回聲$鍵和電流($ VAL)我會得到數組索引號?和當前頁面的圖像ID。在上面的情況下,這將是0和4.但是,我總是得到數組的最後一個索引詳細信息,該特定專輯恰好是13和2181。

我知道所有的數據在那裏,因爲我已經迴應了數組,並且一切似乎都沒問題。

我不知道爲什麼。

繼續從我有什麼我不能解決如何繼續獲得下一個和以前的設置謝謝。

+1

我不明白你的問題實際上是什麼 - 你想達到什麼目的? – moonwave99

+0

您可以在'$ images = get_images_album($ artist_id)'之後添加'var_dump($ images);'併發布結果嗎?查看結果數據集將有所幫助。我猜你會得到這些值,因爲它是遍歷整個結果集後的最後一個'foreach()'。你是否想要從集合中獲得每件物品,或者只是第一件物品,還是..? –

+0

嗨, 謝謝你的回答。 我最終試圖從顯示的圖像開始上一個/下一個鏈接。這可能是陣列位置1 5 10或其他。 使用var_dump($ images)給出的結果與顯示的圖像相匹配,並且將會改變以反映所顯示的圖像(正確)。所以這意味着如果我使用foreach循環,它將始終在循環列表後顯示最後一個元素。希望我理解正確。 – kevo

回答

0

更簡單的方法去了解這可能是改變你的get_images_album稍微功能:

function get_images_album($artist_id) { 
    $artist_id = (int)$artist_id; 
    $images = array(); 
    $image_query = mysql_query("SELECT `image_album_id`, `member_id`, `artist_id`,  
        `albumname`, `ext`, `timestamp` FROM `album_images` 
    WHERE `artist_id`=$artist_id AND `member_id`=".$_SESSION['member_id']); 
    While ($images_row = mysql_fetch_assoc($image_query)) { 
    // $images[] = array(
    // instead, make your images array a hash and key it on the album_id 
    $images[$images_row["image_album_id"]] = array(
     'id' => $images_row['image_album_id'], 
     'album' => $images_row['artist_id'], 
     'albumname' => $images_row['albumname'], 
     'ext' => $images_row['ext'], 
     'timestamp' => $images_row['timestamp'] 
    ); 
    } 
    return $images; 
} 

現在你可以消除你的foreach循環,只是讓你要找的專輯:

$images = get_images_album($artist_id); 
if (empty($images)) { 
echo 'There are no Images in this album.'; 
}else{ 
    $album_images = $images[$image_album_id]; 

    // do stuff with the $album_images hash 
    var_dump($album_images); 

} 
+0

嗨, 謝謝你的回答。 我最終試圖從顯示的圖像開始上一個/下一個鏈接。這可能是陣列位置1 5 10或 不確定我關注你rkstar。我可以得到專輯確定和圖像。但我無法弄清楚如何從數組中的任何位置獲取上一張或下一張圖像。 我很欣賞你對功能變化的觀點。 – kevo

相關問題