WebmasterGuy Posted May 14, 2014 Share Posted May 14, 2014 Hello everyone, I'm new here. Amazing forum, I found some help in the past by searching google. Now I am looking for some help with the following code, I use this code to grab a JSON Feed and list the items in my page, this code works fine to get all the items, but I need to limit the number of items, otherwise It will load more than 500 items in my page and never stop loading. Is there a way to limit the number of items grabbed from the JSON Feed? Let's say, list 50 items in page 1, another 50 in page 2 and so on? <?php $data = file_get_contents('http://json-url.com/type=json'); $decode = json_decode($data, true); foreach($decode as $d) { if($d['status'] == true){ echo '<div class="div">'; echo '<a href="'.$d['item_url'].'">'; echo '<img src="'.$d['item_image']['jpeg_thumbnail'].'" />'; echo '<span>'.$d['item_name'].'</span>'; echo '</a>'; echo '</div>'; } } ?> Thanks! Quote Link to comment Share on other sites More sharing options...
Barand Posted May 14, 2014 Share Posted May 14, 2014 $offset = ($page-1) * $itemsPerPage $decode = array_slice(json_decode($data, true), $offset, $itemsPerPage); Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 14, 2014 Share Posted May 14, 2014 It doesn't make much sense to load all data and then throw away the unwanted 90%. The page will still be slow, and you'll waste a lot of bandwidth. Check the API of the feed. It should allow you to load only partial content. Depending on how frequently the data changes, you should also consider throwing away the static pagination altogether. If new entries are added while a user browses the pages, then this user will either miss entries or see the same entries on different pages. Quote Link to comment Share on other sites More sharing options...
WebmasterGuy Posted May 15, 2014 Author Share Posted May 15, 2014 (edited) $offset = ($page-1) * $itemsPerPage $decode = array_slice(json_decode($data, true), $offset, $itemsPerPage); Thanks, I tried inserting that code into the PHP file, but received some error: Parse error: syntax error, unexpected T_VARIABLE It doesn't make much sense to load all data and then throw away the unwanted 90%. The page will still be slow, and you'll waste a lot of bandwidth. Check the API of the feed. It should allow you to load only partial content. Depending on how frequently the data changes, you should also consider throwing away the static pagination altogether. If new entries are added while a user browses the pages, then this user will either miss entries or see the same entries on different pages. I understand, so do you think it's better to send all this to a MySql DB and then get the data from there? Thanks for your message. Edited May 15, 2014 by WebmasterGuy Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 15, 2014 Share Posted May 15, 2014 I understand, so do you think it's better to send all this to a MySql DB and then get the data from there? No, I think you should utilize the API of the JSON feed. If the API is garbage and doesn't support any kind of limit, then, yes, you'll probably have to cache the data in your own database. Quote Link to comment Share on other sites More sharing options...
WebmasterGuy Posted May 15, 2014 Author Share Posted May 15, 2014 (edited) No, I think you should utilize the API of the JSON feed. If the API is garbage and doesn't support any kind of limit, then, yes, you'll probably have to cache the data in your own database. Yes unfortunately the site is not giving me any options to grab partial content. They only give me the full Feed, either in JSON or XML format. Thx for your input. EDIT: BTW, is it too hard to do the MySql part? I have some very basic knowledge about PHP+MySql, do you think I need to hire a programmer or is there some guide to do something like this. I looked for some help in google, but every page I find, it looks like oriented to advanced programmers, and they avoid posting the whole process, like assuming the reader is an advanced coder. Edited May 15, 2014 by WebmasterGuy Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 15, 2014 Share Posted May 15, 2014 No, it's not difficult, but you'll need some time and motivation to learn the basics of MySQL and PDO. Do not rely on “tutorials”. Most of them are crap, and it's generally a bad idea to copy and paste code without understanding it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.