简体   繁体   English

分页和循环遍历总页数

[英]Pagination and loop through total pages count

I am building a small movie database to learn php. I make a call to the API from The Movie DB:我正在构建一个小型电影数据库来学习 php。我从 The Movie DB 调用 API:

$url = "https://api.themoviedb.org/3/search/movie?api_key=". $apiKey. "&". $language. "&query=". $searchTerm."&". $sortBy.""; // path to your JSON file

The results for the search term brother looks like:搜索词brother的结果如下所示:

object(stdClass)#2 (4) {
  ["page"]=>
  int(1)
  ["total_results"]=>
  int(1202)
  ["total_pages"]=>
  int(61)
  ["results"]=>
  array(20) {
    [0]=>
    object(stdClass)#3 (14) {
      ["popularity"]=>
      float(18.305)
      ["vote_count"]=>
      int(219)
      ["video"]=>
      bool(false)
      ["poster_path"]=>
      string(32) "/frhzQ5JJ29R3kXuVq3CRYQSczpn.jpg"
      ["id"]=>
      int(327)
      ["adult"]=>
      bool(false)
      ["backdrop_path"]=>
      string(32) "/cL1VX6WllejQQhGTUGGWHW25yor.jpg"
      ["original_language"]=>
      string(2) "en"
      ["original_title"]=>
      string(7) "Brother"
      ["genre_ids"]=>
      array(3) {
        [0]=>
        int(80)
        [1]=>
        int(18)
        [2]=>
        int(53)
      }
      ["title"]=>
      string(7) "Brother"
      ["vote_average"]=>
      int(7)
      ["overview"]=>
      string(613) "Ausgestoßen durch die Bruderschaft seines Yakuza-Clans wird der eiskalte Killer Yamamoto gezwungen, Tokio Richtung Los Angeles zu verlassen. Dort angekommen findet er sich selbst sehr schnell in der alten Routine seines gewalttätigen Tokio Leben wieder. In kürzester Zeit formt er eine neue skrupellos operierende Bruderschaft um sich herum. Macht, Mädchen und Geld liegen im zu Füssen. Aber das ist dem ehemaligen Yakuza Killer Yamamoto nicht genug. Er will alles! Er startet einen blutigen und erbitterten Kampf gegen die Mafia und muss wieder einmal feststellen, dass er von seinen Brüdern verraten wird."

The results show that I am one page 1 of 61 with total results of 1202 .结果显示我是61的第1页,总结果为1202

I know that I have to loop through the total_pages but I don't know how to build that query around the API call.我知道我必须遍历total_pages但我不知道如何围绕 API 调用构建该查询。

I am thankful for every hint.我感谢每一个提示。

From their documentation you can see that there is an optional parameter "page".从他们的文档中您可以看到有一个可选参数“page”。 So you can send following requests for each page like:因此,您可以为每个页面发送以下请求,例如:

$url = "https://api.themoviedb.org/3/search/movie?api_key=". $url = "https://api.themoviedb.org/3/search/movie?api_key=". $apiKey. $api密钥。 "&". “&”。 $language. $语言。 "&query=". “&查询=”。 $searchTerm."&". $searchTerm."&". $sortBy. $排序方式。 "&page=". “&页面=”。 "$pageNumber"; "$页码";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM