I have small problem here :)
Anyone can help me, what I should do if i want to use this foreach statement with 2 values, because I need to insert in one table at the same time. Because if I used this code, it will be error. So && is not properly used in this case?
$hasilmsisdn=explode(";", $msisdn);
$hasilaccid=explode(";", $accid);
foreach ($hasilmsisdn as $value "&&" $hasilaccid as $value2)
{
$sql = "INSERT INTO complaint_detail (MSISDN,ACCID) VALUES ('$value','$value2');";
$run=mysql_query($sql);
}
Thanks all, hope you help me :)
Answer:
Use
array_combine()
to combine both arrays as key/value pairs:$hasilmsisdn=explode(";", $msisdn);
$hasilaccid=explode(";", $accid);
$array = array_combine($hasilmsisdn,$hasilaccid);
foreach ($array as $msisdn => $accid){
$sql = "INSERT INTO complaint_detail (MSISDN,ACCID) VALUES ('$msisdn','$accid');";
$run=mysql_query($sql);
}
Answer:
If both the arrays are having the same size you can use a for loop like this:
for($i = 0, $i < count($hasilmsisdn); $i++) {
$sql = "INSERT INTO complaint_detail (MSISDN, ACCID) VALUES ('$hasilmsisdn[$i]', '$hasilaccid[$i]');";
$run = mysql_query($sql);
}
maybe something like this
foreach ($hasilmsisdn as $value)
{
foreach ($hasilaccid as $value2)
{
...
}
}
No comments:
Post a Comment