doucang6914 2011-10-31 13:25
浏览 63

分页和foreach循环亚马逊s3

I am trying to implement pagination with amazon s3, is their a way i can set when a foreach starts and when it finished?

Example i have the following output from amazon. this is the output from my 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
        )
)

i want to start from the 4th element in the foreach and stop at the 6th only show those inbetween.

$i = 0;

$start = 4;

$stop = 6;

    foreach($files as $v){ 

      if (++$i == $start)

    echo $v['name'];

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

    }

Can someone please help me on how to implement this???

  • 写回答

2条回答 默认 最新

  • drtpbx3606 2011-10-31 13:42
    关注

    It's VERY ugly but it can work (if you want to show item 4 and 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'];
    }
    

    You can use this if you are a dirty coder ! I think you can do something beautyful with Iterators.

    EDIT: Or you can also try this : (a bit more like your code)

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

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站