2

I am building a webpage for learning. Actually doing the page is the main goal, if it works well it would only be a bonus since i will most likely be the only person using it.

That being said i am using Angular Objects that hold a lot of informations, like:

Semester - Subcategory - Question - List of answers as objects with "true"/ "false" properties for multi choice and the answer itself ect.

Since i will be doing the whole sorting / filtering with angular i wonder if i really need SQL or if a XML file would be superior.

With SQL saving is my main issue here. PHP seems to butcher arrays into a string with the value "array". If i use json_encode it saves correctly, but on GET it stops working since i have to rebuild the whole data structure with " and ' about everywhere.

With XML it really looks like angular just is not build for that. I have found some outdated tutorials that did not even have a working example.

So i guess my question here is: Do i either go for SQL, putting up with multiple tables. Splitting my objects into several columns with optional values all over the place, while also rebuilding the whole thing on load?

Or do i use XML, since i would only use the DB to GET the whole thing anyways?

Both approaches have been tested by me and work, somewhat. Both would need quite a lot of further digging, reading and trying. I don't have the spare time to do both routes. Which one is the better one to go for in this particular use case?

Marco Heumann
  • 121
  • 1
  • 10
  • Will you be writing to your data source at all? I personally would never go with XML for a task like this (it could be my innate predisposition to dislike XML). If you are going to write I would say SQL. Otherwise, it really depends on your needs. You might get a lot of biased answers here (like mine). That's why questions recommending technologies are considered off-topic. – Hanlet Escaño Feb 11 '16 at 15:11
  • Well, hard to get any feedback without asking, even if it is considered offtopic. After all: If it was an easy choice i would not have to ask in the first place. And as a beginner to databases and storing data i have to start somewhere. Also: Yes i will be saving new data regulary. – Marco Heumann Feb 11 '16 at 15:15
  • You haven't mentioned `JSON`, so you may want to take a look at it. If you just wantto store the data for the user to recieve, it will be suficcient. If you want to be serious, so evaluating the answers, i would recommend using a webservice and getting the data from it (so it will get the data from a database or file). – AntiHeadshot Feb 11 '16 at 15:15
  • @Rattenmann the quote on this answer might be helpful: http://stackoverflow.com/a/201594/752527 - actually, the entire thread is really good. – Hanlet Escaño Feb 11 '16 at 15:18
  • @AntiHeadshot Actually that might already be the answer. I really did not think about just storing my data in a .json file. I will look into that as it would remove all the coverting / rebuilding issues. Thanks! – Marco Heumann Feb 11 '16 at 15:29
  • @HanletEscaño it is true, but for a prototype/learning project it may be overshooting to use a sql database, if you are new to all that. Using a simpler alternative still will leave him the opportunity to change it to a database later, if he wishes to. – AntiHeadshot Feb 11 '16 at 15:42

2 Answers2

2

This is ofcourse a personal preference but I always try to avoid XML. The JSON format is alot lean and meaner and it's way easier to work with in web applications.

In fact I would suggest to start with some static JSON files until you're finished with giving your website some structure. You can generate them manually, use some generator tools (like http://www.mockaroo.com/) or build them by using some simple javascript (JSON.stringify is your friend). You can then use this data quite easily by using the $http service:

$http.get('my-data.json')
  .then(function(response) {
    $scope.myData = response.data;
  });

This is actually the approach my teams take when building large enterprise applications. We mock all data resources and replace them with the real thing when we (or the customer) are happy with the progress.

null
  • 7,906
  • 3
  • 36
  • 37
  • Exactly what i wanted. Plain save functionality without the hassle of rebuilding all objects after pulling them out of a SQL database. This even removes the step needed to store in XML, witch makes this approach golden for my usecase. Thanks for the links and Angular example code as well! – Marco Heumann Feb 11 '16 at 17:17
1

Using a JSON-File should be sufficient. You can store all the needed objects in it and change it easily. With the following code you can load the data within JavaScript

function loadJSON(path, success, error) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === XMLHttpRequest.DONE) {
            if (xhr.status === 200) {
                if (success)
                    success(JSONH.parse(xhr.responseText));
            } else {
                if (error)
                    error(xhr);
            }
        }
    };
    xhr.open("GET", path, true);
    xhr.send();
}

usage

loadJSON('data.json',//relative path
    function (data) {//success function
        $scope.questions = question;
        $scope.$apply();
    },
    function (xhr) {//error function
        console.error(xhr);
    }
);
AntiHeadshot
  • 1,130
  • 9
  • 24
  • Thanks for pointing out that i missed this very simple and basic approach (yet perfectly fitting for my use-case). Only picked Nulls answer due to the explaination and for the sleek code snipped without the need for a foreach and push. Seriously, saved my day from wasting my time on researching the wrong direction. – Marco Heumann Feb 11 '16 at 17:24
  • @Rattenmann the foreach was useless, i just needed it for my implementation. The main difference between mine and the one from null is that his one is the Angular way of doing it, and mine is plain JavaScript. – AntiHeadshot Feb 12 '16 at 07:16