mysql - Loop through an array to unset a known value and implode to sql -
i have 2 set of arrays in different mysql table. want do
want table_one connect table. value want session_id array associated value (session_id) explode array individual values. now::::: - go table_two table_two go straight first value array (table_one) explode array associated it. delete number that's equal session_id _____________________________________________________ , fort....
more visual explanation below: session_id = 4
table_one:
id array1 1 4 2 1 3 2,5 4 1,3,4,5 5 4,5
table_two:
id array2 1 4,6,9,2 2 3,7,8,2 3 7,12,4,9 4 1,5,4,8 5 3,6,12,3,5,4
so, because session_id = 4
, go table_one id 4
. array of id-4 1,3,4,5. know 4 can found in id 1,3,4,5 of table_two
should explode table_two's array , delete 4
there array. implode array , save new value database.
this have done - delete '4' them id-3 , delete values in id-4. please help!!
$session = 4; $depsql = mysql_query("select array1 table_one id='$session' limit 1"); while($row=mysql_fetch_array($depsql)) { $deparray = $row["array1"]; } $explodeddep = explode(",", $deparray); foreach ($explodeddep $key1 => $value1) { $stsql = mysql_query("select array2 table_two id='$value1'"); while($get=mysql_fetch_array($stsql)) { $starray = $get["array2"];} $explodedst = explode(",", $starray); foreach ($explodedst $key2 => $value2) { if ($value2 == $session) { unset($explodedst[$key2]); } } $newst = implode(",", $explodedst); $sql = mysql_query("update table_two set array2 ='$newst' id='$value2'"); } exit();
please help!!! i'm struggling on it. have tried hours , havent got where. think problem inserting database. please help.
you can avoid 1 loop using array1 in second sql directly since comma seperated list.
try this: edit: updated code after testing.
$session['id'] = 4; $depsql = mysql_query("select array1 table1 id='".$session['id']."' limit 1"); while($row=mysql_fetch_array($depsql)) { $deparray = $row["array1"]; } $stsql = mysql_query("select id, array2 table2 id in ($deparray)") or die("query error"); while($get=mysql_fetch_array($stsql)) { $starray = $get["array2"]; $id = $get["id"]; $explodedst = explode(",", $starray); foreach ($explodedst $key2 => $value2) { if ($value2 == $session['id']) { unset($explodedst[$key2]); } } $newst = implode(",", $explodedst); echo $id . " " . $newst . "<br/>" ; $sql = mysql_query("update table2 set array2 ='$newst' id='$id'"); } exit();
Comments
Post a Comment