RyanMinor Posted January 27, 2012 Share Posted January 27, 2012 I am trying to retrieve certain keys of a post array. I am sending a payment form with a dynamic number of product id's. I will never know how many product id's will be sent for each order. When the post array returns the values to my script it returns them as: $_POST['product_id_1'], $_POST['product_id_2'], etc. How would I be able to extract all post array keys that start with "product_id_? Here is what I was thinking earlier, but it doesn't work. $i = 1; foreach ($_POST['product_id_' . $i] as $product) { $productId = $product['product_id_' . $i]; $queryInsert = mysql_query("INSERT INTO video_purchase (video_purchase_purchase_id, video_purchase_video_id) VALUES ($purchaseId, $productId)", $connect) or die(mysql_error()); $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/255861-help-needed-with-receiving-postback-from-ccbill/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 27, 2012 Share Posted January 27, 2012 Will the keys be sequential, starting at one? Or can there be gaps in the numbers? Quote Link to comment https://forums.phpfreaks.com/topic/255861-help-needed-with-receiving-postback-from-ccbill/#findComment-1311578 Share on other sites More sharing options...
RyanMinor Posted January 27, 2012 Author Share Posted January 27, 2012 They will be sequential and starting at 1. Below is the part of the form that will be generating the values: <?php $i = 1; foreach($_SESSION['cart'] as $video_id => $x) { ?> <input type="hidden" name="product_id_<?php echo $i; ?>" value="<?php echo $video_id; ?>" /> <?php $i++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/255861-help-needed-with-receiving-postback-from-ccbill/#findComment-1311580 Share on other sites More sharing options...
PFMaBiSmAd Posted January 27, 2012 Share Posted January 27, 2012 Either one of the following methods will access just the product_id_x values - <?php $i = 1; while(isset($_POST['product_id_' . $i])){ echo $_POST['product_id_' . $i] . '<br />'; $i++; } <?php $keys = array_keys($_POST); foreach($keys as $key){ if(strpos($key,'product_id_')===0){ echo "Key: $key, Value: {$_POST[$key]}<br />"; } } If this is just within your code and is not a format that the gateway requires, you should use array names for the form field - http://us3.php.net/manual/en/faq.html.php#faq.html.arrays You will then be able to use a very simple foreach(){} loop to iterate over the $_POST['product_id'] array. Quote Link to comment https://forums.phpfreaks.com/topic/255861-help-needed-with-receiving-postback-from-ccbill/#findComment-1311582 Share on other sites More sharing options...
RyanMinor Posted January 27, 2012 Author Share Posted January 27, 2012 I chose your method #1 and it works perfectly. Thanks for the quick response. I will probably take your last part of advice later one, but for now the client handed me this site and wants it done quickly. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/255861-help-needed-with-receiving-postback-from-ccbill/#findComment-1311584 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.