Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Programming Q&A
HTML/CSS Responsive question
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Solar" data-source="post: 470749" data-attributes="member: 12018"><p>Hello Skythrust,</p><p></p><p>I've never done a pagination yet with arrays and JSON so I wanted to give my input on it!</p><p></p><p>Get the JSON file and decode it into an array.</p><p>[CODE=php]</p><p>$file = file_get_contents("persons.json");</p><p>$persons = json_decode($file,true);</p><p>//print_r($persons);[/CODE]</p><p></p><p>The json_decode (above) has an extra parameter "<em>true" </em>to help us iterate through the object (below) easier.</p><p></p><p>[CODE=php]$page = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1;</p><p>$total = count( $persons ); //total items in array</p><p>$limit = 2; //per page</p><p>$totalPages = ceil( $total/ $limit ); //calculate total pages</p><p>$page = max($page, 1); //get 1 page when $_GET['page'] <= 0</p><p>$page = min($page, $totalPages); //get last page when $_GET['page'] > $totalPages</p><p>$offset = ($page - 1) * $limit;</p><p>if($offset < 0) $offset = 0;</p><p></p><p>$persons = array_slice($persons,$offset,$limit);</p><p></p><p>foreach($persons as $p){</p><p> echo $p['FrstName'].'<br/>';</p><p>}</p><p></p><p>$link = 'index.php?page=%d';</p><p>$pagerContainer = '<div style="width: 300px;">';</p><p>if($totalPages != 0){</p><p> if($page == 1){</p><p> $pagerContainer .= '';</p><p> }else{</p><p> $pagerContainer .= sprintf( '<a href="' . $link . '" style="color: #c00"> &#171; prev page</a>', $page - 1 );</p><p> }</p><p> $pagerContainer .= ' <span> page <strong>' . $page . '</strong> from ' . $totalPages . '</span>';</p><p> if($page == $totalPages){</p><p> $pagerContainer .= '';</p><p> }else{</p><p> $pagerContainer .= sprintf( '<a href="' . $link . '" style="color: #c00"> next page &#187; </a>', $page + 1 );</p><p> }</p><p>} </p><p>$pagerContainer .= '</div>';</p><p></p><p>echo $pagerContainer;[/CODE]</p><p></p><p>I left the pagination almost the same as yours. I would totally do something different but seeing how you're settled on this code, we'll stick to it.</p><p></p><p>and the persons.json</p><p>[CODE=json][</p><p>[{</p><p> "FrstName": "Henry",</p><p> "Middlename": "",</p><p> "LastName": "Walton",</p><p> "Online": true,</p><p> "DeptId": "4"</p><p> },</p><p> {</p><p> "FrstName": "Klaus",</p><p> "Middlename": "",</p><p> "LastName": "Mikaelson",</p><p> "Online": true,</p><p> "DeptId": "2"</p><p> },</p><p> {</p><p> "FrstName": "Kylo",</p><p> "Middlename": "",</p><p> "LastName": "Ren",</p><p> "Online": false,</p><p> "DeptId": "4"</p><p> },</p><p> {</p><p> "FrstName": "Stan",</p><p> "Middlename": "",</p><p> "LastName": "Lee",</p><p> "Online": false,</p><p> "DeptId": "3"</p><p> },</p><p> {</p><p> "FrstName": "Kevin",</p><p> "Middlename": "",</p><p> "LastName": "McNally",</p><p> "Online": false,</p><p> "DeptId": "3"</p><p> },</p><p> {</p><p> "FrstName": "Katherine",</p><p> "Middlename": "",</p><p> "LastName": "Pierce",</p><p> "Online": false,</p><p> "DeptId": "2"</p><p> },</p><p> {</p><p> "FrstName": "Clint",</p><p> "Middlename": "",</p><p> "LastName": "Barton",</p><p> "Online": true,</p><p> "DeptId": "3"</p><p> },</p><p> {</p><p> "FrstName": "Hope",</p><p> "Middlename": "van",</p><p> "LastName": "Dyne",</p><p> "Online": false,</p><p> "DeptId": "3"</p><p> }</p><p>]</p><p>][/CODE]</p><p></p><p>Hope this helps!</p></blockquote><p></p>
[QUOTE="Solar, post: 470749, member: 12018"] Hello Skythrust, I've never done a pagination yet with arrays and JSON so I wanted to give my input on it! Get the JSON file and decode it into an array. [CODE=php] $file = file_get_contents("persons.json"); $persons = json_decode($file,true); //print_r($persons);[/CODE] The json_decode (above) has an extra parameter "[I]true" [/I]to help us iterate through the object (below) easier. [CODE=php]$page = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1; $total = count( $persons ); //total items in array $limit = 2; //per page $totalPages = ceil( $total/ $limit ); //calculate total pages $page = max($page, 1); //get 1 page when $_GET['page'] <= 0 $page = min($page, $totalPages); //get last page when $_GET['page'] > $totalPages $offset = ($page - 1) * $limit; if($offset < 0) $offset = 0; $persons = array_slice($persons,$offset,$limit); foreach($persons as $p){ echo $p['FrstName'].'<br/>'; } $link = 'index.php?page=%d'; $pagerContainer = '<div style="width: 300px;">'; if($totalPages != 0){ if($page == 1){ $pagerContainer .= ''; }else{ $pagerContainer .= sprintf( '<a href="' . $link . '" style="color: #c00"> « prev page</a>', $page - 1 ); } $pagerContainer .= ' <span> page <strong>' . $page . '</strong> from ' . $totalPages . '</span>'; if($page == $totalPages){ $pagerContainer .= ''; }else{ $pagerContainer .= sprintf( '<a href="' . $link . '" style="color: #c00"> next page » </a>', $page + 1 ); } } $pagerContainer .= '</div>'; echo $pagerContainer;[/CODE] I left the pagination almost the same as yours. I would totally do something different but seeing how you're settled on this code, we'll stick to it. and the persons.json [CODE=json][ [{ "FrstName": "Henry", "Middlename": "", "LastName": "Walton", "Online": true, "DeptId": "4" }, { "FrstName": "Klaus", "Middlename": "", "LastName": "Mikaelson", "Online": true, "DeptId": "2" }, { "FrstName": "Kylo", "Middlename": "", "LastName": "Ren", "Online": false, "DeptId": "4" }, { "FrstName": "Stan", "Middlename": "", "LastName": "Lee", "Online": false, "DeptId": "3" }, { "FrstName": "Kevin", "Middlename": "", "LastName": "McNally", "Online": false, "DeptId": "3" }, { "FrstName": "Katherine", "Middlename": "", "LastName": "Pierce", "Online": false, "DeptId": "2" }, { "FrstName": "Clint", "Middlename": "", "LastName": "Barton", "Online": true, "DeptId": "3" }, { "FrstName": "Hope", "Middlename": "van", "LastName": "Dyne", "Online": false, "DeptId": "3" } ] ][/CODE] Hope this helps! [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
HTML/CSS Responsive question
Top