Jump to content

array question


ballhogjoni

Recommended Posts

how can I make this work? I have posted the whole function, but I want to run this code

 

echo '
	<input type="hidden" name="item_name_'.$row.'" value="'.$title.'"/>
	<input type="hidden" name="item_description_'.$row.'" value="A completly handmade diaper cake with elegant decorations"/>
	<input type="hidden" name="item_quantity_'.$row.'" value="'.$qty.'"/>
	<input type="hidden" name="item_price_'.$row.'" value="'.$price.'"/>';

 

for each item that is in my cart. Needless to say I can't figure it out. Somebody help please!

 

function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
	$items = explode(',',$cart);
	$contents = array();
	foreach ($items as $item) {
		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
	}
	$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
	$output[] = '<table width="500px" style="border:1px solid #EEEEEE;">';
	$output[] = '<tr>';
	$output[] = '<td>';
	$output[] = '<table align="center">';
	$output[] = '<tr>';
		$output[] = '<td align="center" class="style1">Item</td>';
		$output[] = '<td align="center" class="style1">Qty</td>';
		$output[] = '<td align="center" class="style1">Price</td>';
		$output[] = '<td align="center" class="style1">Sub Total</td>';
		$output[] = '</tr>';
	foreach ($contents as $id=>$qty) {
		$sql = 'SELECT * FROM products WHERE id = '.$id;
		$result = $db->query($sql);
		$row = $result->fetch();
		extract($row);
		$output[] = '<tr>';
		$output[] = '<td class="style4" align="center">'.$title.'</td>';
		$output[] = '<td align="center"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
		$output[] = '<td class="style4" align="center">$'.$price.'</td>';
		$output[] = '<td class="style4" align="center">$'.($price * $qty).'</td>';
		$output[] = '<td align="center"><a href="cart.php?action=delete&id='.$id.'" class="r"><img src="/images/delete.jpg" alt="delete the diaper cake product" border="0" width="22" height="25"></a></td>';
		$total += $price * $qty;
		$output[] = '</tr>';
	}
	$output[] = '</table>';
	$output[] = '<table align="center" width="500px">';
	$output[] = '<tr>';
	$output[] = '<td>';
	$output[] = '</td>';
	$output[] = '<td align="right">';
	$output[] = '<button type="submit">Update Qty</button>';
	$output[] = '</td>';
	$output[] = '</tr>';
	$output[] = '<tr>';
	$output[] = '<td>';
	$output[] = '</td>';
	$output[] = '<td align="right" style="font-size:large">';
	$output[] = 'Total:';
	$output[] = '</td>';
	$output[] = '<td style="font-size:medium; font-weight:bold; text-align:center;">$'.$total;
	$output[] = '</td>';
	$output[] = '</tr>';
	$output[] = '</table>';
	$output[] = '</form>';

	echo '<form method="POST" action="https://checkout.google.com/cws/v2/Merchant/123456789/checkoutForm"
   accept-charset="utf-8">';

$num = count($items);

//for ($i=1; $i<=$num; $i++)

$i='1';
if ($i<=$num) {
while ($i<=$num) {
echo '
	<input type="hidden" name="item_name_'.$row.'" value="'.$title.'"/>
	<input type="hidden" name="item_description_'.$row.'" value="A completly handmade diaper cake with elegant decorations"/>
	<input type="hidden" name="item_quantity_'.$row.'" value="'.$qty.'"/>
	<input type="hidden" name="item_price_'.$row.'" value="'.$price.'"/>';
}
}
echo '

	<input type="hidden" name="ship_method_name_1" value="UPS Ground"/>
	<input type="hidden" name="ship_method_price_1" value="16.95"/>
	<input type="hidden" name="ship_method_us_area_1" value="FULL_50_STATES"/>

   <input type="hidden" name="ship_method_name_2" value="UPS Priority"/>
   <input type="hidden" name="ship_method_price_2" value="29.99"/>
   <input type="hidden" name="ship_method_us_area_2" value="FULL_50_STATES"/>

<input type="hidden" name="ship_method_name_3" value="UPS Express Overnight"/>
   <input type="hidden" name="ship_method_price_3" value="45.99"/>
   <input type="hidden" name="ship_method_us_area_3" value="FULL_50_STATES"/>

<input type="hidden" name="ship_method_name_4" value="Local Delivery"/>
   <input type="hidden" name="ship_method_price_4" value="10.00"/>
   <input type="hidden" name="ship_method_us_area_4" value="FULL_50_STATES"/>

<input type="hidden" name="ship_method_name_5" value="Pickup"/>
   <input type="hidden" name="ship_method_price_5" value="0.00"/>
   <input type="hidden" name="ship_method_us_area_5" value="FULL_50_STATES"/>
<input type="hidden" name="_charset_"/>
<input type="image" name="Google Checkout" alt="Fast checkout through Google"
       src="https://checkout.google.com/buttons/checkout.gif?merchant_id=123456789&w=180&h=46&style=white&variant=text&loc=en_US"/>
	</form>';

}?>

 

What I want is for the first block of code to print in the form for every item in the cart. Ex: say there is 5 items in the cart I want this first block of code to print 5 times in the form.

Link to comment
https://forums.phpfreaks.com/topic/59191-array-question/
Share on other sites

See if this gives you what you want.

 

<?php

function showCart()
{
    global $db;
    $cart = $_SESSION['cart'];
    if ($cart) {
        $items = explode(',',$cart);
        $contents = array();
        foreach($items as $item) {
            $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
        }
        $output[] = '<form action="cart.php?action=update" method="post" id="cart">';
        $output[] = '<table width="500px" style="border:1px solid #EEEEEE;">';
        $output[] = '<tr>';
        $output[] = '<td>';
        $output[] = '<table align="center">';
        $output[] = '<tr>';
        $output[] = '<td align="center" class="style1">Item</td>';
        $output[] = '<td align="center" class="style1">Qty</td>';
        $output[] = '<td align="center" class="style1">Price</td>';
        $output[] = '<td align="center" class="style1">Sub Total</td>';
        $output[] = '</tr>';
        
        echo '<form method="POST" action="https://checkout.google.com/cws/v2/Merchant/123456789/checkoutForm"
accept-charset="utf-8">';
        
        foreach($contents as $id=>$qty) {
            $sql = 'SELECT * FROM products WHERE id = '.$id;
            $result = $db->query($sql);
            $row = $result->fetch();
            extract($row);
            $output[] = '<tr>';
            $output[] = '<td class="style4" align="center">'.$title.'</td>';
            $output[] = '<td align="center"><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
            $output[] = '<td class="style4" align="center">$'.$price.'</td>';
            $output[] = '<td class="style4" align="center">$'.($price * $qty).'</td>';
            $output[] = '<td align="center"><a href="cart.php?action=delete&id='.$id.'" class="r"><img src="/images/delete.jpg" alt="delete the diaper cake product" border="0" width="22" height="25"></a>';
            $total += $price * $qty;
            $output[] = '</tr>';
            
            echo '
<input type="hidden" name="item_name_'.$row.'" value="'.$title.'"/>
<input type="hidden" name="item_description_'.$row.'" value="A completly handmade diaper cake with elegant decorations"/>
<input type="hidden" name="item_quantity_'.$row.'" value="'.$qty.'"/>
<input type="hidden" name="item_price_'.$row.'" value="'.$price.'"/></td>';
        }
        $output[] = '</table>';
        $output[] = '<table align="center" width="500px">';
        $output[] = '<tr>';
        $output[] = '<td>';
        $output[] = '</td>';
        $output[] = '<td align="right">';
        $output[] = '<button type="submit">Update Qty</button>';
        $output[] = '</td>';
        $output[] = '</tr>';
        $output[] = '<tr>';
        $output[] = '<td>';
        $output[] = '</td>';
        $output[] = '<td align="right" style="font-size:large">';
        $output[] = 'Total:';
        $output[] = '</td>';
        $output[] = '<td style="font-size:medium; font-weight:bold; text-align:center;">$'.$total;
        $output[] = '</td>';
        $output[] = '</tr>';
        $output[] = '</table>';
        $output[] = '</form>';
        
    }
}
echo '

<input type="hidden" name="ship_method_name_1" value="UPS Ground"/>
<input type="hidden" name="ship_method_price_1" value="16.95"/>
<input type="hidden" name="ship_method_us_area_1" value="FULL_50_STATES"/>

<input type="hidden" name="ship_method_name_2" value="UPS Priority"/>
<input type="hidden" name="ship_method_price_2" value="29.99"/>
<input type="hidden" name="ship_method_us_area_2" value="FULL_50_STATES"/>

<input type="hidden" name="ship_method_name_3" value="UPS Express Overnight"/>
<input type="hidden" name="ship_method_price_3" value="45.99"/>
<input type="hidden" name="ship_method_us_area_3" value="FULL_50_STATES"/>

<input type="hidden" name="ship_method_name_4" value="Local Delivery"/>
<input type="hidden" name="ship_method_price_4" value="10.00"/>
<input type="hidden" name="ship_method_us_area_4" value="FULL_50_STATES"/>

<input type="hidden" name="ship_method_name_5" value="Pickup"/>
<input type="hidden" name="ship_method_price_5" value="0.00"/>
<input type="hidden" name="ship_method_us_area_5" value="FULL_50_STATES"/>
<input type="hidden" name="_charset_"/>
<input type="image" name="Google Checkout" alt="Fast checkout through Google"
src="https://checkout.google.com/buttons/checkout.gif?merchant_id=123456789&w=180&h=46&style=white&variant=text&loc=en_US"/>
</form>';

?>

Link to comment
https://forums.phpfreaks.com/topic/59191-array-question/#findComment-294075
Share on other sites

Ok I am going to try and describe this the best that I can. I want the variable $item_num to increment every time this code is ran. The reason for this is because the user may have more than one item in their shopping cart and this foreach loop displays each item name, description, quantity, & price. Google checkout which is what I am going to use once I get it working, requires each item to have a different number for the item name, item description, item quantity, & item price. Example: the first item would be: item_name_1, item_description_1, item_quantity_1, item_price_1

 

the second item would be: item_name_2, item_description_2, item_quantity_2, item_price_2

 

the third item would be: item_name_3, item_description_3, item_quantity_3, item_price_3

 

etc...

 

foreach ($contents as $id=>$qty) {
		$sql = 'SELECT * FROM products WHERE id = '.$id;
		$result = $db->query($sql);
		$row = $result->fetch();
		extract($row);
		$output[] = '<input type="text" name="item_name_'.$item_num.'" value="'.$title.'"/>';
		$output[] = '<input type="text" name="item_description_'.$item_num.'" value="A completly handmade diaper cake with elegant decorations"/>';
		$output[] = '<input type="text" name="item_quantity_'.$item_num.'" value="'.$qty.'"/>';
		$output[] = '<input type="text" name="item_price_'.$item_num.'" value="'.$price.'"/>';	
	}

to

Link to comment
https://forums.phpfreaks.com/topic/59191-array-question/#findComment-294101
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.