2011-10-31 82 views
1

我試圖實現與Amazon S3的分頁,是他們的一種方式時,當它完成了一個foreach開始,我可以設置?分頁和foreach循環Amazon S3的

示例我從amazon獲得以下輸出。這是我的print_r($ files)的輸出;

Array 
(
    [Computer Fix Files/] => Array 
     (
      [name] => Computer Fix Files/ 
      [time] => 1296249553 
      [size] => 0 
      [hash] => d41d8cd98f00b204e9800998ecf8427e 
     ) 

    [Computer Fix Files/3DP_Net_v1101.exe] => Array 
     (
      [name] => Computer Fix Files/3DP_Net_v1101.exe 
      [time] => 1296254834 
      [size] => 33672146 
      [hash] => 068b68fdafe6a8e55020dbbc152b3a26 
     ) 

    [Computer Fix Files/DriverEasy_Setup.exe] => Array 
     (
      [name] => Computer Fix Files/DriverEasy_Setup.exe 
      [time] => 1296255133 
      [size] => 1838760 
      [hash] => 4f7d0cfe38f62637b2a4c1d7226a2821 
     ) 

    [Computer Fix Files/Dropbox 0.7.110.exe] => Array 
     (
      [name] => Computer Fix Files/Dropbox 0.7.110.exe 
      [time] => 1296255148 
      [size] => 13525424 
      [hash] => 843135bb60735f8a40810555ee960d5d 
     ) 

    [Computer Fix Files/GoodSync-Setup.exe] => Array 
     (
      [name] => Computer Fix Files/GoodSync-Setup.exe 
      [time] => 1296255257 
      [size] => 6124120 
      [hash] => f9b197072675bb930d529080563671d0 
     ) 

    [Computer Fix Files/ccsetup302.exe] => Array 
     (
      [name] => Computer Fix Files/ccsetup302.exe 
      [time] => 1296255107 
      [size] => 2976440 
      [hash] => 8142275100b3de92db382a0deb3a24d5 
     ) 

    [Computer Fix Files/driver-ml-dlan-usb-windows-r2.exe] => Array 
     (
      [name] => Computer Fix Files/driver-ml-dlan-usb-windows-r2.exe 
      [time] => 1296255131 
      [size] => 262144 
      [hash] => 4e673fbe72579e3eaeccd2e3d8c22e46 
     ) 

    [Computer Fix Files/installer_driver_hercules_dj_console_mk2_pc_2009_English.exe] => Array 
     (
      [name] => Computer Fix Files/installer_driver_hercules_dj_console_mk2_pc_2009_English.exe 
      [time] => 1296255306 
      [size] => 2844911 
      [hash] => c52f7500a8c3be68c62fd500ca0cb211 
     ) 

    [Computer Fix Files/jre-6u21-windows-i586-iftw-rv.exe] => Array 
     (
      [name] => Computer Fix Files/jre-6u21-windows-i586-iftw-rv.exe 
      [time] => 1296255329 
      [size] => 875296 
      [hash] => dfccbb06ed411e0c006f05bcb1bdf7c2 
     ) 

    [Computer Fix Files/m4a-to-mp3-converter.exe] => Array 
     (
      [name] => Computer Fix Files/m4a-to-mp3-converter.exe 
      [time] => 1296255336 
      [size] => 5461664 
      [hash] => 4bb68b384902f3f4fcc78e69d795e82d 
     ) 

    [Computer Fix Files/software-dlan-windows-v20.exe] => Array 
     (
      [name] => Computer Fix Files/software-dlan-windows-v20.exe 
      [time] => 1296255380 
      [size] => 2393336 
      [hash] => 4034845466edd280e3241ff93c61bfde 
     ) 

    [Music/] => Array 
     (
      [name] => Music/ 
      [time] => 1296576896 
      [size] => 0 
      [hash] => d41d8cd98f00b204e9800998ecf8427e 
     ) 

    [Music/TheBeautifulSouth/] => Array 
     (
      [name] => Music/TheBeautifulSouth/ 
      [time] => 1296610421 
      [size] => 0 
      [hash] => d41d8cd98f00b204e9800998ecf8427e 
     ) 

    [Music/TheBeautifulSouth/1-01 Song for Whoever (Single Versio.m4a] => Array 
     (
      [name] => Music/TheBeautifulSouth/1-01 Song for Whoever (Single Versio.m4a 
      [time] => 1296610421 
      [size] => 8667378 
      [hash] => c0a7ba2388267a542049369bf90e7dd1 
     ) 

    [Music/TheBeautifulSouth/1-02 A Little Time.m4a] => Array 
     (
      [name] => Music/TheBeautifulSouth/1-02 A Little Time.m4a 
      [time] => 1296610491 
      [size] => 6467370 
      [hash] => c9ca14f142709a90196ab9b447ee83e1 
     ) 
) 

我想從foreach中的第4個元素開始,並停在第6個只顯示那些inbetween。

$i = 0; 

$start = 4; 

$stop = 6; 

    foreach($files as $v){ 

     if (++$i == $start) 

    echo $v['name']; 

     if (++$i == $stop) break; 

    } 

有人可以請幫助我如何實現這???

回答

0

這是非常醜陋的,但它可以工作(如果你想顯示的項目4和5):

reset ($files); 
for($i = 0; $i < 3; $i++) { 
    next($files); 
} 
for ($i = 0, $current = current($files); $i < 2 && false !== $current; $i++, $current = next($files)) { 
    echo $current['name']; 
} 

你可以使用這個如果你是一個骯髒的編碼器!我認爲你可以對迭代器做一些美事。

編輯: 或者你也可以試試這個:(更像是你的代碼位)

$i = 0; 
$start = 4; 
$stop = 6; 
foreach($files as $v){ 
    if (++$i >= $start) echo $v['name']; 
    if ($i == $stop) break; 
} 
0

你必須讓你的限制器的動態。您還需要2個不同的分頁計數器。 一個計數器從零開始......下一個從PAGE *結果開始。這兩個需要每個循環都加1:

$c=0; 
$p=$_GET[page] * 25; 
$l=$p + 25; 
foreach($array as $obj){ 
$c++; $p++; 
if ($p==$l) break; 
if ($c>$p) echo $obj; 
}