views:

265

answers:

4

Great site! I have a question I haven't seen answered.

I am very new to this, I didn't know what JSON was yesterday, so I apologize if this is a stupid question. A client has requested an JSON/XML type scheme to store data because they want a flat file to edit/update without having to deal with queries, and they want it flexible for future development. I would like the data to be structured, as I'll use it and php to build html pages, two parts of the site will use it, both on the same server.

The json file will contain an array of objects, probably eventually 50, that consist of 5 or 6 short tag/data combos, and 1-3 tag/data combos that will be long, a couple paragraphs of text each.

Details: server side implementation only, everything will reside on the same server. At this time, I will manually build the json file, in the future it may be automatically generated. I prefer json to xml as it seems to be easier to manipulate with php. I have php 5.2.6 and json 1.2.1.

My questions are:

  1. Is it appropriate to use json for data that's textarea size? Will it cause performance issues at build-out when I have 30-50 objects in my array, and I need to find the right one?

  2. Is it appropriate to use json as a data repository like this?

  3. If so, I'm having trouble finding instructions on the very basics, like how to access the file. Do I have to use XMLHttpRequest even on the same server? Any informational links to help better understand this would be greatly appreciated.

  4. If this is not a good usage, any tips on what might be a good choice would be greatly appreciated!

Thanks!

A: 

If they really want it to be flexible, they should throw their data in a database. That's a far more reliable approach than a flat file. Building a CRUD (create/update/delete) web interface for basic data is dirt simple.

Answers:

  1. using jQuery and the like , it's pretty easy to search an array of JSON objects or an XML tree.

  2. No. Use MySQL, SQL server, anything anything anything but a flat file.

  3. File IO in PHP: http://www.developertutorials.com/tutorials/php/file-access-php-050623/page1.html

  4. Use a database. No modern web application stores data in a flat file.

N. If they insist on having a flat file version of their data, provide a page on the site that will generate an XML or JSON version of what's in the database. That should make them happy, but keep the data in a place that makes sense for the application to use.

David Lively
+2  A: 
  1. Probably not.
  2. No. Please use a database, it will save you a lot of headaches in the future.
  3. If you must do this, just use a PHP library to access the json file.
  4. Use MySql or equivalent. It sounds like you need to dynamically generate page data. That is exactly what MySql was built for (among other things).
EToreo
A: 

JSON sounds like a decent solution to what you're doing, given the client restriction requiring a flat file. If you use PHP's built-in jsonencode() and jsondecode() functions, you shouldn't have any trouble with paragraph-length data.

You won't have the same query ability that you'd have with a relational database like MySQL, but if you only need so search and modify within a single file at a time, that would be pretty easy to do in PHP after using jsondecode() and some of PHP's array functions. To search through many files at once would be fairly challenging though, and I'd recommend a searchable database rather than flat files for something like that.

To open files in PHP, check out the functions file_get_contents() and file_put_contents().

http://us.php.net/manual/en/function.file-get-contents.php

http://us.php.net/manual/en/function.file-put-contents.php

All that said, unless you really need to use a flat file, the other posters are correct that a database would be a much more versatile approach. JSON and XML are used much more for transporting data than storing data these days. MySQL has a number of pre-built interfaces (like PHPMyAdmin) that allow you to search and modify the data without using SQL queries.

Travis
so you do something like:$file = file-get-contents($handle);$obj = json-decode($file);?
Rebecca
Correct. Although the function names are file_get_contents() and json_decode().
Travis
A: 

I am shocked reading stuff like "No modern web application stores data in a flat file."

Are you people aware that Google and Twitter use flat-file systems? Geez... grow up from the database era. Unless you're sorting, searching and combining thousands of data-blocks, mysql does not really make sense. Furthermore, flatfiles are usually the easiest and most portable (not only for backup reasons).

I would indeed dive into "file reading and writing". This will also widen your understanding of core php functionalities (and your results will most probably be faster too).

e-sushi