Jump to content

Help Needed with Receiving Postback from CCBill


RyanMinor

Recommended Posts

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++;
}

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++;
} ?>

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.

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.