Jump to content

store values seperated by commas


biggieuk

Recommended Posts

Hi all,

 

I have a dynamic box on the admin section of my website where the admin can set the number of selectable sessions from the booking form.

 

I do not want to have to add/remove columns in my 'sessions' table to reflect this number so im wondering if there is a way to store values in a database separated by commas and read them back individually?

 

This would allow me to store values one after another in a single field, e.g. t1,t4,t6,t7 . If there is a better method i could use please share your thoughts.

 

thanks

Link to comment
Share on other sites

Put simply, yes

 

Store the values as val1,val2,val3

 

then retrieve the field as $field

$values = explode(",",$field);

 

In your example

t1,t4,t6,t7

would become

$values[0] = "t1";

$values[1] = "t4";

$values[2] = "t6";

$values[3] = "t7";

 

An alternative (probably preferable) would be to make a seperate table with the values and you can just read them straight  out of that table.

 

So you might end up with a table that looks something like this

tvalues

+----------+---------+

|userid      |value      |

+----------+---------+

|1            |t1          |

|1            |t4          |

|1            |t6          |

|1            |t7          |

|2            |t1          |

|2            |t4          |

|3            |t9          |

|3            |t3          |

+----------+---------+

 

And you can just use

SELECT * FROM `tvalues` WHERE `userid`='$userid'

to get the values for that user.

 

If it's only one for the whole site, you can just take out the userid completely, and remove the where clause from the sql

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.