Welcome Guest, Not a member yet? Register   Sign In
Pages dynamically using json
#1

Hi,

I need some help about show pages dynamically using json.
For example, I got a json with a lot of ID-s, and I want to show these IDs on separate pages, like:
domain.com/article/1
domain.com/article/2
domain.com/article/...

and this is the json:
Code:
{
   "articles": [
      {
         "id": 1,
      },
      {
         "id": 2,
      }
   ]
}

Is anybody know how can I implement?

Thanks
Reply
#2

(This post was last modified: 01-31-2015, 12:33 PM by CroNiX.)

First, that's not valid JSON. You can't have trailing commas like you have after your id values. If it were valid JSON, you could just...

Code:
//removed trailing commas for valid JSON
$json = '{
   "articles": [
      {
         "id": 1
      },
      {
         "id": 2
      }
   ]
}';

$articles = json_decode($json, true);

foreach($articles['articles'] as $article)
{
  echo 'domain.com/article/' . $article['id'] . '<br>';
}
Reply
#3

(01-31-2015, 12:32 PM)CroNiX Wrote: First, that's not valid JSON. You can't have trailing commas like you have after your id values. If it were valid JSON, you could just...


Code:
//removed trailing commas for valid JSON
$json = '{
  "articles": [
     {
        "id": 1
     },
     {
        "id": 2
     }
  ]
}';

$articles = json_decode($json, true);

foreach($articles['articles'] as $article)
{
 echo 'domain.com/article/' . $article['id'] . '<br>';
}


Thank, and you are right, the json was wrong.

I'm sorry, the question wasn't good (my fault).
I need to generate pages dynamically.

I have a 3rd party json url with id-s and other information, like author, text etc.
I went through this json with js and list these articles on the index page.
All generated articles has an ID, what I want to use like a page (e.g. domain.com/articles/256)

So when the user click one of the article in the list, they can go to that page.
Also this page has to be a template I think, because I want to show that information from the json (author, text etc.)

Thanks,
L
Reply
#4

(01-31-2015, 12:32 PM)CroNiX Wrote: First, that's not valid JSON. You can't have trailing commas like you have after your id values. If it were valid JSON, you could just...


Code:
//removed trailing commas for valid JSON
$json = '{
  "articles": [
     {
        "id": 1
     },
     {
        "id": 2
     }
  ]
}';

$articles = json_decode($json, true);

foreach($articles['articles'] as $article)
{
 echo 'domain.com/article/' . $article['id'] . '<br>';
}

Thanks,

My question was wrong.
I have a 3rd party json url what includes author, text etc. and this id.
In my index page I listed these articles with js. Every article has ID, and I want to create pages dynamically to see the further information, like a template.

So when the user click the article, they can jump to the page (e.g. domain.com/article/2938)

How can I manage this?

Thanks
Reply
#5

Thanks,

My question was wrong.
I have a 3rd party json url what includes author, text etc. and this id.
In my index page I listed these articles with js. Every article has ID, and I want to create pages dynamically to see the further information, like a template.

So when the user click the article, they can jump to the page (e.g. domain.com/article/2938)

How can I manage this?

Thanks
Reply
#6

(This post was last modified: 01-31-2015, 03:00 PM by CroNiX.)

I'm not following what you mean by "listed these articles with js". Are you saying you retrieve the json with js, parse it and build links? All using pure js? Or are you retrieving the json with php, parsing it and building links in a view?

If it's PHP, you can use the code I showed and just modify what the output is (links instead of test).
Code:
foreach($articles['articles'] as $article)
{
echo '<div>' . anchor('article/' . $article['id'], "Article " . $article['id']) . '</div>';
}

Then, you'd have an article controller that receives the ID

Code:
class Article extends CI_Controller {
  public function index($article_id = 0)
  {
    if ($article_id)
    {
      //use your model to retrieve the article with this ID
    }
  }
}

If it's using pure JS, then you'd build your links using JS, which still point to the articles controller passing its id.
Reply
#7

Personally, I wouldn't build the links using JS if that's what you're doing. Search engines won't find them if it's not in the initial HTML markup, since they don't use JS. May not be applicable to you, but thought I'd toss it out there.
Reply
#8

Thank you for your answer!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB