Jump to content

Use PHP to update a txt doc, then convert doc to JSON?


melting_dog

Recommended Posts

Hi all,

 

I am trying to figure out a way to make a JSON script more dynamic and responsive to a database.

 

I am forming and idea in my head and just wanted to run it past people here - not expecting any coding answers just want to know if it is possible.

 

Use PHP to query the DB and create some vars,

Write the vars to a txt file adding in the necessary JSON brackets,

Use PHP's JSON Encode to encode the txt file to JSON

Repeat when ever the database is updated

 

What do you guys think? Is this a plausible scenario?

Link to comment
Share on other sites

Besides step 2 being completely unnecessary, and even detrimental, I'm struggling to figure out why you want to do this?

Why not just have the PHP script pull the data from the database at request time, encode it and send it directly to the requesting source? Keep it simple, and you'll have less troubles with writing the code as well as maintaining it. Not to mention that you can't get more responsive than pulling it from the database at request time.

 

PS: JSON isn't a script, it's a data format. I'm assuming you're referring to a JavaScript, that expects to receive the data in the JSON format.

Link to comment
Share on other sites

Use PHP to query the DB and create some vars,

Write the vars to a txt file adding in the necessary JSON brackets,

Use PHP's JSON Encode to encode the txt file to JSON

Repeat when ever the database is updated

But...why?  Why not make a PHP page that spits out a json-formatted document from the database every time you call it?  Why all these extra steps?
Link to comment
Share on other sites

Use PHP to query the DB and create some vars,

Write the vars to a txt file adding in the necessary JSON brackets,

Use PHP's JSON Encode to encode the txt file to JSON

Repeat when ever the database is updated

But...why?  Why not make a PHP page that spits out a json-formatted document from the database every time you call it?  Why all these extra steps?

 

Thanks for the response! And to answer your question of why: because I am trying to learn the best way of approaching this. Are you suggesting using the having my JavaScript read directly from the .php file using JSONencode?

Link to comment
Share on other sites

because I am trying to learn the best way of approaching this.

 

Approaching what? You haven't told us what your trying to do, you've simply told us how your planning on doing it, and your how sounds very convoluted and unnecessary.

Link to comment
Share on other sites

because I am trying to learn the best way of approaching this.
As thorpe said, you haven't told us what "this" is.  But I think it's safe to say that you're not doing it right if you're trying to make a flat file on your filesystem, you're not doing it right.

 

Are you suggesting using the having my JavaScript read directly from the .php file using JSONencode?
That's how it's done, yes.
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.