When I loop through Rice form variables at php end. everything work well.
<input class="rice" type="text" name="rice[]" >
<input class="beans" type="text" name="beans[]" >
<input class="price" type="text" name="price[]" >
// pdo connection
foreach($_POST['rice'] as $index => $value) {
$statement= $db->prepare('INSERT INTO product(rice)values(:rice)');
$statement->execute(array(':rice' => $value));
}
now how do I loop through beans and price as well and then insert the three products, rice, beans and price into database.
// pdo connection
foreach($_POST['rice'] as $index => $value) {
$statement= $db->prepare('INSERT INTO product(rice,beans,price)values(:rice,:beans,:price)');
$statement->execute(array(':rice' => $value,':beans'=>$value2,':price'=>$value3));
}
You could loop through them like this:
// Make sure we have same amount of everything
$riceTotal = count($_POST['rice']);
if($riceTotal == count($_POST['beans']) && $riceTotal == count($_POST['price']){
foreach($_POST['rice'] as $key => $value){
$statement = $db->prepare('INSERT INTO product(rice, beans, price) VALUES(:rice, :beans, :price)');
$statement->execute(array(':rice' => $value, ':beans' => $_POST['beans'][$key], ':price' => $_POST['price'][$key]));
}
}
Try something like this:
$arrayRBP = array('rice','beans','price');
foreach($arrayRBP as $data) {
foreach($postArray as $value) {
var_dump(${$value}[$data]);
}
}