Jump to content

JSON Decode


WebmasterGuy

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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 by WebmasterGuy
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by WebmasterGuy
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.