Jump to content


Photo

JSON Decode


  • Please log in to reply
6 replies to this topic

#1 WebmasterGuy

WebmasterGuy

    Newbie

  • New Members
  • Pip
  • 3 posts

Posted 14 May 2014 - 05:29 PM

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!



#2 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,135 posts
  • LocationCheshire, UK

Posted 14 May 2014 - 05:35 PM

$offset = ($page-1) * $itemsPerPage

$decode = array_slice(json_decode($data, true), $offset, $itemsPerPage);


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#3 Jacques1

Jacques1

    Advanced Member

  • Members
  • PipPipPip
  • 563 posts

Posted 14 May 2014 - 06:04 PM

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.



#4 WebmasterGuy

WebmasterGuy

    Newbie

  • New Members
  • Pip
  • 3 posts

Posted 14 May 2014 - 11:28 PM

$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, 14 May 2014 - 11:29 PM.


#5 Jacques1

Jacques1

    Advanced Member

  • Members
  • PipPipPip
  • 563 posts

Posted 14 May 2014 - 11:37 PM

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.



#6 WebmasterGuy

WebmasterGuy

    Newbie

  • New Members
  • Pip
  • 3 posts

Posted 15 May 2014 - 12:43 AM

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, 15 May 2014 - 12:53 AM.


#7 Jacques1

Jacques1

    Advanced Member

  • Members
  • PipPipPip
  • 563 posts

Posted 15 May 2014 - 01:18 AM

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.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com