Sasha
Sasha

Reputation: 8705

Codeigniter 2.1, PHP - foreach post

I have following problem:

I have input field name and input field phone and they are bound together (for example Home - 555-5555-555, Hideout - 333-333-3 ), and user can insert as many phone pairs as he wants. For the moment (this is old DB version which needs to be upgraded) user have one row for inserting data. Phone field is made in the following way;

333-333-333,55-555-555.... and when it is needed to show phone numbers this is exploded. I would like to keep this and make phone pair in the following way Home_55555-55,Hideout_333-33-33....

How can I do this and is this a good way to do this or I should do it the classical way (one row per phone pair)?

Upvotes: 0

Views: 98

Answers (1)

giorgio
giorgio

Reputation: 10212

Nope, this is not the way to do it, you really want a row per phone number, furthermore, you'd want a separate field (column) for the phone type (eg. Home or Hideout). The database table should look something like this:

+----+----------------+--------------+
| id | phone_location | phone_number |
+----+----------------+--------------+
| 1  | Home           | 333-3333-333 |
| 2  | Hideout        | 555-5555-555 |
  ...

You might even want to consider a separate table for the locations (in case the 'location'-name are some sort of fixed, ie. users cannot choose the names randomly). You would create a table like this:

+----+----------------+
| id | phone_loaction |
+----+----------------+
| 1  | Home           |
| 2  | Hideout        |
  ...

And your original table would look like this:

| id | phone_location | phone_number |
+----+----------------+--------------+
| 1  | 1              | 333-3333-333 |
| 2  | 2              | 555-5555-555 |
  ...

Ofcourse this doesn't consider different users, so you would add an extra column to the phone_number table with a user_id column, which points to the users table

Upvotes: 2

Related Questions