graham23s Posted August 20, 2008 Share Posted August 20, 2008 Hi Guys, i'm having trouble with this piece of code: <?php // See what people have in there carts $q_sneak = "SELECT `customer_id`,`product_id` FROM `fcp_orders` ORDER BY `customer_id` DESC"; $r_sneak = mysql_query($q_sneak); // Loop them out while ($a_sneak = mysql_fetch_array($r_sneak)) { // Customers id $cus_id = $a_sneak['customer_id']; $cus_pt = $a_sneak['product_id']; // Query customer details $q_c = "SELECT * FROM `fcp_customers` WHERE `id`='$cus_id'"; $r_c = mysql_query($q_c); $a_c = mysql_fetch_array($r_c); // Vars $c_fn = $a_c['first_name']; $c_ln = $a_c['last_name']; // Query the product details $q_p = "SELECT * FROM `fcp_products` WHERE `id`='$cus_pt'"; $r_p = mysql_query($q_p); $a_p = mysql_fetch_array($r_p); // Vars $p_im = $a_p['product_thumbnail']; print("<table width='95%' border='1' cellpadding='5' cellspacing='0' />\n"); print("<tr>\n"); print("<td colspan='2' align='left' width=\"15%\">Customers Name:</td><td align=\"left\">$c_fn $c_ln</td>\n"); print("</tr>\n"); print("<tr>\n"); print("<td colspan='2' align='left' width=\"15%\">Products in cart:</td><td align=\"left\"><img src=\"products/thumbnails/$p_im\"></td>\n"); print("</tr>\n"); print("</table>\n"); print("<br />"); } ?> what im trying to do is say i have 3 items in my shopping cart: 1, 456, 23 <-- product ids i want to see who has added what in there cart but the way the code is now it loops out 3 tables! instead of 1 table with the 3 items in it! so if i have 6 items in my cart i get 6 tables where as i just want 1 table with the 6 products images in it per person! hope that makes sense lol cheers for nay help Graham Link to comment https://forums.phpfreaks.com/topic/120619-solved-looping-tables/ Share on other sites More sharing options...
akitchin Posted August 20, 2008 Share Posted August 20, 2008 this involves tracking which customer you're looking at. once the customer_id changes, end the table and echo a new one: $last_customer = 0; // Loop them out while ($a_sneak = mysql_fetch_array($r_sneak)) { // Customers id $cus_id = $a_sneak['customer_id']; $cus_pt = $a_sneak['product_id']; // check if this is a new customer if ($cus_id != $last_customer) { // if he is, and he isn't the first, end the last table if ($last_customer != 0) { print("</table>\n<br />\n"); } // start a new table print("<table width='95%' border='1' cellpadding='5' cellspacing='0' />\n"); } // Query customer details $q_c = "SELECT * FROM `fcp_customers` WHERE `id`='$cus_id'"; $r_c = mysql_query($q_c); $a_c = mysql_fetch_array($r_c); // Vars $c_fn = $a_c['first_name']; $c_ln = $a_c['last_name']; // Query the product details $q_p = "SELECT * FROM `fcp_products` WHERE `id`='$cus_pt'"; $r_p = mysql_query($q_p); $a_p = mysql_fetch_array($r_p); // Vars $p_im = $a_p['product_thumbnail']; print("<tr>\n"); print("<td colspan='2' align='left' width=\"15%\">Customers Name:</td><td align=\"left\">$c_fn $c_ln</td>\n"); print("</tr>\n"); print("<tr>\n"); print("<td colspan='2' align='left' width=\"15%\">Products in cart:</td><td align=\"left\"><img src=\"products/thumbnails/$p_im\"></td>\n"); print("</tr>\n"); // change this current customer to be the last customer $last_customer = $cus_id; } with some more editing, you could get it so that it only echoes the customer's name once. however, that's a little beyond my laziness limit. Link to comment https://forums.phpfreaks.com/topic/120619-solved-looping-tables/#findComment-621527 Share on other sites More sharing options...
graham23s Posted August 20, 2008 Author Share Posted August 20, 2008 not at all mate thats great i will work on the editing bit Graham Link to comment https://forums.phpfreaks.com/topic/120619-solved-looping-tables/#findComment-621538 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.