I'm quite new to programming in general, so I expect this question to have an easy solution. I searched for an answer before posting, but I'm not really sure what I'm looking for.
Basically, I have a database with the following structure:
Table: things
Column 1- ID
Column 2- Name
Column 3- Year
Column 4- Timestamps
I have a large collection of timestamps for many things of the format hh:mm:ss that I want to store in the 4th column of the things table. Each item in the table will have a varying number of timestamps associated with it, so I thought it would make sense to simply store them all in a single column separated by commas (hh:mm:ss,hh:mm:ss) rather than store each timestamp in its own column. From here, I hoped that using PHP I could select the name of a thing and recall its year and timestamps, separating each timestamp into its own variable.
Example:
Column 2- Thing20
Column 3- 1997
Column 4- 00:01:24,00:05:28,00:16:52
$name = "Thing20"
$year = 1997
$ts1 = "00:01:24"
$ts2 = "00:05:28"
$ts3 = "00:16:52"
Here are my questions...
Any help at all would be greatly appreciated. As I said above, I am a beginner so I'm very sorry if this is a trivial thing to ask!
Thank you!
Design issue
If you need n values for each ID, then instead of storing it in a single column you should split it into two tables.
Table 1: things (ID, name, year), where ID is a primary key
Table 2: timestamps (ID, timestamp), where ID is a foreign key from 'things' and where you can store as many ID,timestamp pairs as you wish (and timestamp is a single value)
So in your example, table 1 will look like this:
ID, Name, Year
1, Thing20, 1997
Table 2 will look like this:
ID, timestamp
1, 00:01:24
1, 00:05:28
1, 00:16:52
Practical solution
However, if changing DB structure is not a case, then you can just use:
$ts = explode(',', $timestamp);
and you will receive an array of all timestamps in $ts variable
I won't try to answer as to the merits of your approach. On a practical level, you can split your string into an array using the explode:
// where $col4 = "00:01:24,00:05:28,00:16:52"
$tsarray=explode(',',$col4);
// $tsarray = array("00:01:24","00:05:28","00:16:52");
The ways are many and varied for then allocating the array elements to individual variables. Cheers.