www.webdeveloper.com
Results 1 to 5 of 5

Thread: list data, should i format before putting into DB, or after? when outputting to page.

Hybrid View

  1. #1
    Join Date
    Mar 2010
    Posts
    281

    list data, should i format before putting into DB, or after? when outputting to page.

    I just wanted to run this by the group, to get feedback / opinions

    i'm adding some functionality to a shopping cart's product data.
    in the product table i want to add a features list per product. what i'm questioning is this:

    should i put the text into the database cell with just line breaks, and then add the mark-up
    (<ul><li> etc. ) when outputting/formatting for a webpage?,

    OR should i do the mark-up first and then put into the table. if i remember, it's better to not
    to add HTML to the table itself, but to convert to special chars(?).

    mucho thanks for feedback

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,528
    Generally, I avoid any mark-up in the database, as it tends to make the data less usable for any purposes other than output to a HTML web page. Now, if you're sure that's the only purpose that data will serve, then no big deal (e.g. you can't imagine ever wanting to search against that table column and have to figure out how to ignore text inside of any tags). Also, if you ever want to change the mark-up for some reason (SEO tweaking?) you don't have to edit every row in that table.

    As far as data normalization goes, you might want to figure out a scheme were each element would be in a separate row, perhaps with a foreign key to another table that defines where those rows are used. Then you could loop through the result set, e.g.:
    PHP Code:
    $sql "select item from some_table where list_id = $list_id";
    $result $db->query($sql);
    echo 
    "<ul>\n";
    while(
    $row $db->fetch($result)) {
        echo 
    "<li>".$row['item']."</li>\n";
    }
    echo 
    "</ul>\n"
    (If individual list items could be used in more than one list, then you might need a separate relationship table, adding a JOIN to the above sample query.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2010
    Posts
    281
    thanks a lot for the reply.

    according to the client, there is real straight ahead stuff, with no need for an additional table.

    is there any reasons not use line breaks, '/n' ??

  4. #4
    Join Date
    Jan 2009
    Posts
    3,346
    Is the client a software designer or developer? Foreign keys and additional tables provide a great flexibility when the needs of a data application change (such as SEO as mentioned above). You can use line breaks in a single column, but you will then have to make large modifications to the data that is stored or how it is stored if you ever need to make adjustments to single items that might span multiple lists (such as if a copyright issue were to arise for a particular description given to multiple products).

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,528
    ASCII newlines probably are no problem, and can easily be replaced with <br> tags or </p></p> tags -- whatever makes sense for your output -- via things like preg_replace() or nl2br(). It's also good to filter the output with htmlspecialchars() or htmlentities() (before you add any markup to it).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles