/    Sign up×
Community /Pin to ProfileBookmark

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

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 22.2012 — 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.:
[code=php]
$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";
[/code]

(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.)
Copy linkTweet thisAlerts:
@toptomatoauthorDec 22.2012 — 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' ??
Copy linkTweet thisAlerts:
@criterion9Dec 24.2012 — 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).
Copy linkTweet thisAlerts:
@NogDogDec 24.2012 — 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).
×

Success!

Help @toptomato spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...