使用URL查询构建API服务,每页20个数据项(codeigniter PHP)

时间:2016-12-05 01:28:03

标签: php json codeigniter

我想为我的图片文件夹(目录)构建具有以下功能的API服务:

  1. 但我想使用网址查询每页显示20张图片网址。

  2. 反向排序。

  3. 示例:

    我的目录中有这些图片

    1. /images/path.jpg
    2. /images/path1.jpg
    3. /images/path2.jpg
    4. /images/path3.jpg
    5. /images/path4.jpg
    6. /images/path5.jpg
    7. /images/path6.jpg
    8. /images/path7.jpg
    9. /images/path8.jpg 10./images/path9.jpg
    10. /images/path10.jpg
    11. /images/path11.jpg
    12. /images/path12.jpg
    13. /images/path13.jpg
    14. /images/path14.jpg
    15. /images/path15.jpg
    16. /images/path16.jpg
    17. /images/path17.jpg
    18. /images/path18.jpg
    19. /images/path19.jpg
    20. /images/path20.jpg
    21. /images/path21.jpg
    22. /images/path22.jpg
    23. /images/path23.jpg
    24. /images/path24.jpg
    25. /images/path25.jpg
    26. /images/path26.jpg
    27. /images/path27.jpg
    28. /images/path28.jpg
    29. /images/path29.jpg
    30. /images/path30.jpg
    31. /images/path31.jpg
    32. /images/path32.jpg
    33. /images/path33.jpg
    34. /images/path34.jpg
    35. /images/path35.jpg
    36. /images/path36.jpg
    37. /images/path37.jpg
    38. /images/path38.jpg
    39. /images/path39.jpg
    40. /images/path40.jpg
    41. /images/path41.jpg
    42. 我希望API网址和数据项看起来像这样

      mypage.com/images-json?page=1

      {
      "img1": "/images/path41.jpg",
      "img2": "/images/path40.jpg",
      "img3": "/images/path39.jpg",
      "img4": "/images/path38.jpg",
      "img5": "/images/path37.jpg",
      "img6": "/images/path36.jpg",
      "img7": "/images/path35.jpg",
      "img8": "/images/path34.jpg",
      "img9": "/images/path33.jpg",
      "img10": "/images/path32.jpg",
      "img11": "/images/path31.jpg",
      "img12": "/images/path30.jpg",
      "img13": "/images/path29.jpg",
      "img14": "/images/path28.jpg",
      "img15": "/images/path27.jpg",
      "img16": "/images/path26.jpg",
      "img17": "/images/path25.jpg",
      "img18": "/images/path24.jpg",
      "img19": "/images/path23.jpg",
      "img20": "/images/path22.jpg"
      }
      

      mypage.com/images-json?page=2

      {
      "img1": "/images/path21.jpg",
      "img2": "/images/path20.jpg",
      "img3": "/images/path19.jpg",
      "img4": "/images/path18.jpg",
      "img5": "/images/path17.jpg",
      "img6": "/images/path16.jpg",
      "img7": "/images/path15.jpg",
      "img8": "/images/path14.jpg",
      "img9": "/images/path13.jpg",
      "img10": "/images/path12.jpg",
      "img11": "/images/path11.jpg",
      "img12": "/images/path10.jpg",
      "img13": "/images/path9.jpg",
      "img14": "/images/path8.jpg",
      "img15": "/images/path7.jpg",
      "img16": "/images/path6.jpg",
      "img17": "/images/path5.jpg",
      "img18": "/images/path4.jpg",
      "img19": "/images/path3.jpg",
      "img20": "/images/path2.jpg"
      }
      

      mypage.com/images-json?page=3

      {
      "img1": "/images/path1.jpg",
      "img2": "/images/path.jpg"
      }
      

      目前我只有这个代码,但是这段代码只显示我目录文件夹中的所有图片,排序也不反转:

      <?php 
      $this->load->helper('directory'); //load directory helper
      $dir = "images"; // Your Path to folder
      $map = directory_map($dir); /* This function reads the directory path specified in the first parameter and builds an array representation of it and all its contained files. */
      
      foreach ($map as $k)
      {?>
           <img src="<?php echo base_url($dir)."/".$k;?>" alt="">
      
      <?php }
      
      ?> 
      

1 个答案:

答案 0 :(得分:0)

尝试对目录网址使用FC_PATH,因为base_url不适用于目录数据。所以希望它能帮到你。

<?php 
     $this->load->helper('directory'); //load directory helper
     $dir = FCPATH."images/"; // Your Path to folder
     $map = directory_map($dir); /* This function reads the directory path specified in the first parameter and builds an array representation of it and all its contained files. */
     //and for sorting may be use krsort for this .
     krsort($map);

     foreach ($map as $k)
     {?>
        <img src="<?php echo $dir.$k;?>" alt="">

    <?php }
?>