Reputation: 37
$stringitem2= soap|2|15|30&soap|1|5|10&soap|20|150|300
$array = explode('&', $stringitem2);
// Now explode each on ,
foreach ($array as &$substring) {
$substring = explode('|', $substring);
echo $substring[0];
echo "<br/>";
echo $substring[1];
echo "<br/>";
echo $substring[2];
echo "<br/>";
echo $substring[3];
}
and Here I am exploding one string which is defined in first and substring explodes using foreach loop
My Output is
soap
2
15
30
soap
1
5
10
soap
20
150
300
And I am inserting data to database using below code...
$bill_no = $stringnew[2];
$bill_amount = $stringtotal[1];
$item_name = $substring[0];
$quantity = $substring[1];
$rate = $substring[2];
$amount = $substring[3];
$discount = $stringtotal[0];
try {
$dbh = new PDO('mysql:host=' . DB_SERVER . ';dbname=' . DB_NAME . '', DB_USER, DB_PASS);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
} catch (PDOException $e) {
echo $e->getMessage();
die();
}
try {
$query = $dbh->prepare("INSERT INTO billing (bill_no, bill_amount, item_name, quantity, rate, amount, discount) VALUES (:bill_no, :bill_amount, :item_name, :quantity, :rate, :amount, :discount);");
$query->setFetchMode(PDO::FETCH_ASSOC);
$query->execute(array(
"bill_no" => $bill_no,
"bill_amount" => $bill_amount,
"item_name" => $item_name,
"quantity" => $quantity,
"rate" => $rate,
"amount" => $amount,
"discount" => $discount
));
} catch (PDOException $ex) {
echo $ex->getMessage();
}
When I am sending input its taking Final values only inserts into table...
soap
20
150
300
I want to insert item_name, quantity, rate, amount,
Those four values which is getting loop...
soap ---insert into item_name
2 ---insert into quantity
15 ---insert into rate
30 ---insert into amount
soap ---insert into item_name
1 ---insert into quantity
5 ---insert into rate
10 ---insert into amount
soap ---insert into item_name
20 ---insert into quantity
150 ---insert into rate
300 ---insert into amount
.............................
.............................
.............................
.............................
Loop may get long some times...I want insert the above values as loop into related fields as per explained...by using above mysql insert query its taking only last loop values ...how to insert all values ....
Note : Except these four item_name, quantity, rate, amount,
The remaining things are not not in loop they get one values itself...
Upvotes: 1
Views: 1041
Reputation: 5596
Try this
//Place inside loop
$values[] = "($bill_no, $bill_amount, $item_name, $quantity, $rate, $amount, $discount)";
// place this code after loop
try{
$params = implode(",", array_fill(0, count($values), "?"));
$query = $dbh->prepare("INSERT INTO billing (bill_no, bill_amount, item_name, quantity, rate, amount, discount) VALUES (".$params.")");
$query->setFetchMode(PDO::FETCH_ASSOC);
$query->execute($values);
}
} catch (PDOException $ex) {
echo $ex->getMessage();
}
Upvotes: 1
Reputation: 21437
I have simplified your code within an array from where you can easily implement values using simple foreach
$stringitem2= 'soap|2|15|30&soap|1|5|10&soap|20|150|300';
$string_arr = array('item_name', 'quantity', 'rate', 'amount');
$result = array();
foreach(explode('&',$stringitem2) as $key => $value){
$result[$key] = array_combine($string_arr,explode('|',$value));
}
print_r($result);//Array ( [0] => Array ( [item_name] => soap [quantity] => 2 [rate] => 15 [amount] => 30 ) [1] => Array ( [item_name] => soap [quantity] => 1 [rate] => 5 [amount] => 10 ) [2] => Array ( [item_name] => soap [quantity] => 20 [rate] => 150 [amount] => 300 ) )
Upvotes: 1