Jump to content

INSERT into database with Foreach?


Mr Chris

Recommended Posts

Hello,

 

I have a function i've written to output the hidden fields that paypal requires to process a payment like so using a foreach

 

 

function Paypal($Database) { 
if($this->NumberOfItems() > 0){

$total = 0; 
$output = '<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"> 
   <input type="hidden" name="cmd" value="_cart"> 
   <input type="hidden" name="upload" value="1"> 
   <input type="hidden" name="business" value="??????????????">
   <input type="hidden" name="currency_code" value="GBP">
'; 
   $i = 1;
   foreach ($this->Cart as $ProductID => $Qty){
                $result = $Database->query('SELECT * FROM shop WHERE id = '.$ProductID); 
                $row = $result->fetch(); 
                $price = sprintf('£%.2f', $row['price']); 
                $priceUnit = sprintf($row['price']); 
                $rowTotal = sprintf('£%.2f', $row['price'] * $Qty); 
                $total += $row['price'] * $Qty; 
                $output .= <<<ROW
                                      <input type="hidden" name="item_name_{$i}" value="{$row['description']}">
                                      <input type="hidden" name="amount_{$i}" value="{$priceUnit}">
                                      <input type="hidden" name="quantity_{$i}" value="{$Qty}">
ROW;
$i++;
            } 
            $total = sprintf('£%.2f', $total); 
            $output .= <<<END
            <input class="paybtn" type="submit" value="Pay"> 
</form> 

END;
            return $output; 
        } 
}

 

Now this works great, but at the same time as running this function and foreach statement to output the hidden fields I'd also like to INSERT the data into a database table row-by row.  Is this possible?

 

The table would just be a simple one like so:

 

items

--------

id (auto inc)

description

amount

quantity

 

But i'm not sure how to include this in the function so it does this?

Link to comment
https://forums.phpfreaks.com/topic/165943-insert-into-database-with-foreach/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.