【问题标题】:Pagination and foreach loop amazon s3分页和foreach循环亚马逊s3
【发布时间】:2011-10-31 13:25:26
【问题描述】:

我正在尝试使用 amazon s3 实现分页,我可以设置 foreach 何时开始和何时结束?

例如我有来自亚马逊的以下输出。这是我的 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 个停止只显示中间的元素。

$i = 0;

$start = 4;

$stop = 6;

    foreach($files as $v){ 

      if (++$i == $start)

    echo $v['name'];

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

    }

有人可以帮我解决这个问题吗???

【问题讨论】:

    标签: php arrays for-loop foreach


    【解决方案1】:

    你必须让你的限制器动态化。您还需要 2 个不同的计数器进行分页。 一个计数器从零开始……下一个从 PAGE * RESULTS 开始。这两个需要在每个循环中添加 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;
    }
    

    【讨论】:

      【解决方案2】:

      它非常难看,但它可以工作(如果你想显示项目 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;
      }
      

      【讨论】:

        猜你喜欢
        • 2012-05-01
        • 2018-02-10
        • 1970-01-01
        • 2013-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-20
        相关资源
        最近更新 更多