jason360 Posted September 18, 2012 Share Posted September 18, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/268528-php-array-help/ Share on other sites More sharing options...
Jessica Posted September 18, 2012 Share Posted September 18, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268528-php-array-help/#findComment-1379099 Share on other sites More sharing options...
jason360 Posted September 18, 2012 Author Share Posted September 18, 2012 Hi Jesi, I had a look and tried what you recommended. Its still not producing an array. will keep trying. Quote Link to comment https://forums.phpfreaks.com/topic/268528-php-array-help/#findComment-1379114 Share on other sites More sharing options...
Jessica Posted September 18, 2012 Share Posted September 18, 2012 Post your new code. Quote Link to comment https://forums.phpfreaks.com/topic/268528-php-array-help/#findComment-1379140 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.