1
 $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...

2
  • you are overwriting the $substring upon each explode. So at the end you will have only last exploded set. Thats why you are inserting only the final set. As soon as, you explode one set soap|2|15|30, insert into DB Commented May 19, 2015 at 7:34
  • But I will get loop values...same like that string in example.. Commented May 19, 2015 at 7:36

2 Answers 2

1

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 ) )
Sign up to request clarification or add additional context in comments.

5 Comments

that's good.But how to insert values as loop in my database now..as per your code...
C'mon !!! Since you have 3 sets. the $result array will have 3 sets. Iterate thru $result and make insertion into the DB 3 times. Put your insertion logic into the for loop.
can you please tell me insertion logic using for loop
@krishna from where all these values are coming from $bill_no,$bill_amount,$discount. All these values are static or dynamic
All values are dynamic which are sending from once machine , here we are using tcp/ip communication ...not problem with $bill_no,$bill_amount,$discount those are inserting well now i want insert loop values into related fields
1

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.