biggieuk Posted May 25, 2008 Share Posted May 25, 2008 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 Quote Link to comment Share on other sites More sharing options...
LooieENG Posted May 25, 2008 Share Posted May 25, 2008 Store them as "t1,t4,t6,t7" and then when you retrieve the data, <?php $sess = explode(',', $row['sessions']); ?> And then <?php echo $sess[0]; ?> Would return "t1" Quote Link to comment Share on other sites More sharing options...
deadonarrival Posted May 25, 2008 Share Posted May 25, 2008 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 Quote Link to comment Share on other sites More sharing options...
biggieuk Posted May 27, 2008 Author Share Posted May 27, 2008 thanks for your help. The explode method works great. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.