Search code examples
phpmysqlarrayspaypaldelimiter

Comma in array as delimiter only shows last section when displaying to user in PHP program


I have built a shopping cart which uses PayPal. If a user buys two products the array will be sent through to the IPN. I'm not sure how I can use the delimiter to create a new product information variable. For example this is what the user buys:

enter image description here

The array looks like this:

35 *-* 1 *-* XS *-* BO-CB005B Mountain Bke Entry Level, 33 *-* 1 *-* XS *-* B014 Mountian Bike,

The email being sent to the user only displays the last product after the first comma. I would like to display all the products ordered rather than the last one.

enter image description here

How can I display all the arrays rather than the last one?

This is the code in the cart that sends the array to IPN:

$product_id_array .= "$item_id *-* " .$each_item['quantity']." *-* " . $each_item['size'] . " *-* ". $product_name . ", ";  

This is the code in the IPN script

$product_id_string = $_POST['custom'];
$product_id_string = rtrim($product_id_string, ","); // remove last comma
// Explode the string, make it an array, then query all the prices out, add them up, and make sure they match the payment_gross amount
$id_str_array = explode(",", $product_id_string); // Uses Comma(,) as delimiter(break point)
$fullAmount = 0;
foreach ($id_str_array as $key => $value) {

    $id_quantity_pair = explode("*-*", $value); // Uses Hyphen(-) as delimiter to separate product ID from its quantity
    $product_id = $id_quantity_pair[0]; // Get the product ID
    $product_quantity = $id_quantity_pair[1]; // Get the quantity
    $product_size = $id_quantity_pair[2];
    $product_name = $id_quantity_pair[3];

    $productInfo = "ID: <strong>$product_id</strong> <br />Name: <strong>$product_name</strong><br />Quantity: <strong>$product_quantity</strong><br />Size: <strong>$product_size</strong>";   

    $sql = mysql_query("SELECT price FROM productList WHERE id='$product_id' LIMIT 1");
    while($row = mysql_fetch_array($sql)){
        $product_price = $row["price"];
    }
    $product_price = $product_price * $product_quantity;
    $fullAmount = $fullAmount + $product_price;
}

'custom' is the hidden value being sent to paypal:

<input type="hidden" name="custom" value="'.$product_id_array.'">

Solution

  • @Drk_alien found the answer.

    $productInfo = "$productInfo ID: <strong>$product_id</strong> <br />Name: <strong>$product_name</strong><br />Quantity: <strong>$product_quantity</strong><br />Size: <strong>$product_size</strong>";  
    

    The line above needs the extra $productInfo in order to contact the previous html generated code. $productInfo variable stores the last product information because it is being overwritten every time.