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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.