0

I am trying to store a bunch of features in a database. The format I was thinking was to insert it like this:

Stuff Here|Separated By That|More Stuff|And some more

Now, I will need to grab that from the database and make it into a list like:

<ul>
    <li>Stuff Here</li>
    <li>Separated By That</li>
    <li>More Stuff</li>
    <li>And some more</li>
</ul>

Will that be easy to do? I know I can do:

str_replace("|", "<li>", $data);

but then that wouldn't work for the very first item and the last item, plus closing tags. Is there anoher way I should be inserting my list into the database? Or is this way fine, and I just need the correct code to format the unordered list better?

1
  • It's generally a bad idea to store data in that format. You want each item to have it's own row in the database. Look up "normalization" for more info about this idea. Commented Sep 20, 2011 at 13:38

5 Answers 5

5

I don't think there's a single-line piece of code you can use to do that (would love to be corrected), so yes, store your data as you currently are in the database and write a small helper function to split up the string and build the HTML list.

function build_list($string) {
  $output = '<ul>';
  $items = explode('|', $string);
  foreach ($items as $item) {
    $output .= '<li>' . $item . '</li>';
  }  
  $output .= '</ul>';

  return $output;
}
Sign up to request clarification or add additional context in comments.

8 Comments

I agree with Clive, I don't believe there will be a single line bit of code - simply because you have to include the first and last item as you said. Clive has got the right idea IMO. A small helper function would be ideal here.
This is the best approach in my opinion. You'll inevitably want to add attributes to the li tags for the currently selected item, if it's for navigation or something similar, and the 'clever' str_replace solutions are no longer useful.
perfect! thanks. except I get a blank bullet at the very end.
Do you have an extra pipe delimiter at the end of the string by any chance?
Is there a way to exclude that last one?
|
2

If you want to keep the simplistic storage approach, you could inject opening and closing tags at the same time:

 str_replace("|", "</li><li>", "<li>$data</li>");

You should also test then if there is a at least one $data entry.

Comments

2

Using "separated by something" approach is almost always a bad way of doing it. Normalize your databse so that you have row per feature, something like

table features(
  id PK,
  Name
)

and everything becomes much easier to query / maintain...

Comments

2

I am using a VIN decoder and once it decodes a VIN it gives me a huge list of features for different categories like Exterior, Interior, Safety, Mechanical. It generates it for me, but users will have the option to delete and edit them. It could get extremely messy if I make it insert into its own table if it doesn't exist and then trying to match it with the vehicle.

This is situation where a relational database works very well. You'll want to create several tables to handle this.

  • You have a one to many relation between VIN and features.

This suggests you'll have a table that stores the different possible features.

table features (
    featureID,
    category,
    feature
)

You could then have a table that associates VIN number with features:

table vin_features (
    VIN,
    featureID
)

You may additionally have a table with information specific to each VIN (maybe make, model, year?) and perhaps a table for users options.

This makes your data management and querying much easier in the long run, especially if you start having to answer more complicated questions.

2 Comments

So when a new VIN is inserted, automatically insert into the features table if it does not exist. Then, insert the default features for the vehicle from the vin decoder into the vin_features table. Then if one of them is updated custom, I would need to add that into the features table?
what if someone enters a bogus feature that no one else will ever type in, does it still need to be stored in the features table?
1

use explode() to split your string in an array. Then, you can use a foreach loop to generate your list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.