I want to use foreach in my following code only one time.and i need to paas two array value that is store_data and store_control_text to my database respective fields.Need suggestions for not using foreach loop for multiple array values..
$data = $_REQUEST['columns_one'];
$controlText = $_REQUEST['controlText'];
$store_control_text = explode(",",$controlText);
$store_data = explode(",",$data);
$query = "INSERT INTO formdetailstemp(FormId,ControlType,ControlText,ControlPara1,Mandatory) VALUES ";
$values = array(); //store all the new rows
foreach($store_data as $key =>$value){
$values[] = "('','".$value."','".$controlText."','".$controlPara."','".$mandatoryValue."')";
}
I believe you're looking for a MultipleIterator
, which will iterate over both arrays easily with a single foreach
loop:
$iter = new MultipleIterator;
$iter->attachIterator( new ArrayIterator( $store_control_text));
$iter->attachIterator( new ArrayIterator( $store_data));
foreach( $iter as $data) {
list( $a, $b) = $data;
var_dump( $a, $b);
// Do your SQL insert with $a and $b
}
You can use array_map
foreach ( array_map(null, $store_control_text, $store_data) as $join ) {
list($text, $data) = $join;
//Do your stuff
}
you can use .implode();
foreach ($_POST['description'] as $row=>$name){
$description = $name;
$supplier = $_POST['supplier']; //Normal textbox value
$quantity = $_POST['quantity'][$row]; //Array text box 1
$partnumber = $_POST['partnumber'][$row]; // Array text box 2
$unitcost = $_POST['unitcost'][$row]; // Array text box 3
$sql_array[] = '("NULL","' . $partnumber . '","' . $supplier . '","'.$description.'", "'.$quantity.'","'.$unitcost.'")';
}
$query_single = 'INSERT INTO `inwards` (`id`, `partnumber`, `suppliers`, `description`, `quantity`,`unitcost`) VALUES '. implode(', ', $sql_array);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.