how to use an array?

I'm trying store some code in an array then reuse it further down my page in a repeat region, but not really how to do it.

See code attached.

This is the using to repeat further down the page:

      foreach ($paypalquery as $eachquery2) {

       $eachquery2;      
    }

Hope this makes sense? Can you please check the code.
$paypalquery = array();

$paypalquery[] = $nextIndex = count($PP_DirectPayment_itemized[0]);
  				 $PP_DirectPayment_itemized[0][$nextIndex] = "Amount";
  				 $PP_DirectPayment_itemized[1][$nextIndex] = "".number_format(round($price,2), 2)  ."";
  				 $nextIndex = count($PP_DirectPayment_itemized[0]);
  				 $PP_DirectPayment_itemized[0][$nextIndex] = "Name";
  				 $PP_DirectPayment_itemized[1][$nextIndex] = "".$pname  ."";
  				 $nextIndex = count($PP_DirectPayment_itemized[0]);
  				 $PP_DirectPayment_itemized[0][$nextIndex] = "Number";
  				 $PP_DirectPayment_itemized[1][$nextIndex] = "".$product_id  ."";
  				 $nextIndex = count($PP_DirectPayment_itemized[0]);
  				 $PP_DirectPayment_itemized[0][$nextIndex] = "Quantity";
  				 $PP_DirectPayment_itemized[1][$nextIndex] = "".$qty  ."";
;


	foreach ($paypalquery as $eachquery2) {

       $eachquery2;	
    }

Open in new window

petewinterAsked:
Who is Participating?
 
adbrettConnect With a Mentor Commented:
I'm unsure what your code is trying to achieve, as there aren't any SQL statements in your code, but the syntax you're looking for is below.
<?php

$paypal_query = array();
$paypal_query[] = 'SQL HERE';
$paypal_query[] = 'MORE SQL HERE';
$paypal_query[] = 'NEXT HERE';
$paypal_query[] = 'AND AGAIN HERE';
$paypal_query[] = 'FINAL SQL STATEMENT HERE';


foreach($paypal_query as $sql) {
	mysql_query($sql) or die(mysql_error);
}

?>

Open in new window

0
 
petewinterAuthor Commented:
Sorry supplied the bottom part of the code slightly wrong. It should be:

      foreach ($paypalquery as $eachquery2) {

       $query_db = mysql_query($eachquery2) or die (mysql_error());      
    }
0
 
petewinterAuthor Commented:
Sorry I'm probably confusing the whole problem. All I need to do is repeat the content in this variable $paypalquery further down the page.

Does this make sense?
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
V4nP3rs13Commented:
To repeat content? You cam use FOR to do it. It does not make any sense to repeat content. Why do you want to do it?
0
 
nasirbestConnect With a Mentor Commented:
Very difficult to understand what you want!

let me explain something

1. you want ot create "$PP_DirectPayment_itemized" array

2. which hold some pair of variables mentioned as $PP_DirectPayment_itemized[0]
3. and values mentioned as $PP_DirectPayment_itemized[1]

4. and you also want to count total length of array like $nextIndex = count($PP_DirectPayment_itemized[0]);

5. and then you want to insert every pair into some table        $query_db = mysql_query($eachquery2) or die (mysql_error());      

If it all is ok then find below right solution for you otherwise let us know what is wrong with my understanding
$paypalquery = array();

$paypalquery['Amount']   = number_format(round($price,2), 2);
$paypalquery['Name']     = $pname;
$paypalquery['Number']   = $product_id;
$paypalquery['Quantity'] = $qty;

$nextIndex = count($paypalquery);

foreach ($paypalquery as $key => $value) {
  $result = mysql_query("INSERT INTO sometable (variable_name, value) VALUES ($key, $value)");
}

Open in new window

0
 
petewinterAuthor Commented:
Sorry my knowledge of this is not great. Many thanks for all your help!

See whole code attached.

I will explain further in next post.
<?php
$orderquery = array();
$paypalquery = array();
while (!$Tech8_Spares_eCart1->EOF()) {
    	$product_id  = $Tech8_Spares_eCart1->DisplayInfo("ID");
        $code  = $Tech8_Spares_eCart1->DisplayInfo("Code");
        $pname = $Tech8_Spares_eCart1->DisplayInfo("Name");
        $description = $Tech8_Spares_eCart1->DisplayInfo("Description");
        $weight = $Tech8_Spares_eCart1->DisplayInfo("Weight");
    	$qty = $Tech8_Spares_eCart1->DisplayInfo("Quantity");
		$symbol = $row_rs_exchange_rate_selected['symbol'];
		if($row_WAATKcustomers['price_level'] == 2) {//outer if block
    if($Tech8_Spares_eCart1->DisplayInfo("ID") == 1) {//inner if block
        $ResellerDiscount = "450.00";
    }//closes the inner if statement
    else {
    $ResellerDiscount = $Tech8_Spares_eCart1->DisplayInfo("Price") * $row_rs_mark_up_cost['reseller_discount1']; 
    }//ends the inner if block
}//closes the first if statement
else { 
    $ResellerDiscount = $Tech8_Spares_eCart1->DisplayInfo("Price");
}//end of outer if block
		$price = $ResellerDiscount * $row_rs_exchange_rate_selected['rate'];
		$total = number_format(round($price,2), 2) * $qty;
		$SubTotal +=$total;
		$customer_id = $row_WAATKcustomers['id'];
		$order_id = "T8CUST" .  $row_WAATKcustomers['id'] . $today;
        
    if($_POST) {
		$orderquery[] = "INSERT INTO orders (product_id, code, name, description, weight, quantity, price, customer_id, order_id)
					VALUES ('$product_id', '$code', '$pname', '$description', '$weight', '$qty', '$price', '$customer_id', '$order_id')";
    }

$tableRows .= "
<tr>
	<td bgcolor=\"#E5E8ED\" style=\"border-bottom:none\">{$pname}</td>
    <td bgcolor=\"#E5E8ED\" style=\"border-bottom:none\">{$code}</td>
    <td bgcolor=\"#E5E8ED\" style=\"border-bottom:none\">{$symbol}" . number_format(round($price,2), 2) . "</td>
	<td bgcolor=\"#E5E8ED\" style=\"border-bottom:none\" align=\"center\">{$qty}</td>
	<td bgcolor=\"#E5E8ED\" style=\"border-bottom:none\">{$symbol}" . number_format(round($total,2), 2) . "</td>
</tr>
";

$paypalquery[] = $nextIndex = count($PP_DirectPayment_itemized[0]);
  				 $PP_DirectPayment_itemized[0][$nextIndex] = "Amount";
  				 $PP_DirectPayment_itemized[1][$nextIndex] = "".number_format(round($price,2), 2)  ."";
  				 $nextIndex = count($PP_DirectPayment_itemized[0]);
  				 $PP_DirectPayment_itemized[0][$nextIndex] = "Name";
  				 $PP_DirectPayment_itemized[1][$nextIndex] = "".$pname  ."";
  				 $nextIndex = count($PP_DirectPayment_itemized[0]);
  				 $PP_DirectPayment_itemized[0][$nextIndex] = "Number";
  				 $PP_DirectPayment_itemized[1][$nextIndex] = "".$product_id  ."";
  				 $nextIndex = count($PP_DirectPayment_itemized[0]);
  				 $PP_DirectPayment_itemized[0][$nextIndex] = "Quantity";
  				 $PP_DirectPayment_itemized[1][$nextIndex] = "".$qty  ."";
;


$Tech8_Spares_eCart1->MoveNext(); } $Tech8_Spares_eCart1->MoveFirst();

 ?>
 <? $TaxConvertion = ((round($SubTotal,2) + round($delivery,2)) * ($tax / 100)); ?>
 <? if($row_WAATKcustomers['country'] == 2) { $VAT = $TaxConvertion; } else { $VAT = 0; } ?>
 <? $GrandTotal = $SubTotal - $discount + $charges + $VAT + $delivery; ?>
 
<?php

if (($_SERVER["REQUEST_METHOD"] == "POST") && (isset($_SERVER["HTTP_REFERER"]) && strpos(urldecode($_SERVER["HTTP_REFERER"]), urldecode($_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"])) > 0) && isset($_POST))  {
  $PP_DirectPayment_required = array();
  $PP_DirectPayment_required[0] = array();
  $PP_DirectPayment_required[1] = array();
  $PP_DirectPayment_itemized = array();
  $PP_DirectPayment_itemized[0] = array();
  $PP_DirectPayment_itemized[1] = array();
  $PP_DirectPayment_optional = array();
  $PP_DirectPayment_optional[0] = array();
  $PP_DirectPayment_optional[1] = array();
  $cartIndex = 0;
  $nextIndex = 0;
  $nextIndex = count($PP_DirectPayment_required[0]);
  $PP_DirectPayment_required[0][$nextIndex] = "Username";
  $PP_DirectPayment_required[1][$nextIndex] = "";
  $nextIndex = count($PP_DirectPayment_required[0]);
  $PP_DirectPayment_required[0][$nextIndex] = "Password";
  $PP_DirectPayment_required[1][$nextIndex] = "";
  $nextIndex = count($PP_DirectPayment_required[0]);
  $PP_DirectPayment_required[0][$nextIndex] = "Signature";
  $PP_DirectPayment_required[1][$nextIndex] = "";
  $nextIndex = count($PP_DirectPayment_required[0]);
  $PP_DirectPayment_required[0][$nextIndex] = "PaymentAction";
  $PP_DirectPayment_required[1][$nextIndex] = "Sale";
  $nextIndex = count($PP_DirectPayment_required[0]);
  $PP_DirectPayment_required[0][$nextIndex] = "Currency";
  $PP_DirectPayment_required[1][$nextIndex] = "".$CurrencyCode ."";
  $nextIndex = count($PP_DirectPayment_required[0]);
  $PP_DirectPayment_required[0][$nextIndex] = "IPAddress";
  $PP_DirectPayment_required[1][$nextIndex] = "".$_SERVER["REMOTE_ADDR"]  ."";
  $nextIndex = count($PP_DirectPayment_required[0]);
  $PP_DirectPayment_required[0][$nextIndex] = "CreditCardType";
  $PP_DirectPayment_required[1][$nextIndex] = "".$row_WAATKcustomers['cc_type']  ."";
  $nextIndex = count($PP_DirectPayment_required[0]);
  $PP_DirectPayment_required[0][$nextIndex] = "CreditCardNumber";
  $PP_DirectPayment_required[1][$nextIndex] = "".$row_WAATKcustomers['cc_number']  ."";
  $nextIndex = count($PP_DirectPayment_required[0]);
  $PP_DirectPayment_required[0][$nextIndex] = "ExpMonth";
  $PP_DirectPayment_required[1][$nextIndex] = "".$row_WAATKcustomers['exp_month']  ."";
  $nextIndex = count($PP_DirectPayment_required[0]);
  $PP_DirectPayment_required[0][$nextIndex] = "ExpYear";
  $PP_DirectPayment_required[1][$nextIndex] = "".$row_WAATKcustomers['exp_year']  ."";
  $nextIndex = count($PP_DirectPayment_required[0]);
  $PP_DirectPayment_required[0][$nextIndex] = "FirstName";
  $PP_DirectPayment_required[1][$nextIndex] = "".$row_WAATKcustomers['first_name']  ."";
  $nextIndex = count($PP_DirectPayment_required[0]);
  $PP_DirectPayment_required[0][$nextIndex] = "LastName";
  $PP_DirectPayment_required[1][$nextIndex] = "".$row_WAATKcustomers['last_name']  ."";
  $nextIndex = count($PP_DirectPayment_required[0]);
  $PP_DirectPayment_required[0][$nextIndex] = "Street1";
  $PP_DirectPayment_required[1][$nextIndex] = "".$row_WAATKcustomers['address1']  ."";
  $nextIndex = count($PP_DirectPayment_required[0]);
  $PP_DirectPayment_required[0][$nextIndex] = "CityName";
  $PP_DirectPayment_required[1][$nextIndex] = "".$row_WAATKcustomers['town_city']  ."";
  $nextIndex = count($PP_DirectPayment_required[0]);
  $PP_DirectPayment_required[0][$nextIndex] = "StateOrProvince";
  $PP_DirectPayment_required[1][$nextIndex] = "".$row_WAATKcustomers['county']  ."";
  $nextIndex = count($PP_DirectPayment_required[0]);
  $PP_DirectPayment_required[0][$nextIndex] = "PostalCode";
  $PP_DirectPayment_required[1][$nextIndex] = "".$row_WAATKcustomers['post_code']  ."";
  $nextIndex = count($PP_DirectPayment_required[0]);
  $PP_DirectPayment_required[0][$nextIndex] = "Country";
  $PP_DirectPayment_required[1][$nextIndex] = "".$row_rs_country_list['currency_code']  ."";
  $nextIndex = count($PP_DirectPayment_required[0]);
  $PP_DirectPayment_required[0][$nextIndex] = "UseSandbox";
  $PP_DirectPayment_required[1][$nextIndex] = "true";

  $PP_DirectPayment_itemized[0][0] = "OrderTotal";
  $PP_DirectPayment_itemized[1][0] = "".number_format(round($GrandTotal,2), 2)  ."";
  $PP_DirectPayment_itemized[0][1] = "OrderDescription";
  $PP_DirectPayment_itemized[1][1] = "";
  
 XXXXXX CODE TO GO HERE XXXXXXX
	

  $nextIndex = count($PP_DirectPayment_optional[0]);
  $PP_DirectPayment_optional[0][$nextIndex] = "MerchantSessionID";
  $PP_DirectPayment_optional[1][$nextIndex] = "".session_id()  ."";
  $nextIndex = count($PP_DirectPayment_optional[0]);
  $PP_DirectPayment_optional[0][$nextIndex] = "Payer";
  $PP_DirectPayment_optional[1][$nextIndex] = "".$row_WAATKcustomers['email']  ."";
  $nextIndex = count($PP_DirectPayment_optional[0]);
  $PP_DirectPayment_optional[0][$nextIndex] = "Street2";
  $PP_DirectPayment_optional[1][$nextIndex] = "".$row_WAATKcustomers['address1']  ."";
  $nextIndex = count($PP_DirectPayment_optional[0]);
  $PP_DirectPayment_optional[0][$nextIndex] = "Phone";
  $PP_DirectPayment_optional[1][$nextIndex] = "".$row_WAATKcustomers['phone']  ."";
  $nextIndex = count($PP_DirectPayment_optional[0]);
  $PP_DirectPayment_optional[0][$nextIndex] = "CVV2";
  $PP_DirectPayment_optional[1][$nextIndex] = "".$row_WAATKcustomers['cvv']  ."";
  
  $nextIndex = count($PP_DirectPayment_optional[0]);
  $PP_DirectPayment_optional[0][$nextIndex] = "ItemTotal";
  $PP_DirectPayment_optional[1][$nextIndex] = "".number_format(round($SubTotal,2), 2)  ."";
  $nextIndex = count($PP_DirectPayment_optional[0]);
  $PP_DirectPayment_optional[0][$nextIndex] = "ShippingTotal";
  $PP_DirectPayment_optional[1][$nextIndex] = "".$delivery  ."";
  $nextIndex = count($PP_DirectPayment_optional[0]);
  $PP_DirectPayment_optional[0][$nextIndex] = "HandlingTotal";
  $PP_DirectPayment_optional[1][$nextIndex] = "".$charges - $discount  .""; 
  $nextIndex = count($PP_DirectPayment_optional[0]);
  $PP_DirectPayment_optional[0][$nextIndex] = "TaxTotal";
  $PP_DirectPayment_optional[1][$nextIndex] = "".number_format(round($VAT,2), 2)  ."";
  
  $nextIndex = count($PP_DirectPayment_optional[0]);
  $PP_DirectPayment_optional[0][$nextIndex] = "InvoiceID";
  $PP_DirectPayment_optional[1][$nextIndex] = "".$order_id  ."";
  $nextIndex = count($PP_DirectPayment_optional[0]);
  $PP_DirectPayment_optional[0][$nextIndex] = "SH_Name";
  $PP_DirectPayment_optional[1][$nextIndex] = "".((isset($_POST["first_name_ship"]))?$_POST["first_name_ship"]:"")  ." ".((isset($_POST["last_name_ship"]))?$_POST["last_name_ship"]:"")  ."";
  $nextIndex = count($PP_DirectPayment_optional[0]);
  $PP_DirectPayment_optional[0][$nextIndex] = "SH_Street1";
  $PP_DirectPayment_optional[1][$nextIndex] = "".((isset($_POST["address1_ship"]))?$_POST["address1_ship"]:"")  ."";
  $nextIndex = count($PP_DirectPayment_optional[0]);
  $PP_DirectPayment_optional[0][$nextIndex] = "SH_Street2";
  $PP_DirectPayment_optional[1][$nextIndex] = "".((isset($_POST["address2_ship"]))?$_POST["address2_ship"]:"")  ."";
  $nextIndex = count($PP_DirectPayment_optional[0]);
  $PP_DirectPayment_optional[0][$nextIndex] = "SH_CityName";
  $PP_DirectPayment_optional[1][$nextIndex] = "".((isset($_POST["town_city_ship"]))?$_POST["town_city_ship"]:"")  ."";
  $nextIndex = count($PP_DirectPayment_optional[0]);
  $PP_DirectPayment_optional[0][$nextIndex] = "SH_StateOrProvince";
  $PP_DirectPayment_optional[1][$nextIndex] = "".((isset($_POST["county_ship"]))?$_POST["county_ship"]:"")  ."";
  $nextIndex = count($PP_DirectPayment_optional[0]);
  $PP_DirectPayment_optional[0][$nextIndex] = "SH_PostalCode";
  $PP_DirectPayment_optional[1][$nextIndex] = "".((isset($_POST["post_code_ship"]))?$_POST["post_code_ship"]:"")  ."";
  $nextIndex = count($PP_DirectPayment_optional[0]);
  $PP_DirectPayment_optional[0][$nextIndex] = "SH_Country";
  $PP_DirectPayment_optional[1][$nextIndex] = "".((isset($_POST["country_ship_code"]))?$_POST["country_ship_code"]:"")  ."";
  $nextIndex = count($PP_DirectPayment_optional[0]);
  $PP_DirectPayment_optional[0][$nextIndex] = "SH_Phone";
  $PP_DirectPayment_optional[1][$nextIndex] = "".((isset($_POST["phone_ship"]))?$_POST["phone_ship"]:"")  ."";

  $WAGatewayResponse = PP_DirectPayment_Post($PP_DirectPayment_required,$PP_DirectPayment_itemized,$PP_DirectPayment_optional);
  if ($WAGatewayResponse)  {
	  
	mysql_query("INSERT INTO orders_master (customer_id, order_id, symbol, discount, charges, delivery, tax, exchange_rate, country) VALUES ('$customer_id', '$order_id', '$symbol', '$discount', '$charges', '$delivery', '$tax', '$exchange_rate', '$Countryid')");
	
	foreach ($orderquery as $eachquery) {

       $query_db = mysql_query($eachquery) or die (mysql_error());	
    }

	$RecipientEmail = "".$row_WAATKcustomers['email']  ."";include("WA_Universal_Email/WAUE_order_2cc.php");
	$RecipientEmail = "pete@largeformatreview.com";include("WA_Universal_Email/WAUE_order_1cc.php");
    $Tech8_Spares_eCart1->ClearCart();
	$Tech8_Spares_eCart1->cartAction = "ClearCart";
	
    header('location: order_confirmation.php?no='.$order_id.'');
    exit;
  }
  else if ("order_failure.php" != "") {
    header("Location: ". "order_failure.php");
	exit();
  }
}
?>

Open in new window

0
 
petewinterAuthor Commented:
I want the code in the array on line 44  (This is already in a repeat area)

to replace line 140.
0
 
adbrettConnect With a Mentor Commented:
Hi Pete,

If you remove lines 72, 73, and 74:

$PP_DirectPayment_itemized = array();
$PP_DirectPayment_itemized[0] = array();
$PP_DirectPayment_itemized[1] = array();

You won't need the code to repeat at line 140 as it will already be in memory when you pass it to your payment function at the end.
0
 
petewinterAuthor Commented:
Thanks you have helped me realise what I am doing wrong.
0
All Courses

From novice to tech pro — start learning today.