0

Hi I am looking for someone to answer what is probably an easy question.

At the moment I have two tables "procedures" and "Job Roles", the idea is that I want to be able to create a child table that links which procedures apply to which job roles. At the moment this is done with two tables, by creating a record in the procedure table and then creating subsequent child records in a table called "procedures_jobtitles" which is just a one to many link table.

I am exploring the idea of having a field in the procedures table that is called "applies to", which would be a drop down box presented as check boxes, the idea is that when i create a procedure, on the add/edit page there is a separate tab where this field is displayed, this would display all job roles, so that the user could just tick the ones that apply.

My problem is by doing it this way the field value is stored as (1,2,5,10,11,12) where those numbers represent foreign keys to the job descriptions table. What I would like is to take the "applies to" field and turn it into an array and extract each value that is separated by a comma, so for the above example it would give me a result of:

1
2
5
10
11
12

I can then run a foreach query and insert these records into the link table by an event after saving. The idea behind this is if you have 1 or 2 job descriptions, manually adding to the child table isnt a problem, however when you have 20 or so job titles it becomes much more time consuming. Ive tried hitting google for this, but im afraid my basic understanding of arrays is limited so I dont know what to look for.

Any help using example fields ive given, would be much appreciated

Alex K.
  • 171,639
  • 30
  • 264
  • 288
user5641678
  • 35
  • 1
  • 8
  • 2
    To split a string by a delimiter, use the http://php.net/explode function. `explode(',', "1,2,3,4")` should give you the array you are looking for. – bspellmeyer Apr 12 '16 at 14:20
  • 1
    Duplicated question: http://stackoverflow.com/questions/1125730/how-can-i-split-a-comma-delimited-string-into-an-array-in-php ( first result on google ) – Dupuis David Apr 12 '16 at 14:22
  • Its a bit unclear but if you are considering a database field with comma separated values then simply don't, change the design such that the values are on their own individual rows. This is the correct approach and eliminates a multitude of problems. – Alex K. Apr 12 '16 at 14:22
  • Looking at other answers, i think Implode might be where i am heading? – user5641678 Apr 12 '16 at 14:23
  • Thanks, I know this is the correct approach, the problem is I am using some software that builds my application and this is how their application stores the value, which is why i am trying to correct it with this approach – user5641678 Apr 12 '16 at 14:25

1 Answers1

1

Use explode()

e.g.

<?php
$mystring = "1,2,5,10,11,12";
$myarray = explode(",",$mystring);

//Output each element from $myarray:
foreach($myarray as $item)
{
    echo $item."<br>";
    //or do something else, e.g. insert these records into the link table
}
Black
  • 18,150
  • 39
  • 158
  • 271