开发者

How to separate database entries and display them as individual records using PHP?

I used to have database entries separated by ampersands (&), but this 开发者_JS百科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?


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


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.


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>";
   }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜