Forum Moderators: coopster

Message Too Old, No Replies

php array if to seperate fields?

php array if to seperate fields?

         

Gruessle

4:06 am on Mar 13, 2005 (gmt 0)

10+ Year Member



How do i insert a php array if to seperate fields?

Meaning I have following mysql table:
-------------------------------
reg_id ¦ US_States ¦ progr_id ¦
-------------------------------

and I have a array from multi select box.
How do I get the to look loke this:

-------------------------------
reg_id ¦ US_States ¦ progr_id ¦
-------------------------------
1 ¦ CA ¦ 1 ¦
2 ¦ FL ¦ 1 ¦
3 ¦ NY ¦ 1 ¦

and not like this
-------------------------------
reg_id ¦ US_States ¦ progr_id ¦
-------------------------------
1 ¦ CA,FL,NY ¦ 1 ¦

Gruessle

4:37 am on Mar 13, 2005 (gmt 0)

10+ Year Member



Bad spelling sorry.

Is this what I have to do:

$r = 1;
foreach ($states as $state)
{

INSERT INTO region (US_States,progr_id)
VALUES ('$state','$progr_id')

$r++;
}

Gruessle

4:40 am on Mar 13, 2005 (gmt 0)

10+ Year Member



Well then how do I do an update?
Lets say we have 3 states for one progr and that changes to two states?

Must I first delete all states
WHERE progr_id=progr_id
and then insert new?

hmmm

jatar_k

10:03 pm on Mar 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I wouldn't remove them all

1. you need to select the ones previously entered
2. compare with the new states
3. remove any that no longer apply
4. insert any new ones

gettopreacherman

9:20 pm on Mar 15, 2005 (gmt 0)

10+ Year Member



$array = array(CA,FL,NY);

$r = 0;
foreach ($states as $state)
{

INSERT INTO region (US_States,progr_id)
VALUES ('$array[$r]','$progr_id')

$r++;
}

That's the way I would start at it, may need some tweaking, but I would just call the array until it's done.