How to display multiple data that store in one fields?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rolyestemonio
Forum Newbie
Posts: 19
Joined: Fri Jun 18, 2010 10:30 pm
Location: Metro Manila - Paranaque City
Contact:

How to display multiple data that store in one fields?

Post by rolyestemonio »

Hi guys i have something problem regarding displaying multiple data that store in one fields.
Is it possible to combine all the data in one fields and it will display separately?

Example:

I have 3 fields (id, description, link) and i store this in the ff. withe a separator "|":

description fields

This is a description1 | This is a description2 | This is a description3 | This is a description4

link fields

Link1 | Link2 | Link3 | Link4

And it will display like this :

1. This is a description1
Link1

2. This is a description2
Link2

3. This is a description3
Link3

4. This is a description4
Link 4

Thanks in advance to your help guys.
dheeraja
Forum Commoner
Posts: 36
Joined: Tue Nov 09, 2010 11:03 pm

Re: How to display multiple data that store in one fields?

Post by dheeraja »

Yes you can display data like that, you just have to use explode() like

Code: Select all

<?php
$result = explode("|", $row['description']);    //Variable name used by you.
?>
For more information refer below link..

http://php.net/manual/en/function.explode.php
http://www.w3schools.com/PHP/func_string_explode.asp
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How to display multiple data that store in one fields?

Post by califdon »

While it is true that you can split (explode) a string into components, and you can separate data by regex expressions, but you should know that storing data this way violates the relational model that is the foundation of SQL. This means that if you are designing the database to begin with, you should avoid storing data in that way. It is called "repeating groups" and violates the second "normal form" defined by Dr. C. F. Codd, the IBM mathematician who developed the concept of relational databases in about 1970. When a database doesn't conform to the relational model, you will have difficulty using queries to retrieve data and may be unable to perform certain kinds of database queries at all. If you are stuck with data stored in this way, you may have no alternative than to split the strings, but forget about searches and so forth.
rolyestemonio
Forum Newbie
Posts: 19
Joined: Fri Jun 18, 2010 10:30 pm
Location: Metro Manila - Paranaque City
Contact:

Re: How to display multiple data that store in one fields?

Post by rolyestemonio »

Thanks. It really works.
Post Reply