0

Okay so, first of all, I searched through the www for this question, and I found some question related to arrays but not exactly to mine.

Okay so as you may know, paypal only allows one custom variable to be $POST but I need to gather the product id AND the quantity of the item bought. So to do this I made my custom variable into something that would get the post like (25-1,12-3,13-4) it means, the user bought 3 items(separated by commas), where the first number is the product id (then the separator '-' comes in) and the second one is the quantity. (so they're all in the same row and column)

Now my problem is displaying it from the database. I need to get the product name and details that's why I need to separate the numbers from each array as a string and fetch the data from the database for the information of the product. (I'm using an older version of php anyway, 5.2, I guess.)Now the problem is:

1.) It returns the word 'Array' (literally) so it would say like ArrayArrayArray

2.) How do I explode/separate those data so I can get it because I need the product ID to fetch some other data... I tried exploding it into array, then exploding it again but doesn't work (most likely my code is wrong?)

Here is my code: (I've already connected to the database)

$data = mysql_query("SELECT * from transactions") or die(mysql_error()); 
/* My table tag and headers goes here */
while($info = mysql_fetch_array( $data )) {
    echo "<tr>";
    echo '<td>' . $info['id'] . '</td>';
    echo "<td>";
    $array = $info['product_id_array'];
    $explode_array = explode(",", $array);
    foreach($explode_array as $explode_more){
        $explode_more = explode("-", $explode_array);
        $prod_id = $explode_more[0];
        $quantity = $explode_more[1];
        print_r($prod_id); //should echo the 25 in the array (25-1), right?
        print_r($quantity);
    }

    echo"</td>";
    echo"<tr>";
}

If only paypal would allow multiple custom variables T_T Thank you guys. Forgive me if I can't express my question very well or my language is not good, as english is not my first language :), Good day!

2
  • if you print_r($array) before you explode it what does that look like? Commented Sep 18, 2014 at 18:23
  • It displays the correct output like 25-1,13-1, Commented Sep 18, 2014 at 18:35

2 Answers 2

0

Your variable names are mixed up. Inside the foreach-loop, you should do something like this

foreach($explode_array as $explode_more){
    $explode_even_more = explode("-", $explode_more);
    $prod_id = $explode_even_more[0];
    $quantity = $explode_even_more[1];
    print_r($prod_id); //should echo the 25 in the array (25-1), right?
    print_r($quantity);
}

Note, that $explode_more is used inside the loop and $explore_array is left as is.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow it worked! I misunderstood the foreach names a bit, I guess. Mister, thank you. I was close, or was I not? Hehe, either way, I really thank you, am happy.
0

Separate this in multiple tables, never store non-atomic values in 1 column. Certainly not when they have relation with another table.

Suppose you want to know the sales from a certain product in some period.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.