tags:

views:

41

answers:

3

I used to have database entries separated by ampersands (&), but this was causing certain search issues so I decided to encapsulate my entries on both sides by $ and & symbols like:

$this&

But I am having trouble displaying all the entries of a cell as individual records. Before I used:

$unsplitItems = $row['files'];

$files = explode("@", $unsplitItems);

foreach ($files as $file) {

   if(strlen($file)) {

      echo "<li>$file</li>";

   }

}

Any idea how I can split my records and display all the items in the array as individual entries?

A: 

You shouldn't do that! Make your records separate db entries.

Make another db table with 2 fields - id of parent record and your entry field. So you will be able to search using database features, with single query

Col. Shrapnel
I am making a tagging feature where a post may be assigned one tag, a hundred tags or none at all. So this is the easiest route for me, I haven't delved into mysql beyond the basics so I wouldn't know how to associate entries between two separate tables.
Clark
If you're going to implement a "search all entries having tag xyz" feature the database would have to do a full table scan with this approach. Take a look at http://en.wikipedia.org/wiki/Database_normalization and http://www.w3schools.com/Sql/sql_join.asp
VolkerK
@ VolkerK I have been hearing of "Joining" tables a lot. Looks awesome will look into it.
Clark
A: 

If you really want to do this then use one symbol as a separator and make sure that this symbol is not used in the strings it separates.

Normally, though, you would create 3 tables:

post (id, post_text, ...)   
tag (id, tag_name, ...)
post2tag (post_id, tag_id)

The last table lets you assign as many tags to any post as you want.

This SQL relationship is called many-to-many; you can see examples of its use here.

z-boss
A: 

Try this:

$unsplitItems = $row['files'];

$files = explode("@", $unsplitItems);

foreach ($files as $file) {

   if(empty($file)) continue;

   $sub_record = explode('&', $file);

   foreach($sub_record as $sr) {
      if(empty($sr)) continue;
      $sr = substr($sr, 1);
      echo "<li>$sr</li>";
   }

}
mattbasta