Samza Posted May 9, 2013 Share Posted May 9, 2013 Hey guys, I have created a shopping cart system and I am now up to the point of building the checkout script to allow the customer to checkout using paypal. I have the bqasic paypal script form however I now need to create an array which pulls the data out of the customers shopping cart (which is stored in a DB) and for each product, output in the form of a hidden input form with the fields; item_number_x, item_id_x, item_price_x, item_quantity_x. Where X is the value of each product in the shopping cart. I am really suck on where to start and could do with some advice. This is the code that I originally tried however Im not have any luck as I am fairly new to using arrays; function Paypal_items() { //Get the items to submit for paypal $cart = new ShoppingCart; $cart_id = $cart -> get_cart_id(); $num = 0; $cart_sql = mysql_query("SELECT product_id, product_title, product_price FROM shopping_cart WHERE cart_identifier='".mysql_real_escape_string($cart_id)."'"); $cart_result = mysql_fetch_assoc($cart_sql); foreach ($cart_result as $name => $value) { if ($value != 0) { if($name){ $product_id = $name; echo "<br />name: " . $product_id . " : value". $value; } $sql = mysql_query("SELECT product_id, product_title, product_price FROM shopping_cart WHERE cart_identifier='$cart_id'"); while ($sql_result = mysql_fetch_assoc($sql)) { $num++; echo $num; echo "<input type=\"hidden\" name=\"item_number_\"".$num." value=\"".$product_id."\""; } } else { echo "!= is false<br />"; } } } Thanks! Link to comment https://forums.phpfreaks.com/topic/277855-paypal-checkout-help/ Share on other sites More sharing options...
Samza Posted May 10, 2013 Author Share Posted May 10, 2013 Or if anyone knows any good websites that help teach about arrays in a practical way that would be great help Link to comment https://forums.phpfreaks.com/topic/277855-paypal-checkout-help/#findComment-1429488 Share on other sites More sharing options...
mac_gyver Posted May 10, 2013 Share Posted May 10, 2013 the only thing your code needs to do is run 1 (one) query that gets the content of the cart, then loop over the rows that the query retrieved and produce the output that you want. your foreach(){} loop makes no sense, it is looping over the first row in the cart and your query inside of the loop makes no sense because you have already queried for the contents of the cart. Link to comment https://forums.phpfreaks.com/topic/277855-paypal-checkout-help/#findComment-1429532 Share on other sites More sharing options...
Samza Posted May 10, 2013 Author Share Posted May 10, 2013 the only thing your code needs to do is run 1 (one) query that gets the content of the cart, then loop over the rows that the query retrieved and produce the output that you want. your foreach(){} loop makes no sense, it is looping over the first row in the cart and your query inside of the loop makes no sense because you have already queried for the contents of the cart. Thanks for this help, I was following a tutorial previously but it clearly was a bit messed up and didnt need to be that completed for my cart. Anyway here's what I've got; function Paypal_items() { //Get the items to submit for paypal $cart = new ShoppingCart; $cart_id = $cart -> get_cart_id(); // run 1 query to get the customers cart details // create a loop over the cart rows to output the fields $num = 0; $sql = mysql_query("SELECT * FROM shopping_cart WHERE cart_identifier='".mysql_real_escape_string($cart_id)."'"); $result = mysql_fetch_array($sql,0); $count = mysql_num_rows($sql); $i = 0; while($i < $count) { $num++; $i = $i+1; echo "<input type=\"hidden\" name=\"item_number_".$num."\" value=\"".$result[product_id]."\" />"; echo "<input type=\"hidden\" name=\"item_name_".$num."\" value=\"".$result[product_title]."\" />"; echo "<input type=\"hidden\" name=\"amount_".$num."\" value=\"".$result[product_price]."\" />"; } } However, one problem I am having this that if there is more than 1 item in the cart, it is repeating the same data from the 1st cart record rather than for each cart item Link to comment https://forums.phpfreaks.com/topic/277855-paypal-checkout-help/#findComment-1429561 Share on other sites More sharing options...
mac_gyver Posted May 11, 2013 Share Posted May 11, 2013 mysql_fetch_array only fetches one row each time it is called. to loop over all the rows in a result set, you might have seen some logic that looks like this - while($row = mysql_fetch_assoc($result)){ // code to run for each row } Link to comment https://forums.phpfreaks.com/topic/277855-paypal-checkout-help/#findComment-1429623 Share on other sites More sharing options...
Samza Posted May 12, 2013 Author Share Posted May 12, 2013 Thanks mac_gyver, I have resolved this issue I appreciate your help! Link to comment https://forums.phpfreaks.com/topic/277855-paypal-checkout-help/#findComment-1429649 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.