Jump to content

Php Array Help


jason360

Recommended Posts

Hey Guys,

 

Stuck on building an array that I am using for Paypal.  I want to build an array named $product_id_array to send on a single input value to paypal or all my individual id's and quantities.  I have done various combinations with no luck.  The product id is $id and the quantity is $value, I want to create an array of all for each item/id. The ids and quantities to be in the format "id-quantity," with commas and dashes (ex. 3-1, 5-2, 3-1).

 

I have tried various things but the array isn't working.  I am a noob so any help is appreciated.

 

function paypal_items() {
$num = 0;
foreach($_SESSION as $name => $value)	{
if($value!=0) {
	if(substr($name, 0 , 5)=='cart_') {
	$id = substr($name, 5, strlen($name)-5);
	$get = mysql_query('SELECT id, name, price, shipping FROM products WHERE id='.mysql_real_escape_string((int)$id));
	while ($get_row = mysql_fetch_assoc($get)) {
		$num++;
		echo '<input type="hidden" name="item_number_'.$num.'" value="'.$id.'">';
		echo '<input type="hidden" name="item_name_'.$num.'" value="'.$get_row['name'].'">';
		echo '<input type="hidden" name="amount_'.$num.'" value="'.$get_row['price'].'">';
		echo '<input type="hidden" name="shipping_'.$num.'" value="'.$get_row['shipping'].'">';
		echo '<input type="hidden" name="shipping2_'.$num.'" value="'.$get_row['shipping'].'">';
		echo '<input type="hidden" name="quantity_'.$num.'" value="'.$value.'">';
	}
	}
}
}
}

//product id array start


$product_id_array = '';
$i = 0;
foreach($_SESSION as $name => $value)	{
	$id = substr($name, 5, strlen($name)-5);
		$product_id_array .= "$id-".$value.",";
	}

//product id array end

 

Then send via form to Paypal:

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" >
        <input type="hidden" name="custom" value="<?php echo $product_id_array?>">
        <?php paypal_items(); ?>
</form>

 

Link to comment
https://forums.phpfreaks.com/topic/268528-php-array-help/
Share on other sites

You're not creating an array anywhere.

 

http://us3.php.net/manual/en/language.types.array.php

 

Scroll down to Creating/modifying with square bracket syntax

 

But if you're just going to echo it, you should use a string I guess. Or implode the array.

This line $product_id_array .= "$id-".$value.","; would read $product_id_array[] = "$id-$value";

Then use implode() on it.

Link to comment
https://forums.phpfreaks.com/topic/268528-php-array-help/#findComment-1379099
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.