Grow your CSS skills. Land your dream job.

Best Way to Output Data (PHP+HTML or PHP->JSON+JS+HTML)?

  • # February 24, 2014 at 12:55 pm

    TLDR: -Hi. I’m new. I’m only 16, and not an expert. -Should I loop through SQL result sets and output them with HTML, or store them in a JSON array to output later with JS? -Don’t even MENTION IE

    Hello everybody! My first post here so I hope I can come to closure on this!

    To start off, since it’s my first post, I want to let you all know that I’m no expert.

    I’m no David Walsh, Chris Coyier, or John Resig. I’m just 16, but still interested in this stuff nonetheless.

    I often find myself arguing with and questioning myself on what the best thing to do is, and for the past few days I’ve been wondering what the best way to output data is with PHP.

    Ever since I created sites, they’ve always been small social networks, and output things like forum posts, blog posts, chat messages, and so on.

    I usually just loop through the results of a query and do this:

    <?php
    $result = mysqli_query($db,"SELECT * FROM `chat` ORDER BY `time` ASC");
    while($message = mysqli_fetch_assoc($result)){
        echo "<div class=\"chat-message\">".$message['user'].":".$message['message']."</div>";
    }
    

    ?>

    But for the past week or so, I’ve been asking myself whether or not that is the BEST way to output my data. What I thought was that instead of doing that, I could do something like this (my code wasn’t showing as a “block code”, so here’s plain text):

    $result = mysqli_query($db,”SELECT * FROM chat ORDER BY time ASC”); $json = array(); while($message = mysqli_fetch_assoc($result)){ $json[] = $message; } return json_encode($json);

    To return JSON, and have the DOM create the HTML/CSS to format the raw text data.

    I often like to know how Facebook and Twitter does it, and whether or not I’m wasting my time when clearly there’s a better way to output data from the database.

    Right now, my thoughts are so-so, but JSON looks promising because:

    1. Returning raw data is probably faster in most cases.
    2. Returning raw data allows you to freely manipulate it and format it however you want.
    3. Less load on the server I’m guessing?

    The cons:
    1. It’s SO much easier to just output and format the data in PHP.
    2. I’ve tried it and so far, it seems…yuck. Example (http://ajango.org/.feed/new “Example (View Source for the JS)”)

    Could somebody help me decide this?

    Please note: I’m not developing for IE6-8.

    Thanks so much for your answers! ~Ricky Yoder

    # February 24, 2014 at 3:01 pm

    I think you’re worrying too much about making the page source attractive.

    # February 24, 2014 at 4:54 pm

    It’s not that. I don’t mind whether or not DIVs are all mushed together, rather, which is “faster” or the most optimal.

    # February 24, 2014 at 4:58 pm

    I think you’re worrying too much about making the page source attractive.

    Not sure what page source has to do with this. To me it sound like he’s interested in building some sort of API.

    Best Way to Output Data

    I don’t think there’s a “best” way to display data. It’s all about what you’re trying to do and what tools you choose to employ.

    JSON looks promising because

    Not sure if there are any benefits as far as performance is concerned. The only benefit I see is that you can have various platforms consume your API. Perhaps your web app uses it to display whatever information, then your native app, etc…

    I’m not developing for IE6-8.

    Not sure what this has to do with anything.

    PHP > HTML – less fail points
    PHP > JSON > JS/HTML – more fail points (what if JavaScript fails or is disabled)

    Go to YouTube and search for information about building APIs.

    Hope that helps.

    # February 24, 2014 at 5:04 pm

    @Alen Right!

    As for the IE6-8 thing, I just wanted to make it clear that, if I were to use JavaScript, I would not make it compliant for older browsers.

    Totally forgot to say that I wanted to make an API–I’m guessing I ought to do it through PHP, though.

    This all really helps. Thank you very much! :)

    # February 24, 2014 at 6:04 pm

    Have I misread your post or did you edit it? I didn’t recall reading anything about looking at the javascript specifically.

    # February 25, 2014 at 6:50 am

    @RickyYoder

    I’m guessing I ought to do it through PHP

    That is totally up to you and what you feel most comfortable with. There’s nothing wrong with PHP. With the rise of frameworks like Laravel, it’s really becoming mature language and more than enough for many applications.

    I have Build APIs You Won’t Hate on my reading to do list. You might want to check it out as well. If you’re trying to build enterprise level APIs, it might be of use.

    # February 25, 2014 at 10:55 am

    @chrisburton I was editing it as I re-read it.

    @Alen Well, PHP does seem like the best option, because not all browsers support JSON natively, and it seems icky to include json.js. I looked at API tutorials, however (betterPHP, phpacademy), and they all used jQuery and JSONp, so I feel as though PHP by itself seems a bit left out.

    I’ll definitely buy “Build APIs You Won’t Hate”, because after reading about it, it seems very promising.

    # February 25, 2014 at 3:50 pm

    Here’s a blog post from the author of that book: http://philsturgeon.co.uk/blog/2013/07/building-a-decent-api

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".