lional Posted April 8, 2009 Share Posted April 8, 2009 I have clients with a predefined list of products that they order each month. These are stored in a table with the clients id, the product code and the quantity. I would like to pull the clients selection and then tie it up to the products table. I am getting an invalid argument with the foreach when I pull the clients selection and assign it to the shopping cart. I have posted the code where I am getting the problem. <body bgcolor="#32818B" topmargin="0"> <?php session_start(); include 'header.php'; $client_out = $_SESSION['clid']; include 'includes/conn_db.php'; $query_cl = "SELECT * from client_supps WHERE client_id = '$client_out'"; $result_cl = mysql_query($query_cl, $conn); while ($row_cl = mysql_fetch_assoc($result_cl)){ $supp_id_out = $row_cl["supp_id"]; $qty_out = $row_cl["qty"]; foreach($supp_id_out as $key => $value) { $qty1 = $qty_out; $pid = $key; // add to the cart session variable $_SESSION['cart'][$pid] = $qty1; }} if (isset($_POST['submit'])) { foreach ($_POST['product'] as $key => $value) { if (($value == 0) AND (is_numeric($value))) { unset ($_SESSION['cart'][$key]); } elseif (is_numeric($value) AND ($value > 0)) { $_SESSION['cart'][$key] = $value; } } } // check if the shopping cart is empty $empty = TRUE; if (isset($_SESSION['cart'])) { foreach($_SESSION['cart'] as $key => $value) { if (isset($value)) { $empty = FALSE; } } } Thanks in advance Lional Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/ Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 That's because $supp_id_out is a string and foreach takes an array. Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804748 Share on other sites More sharing options...
revraz Posted April 8, 2009 Share Posted April 8, 2009 Move your FOREACH code up into your WHILE loop. Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804751 Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 Like revraz said, you probably want $supp_id_out to be an array that's created from the while loop. Try changing this portion: $supp_id_out = array(); while ($row_cl = mysql_fetch_assoc($result_cl)){ $supp_id_out = $row_cl["supp_id"]; $qty_out = $row_cl["qty"]; } foreach($supp_id_out as $key => $value) { $qty1 = $qty_out; $pid = $key; // add to the cart session variable $_SESSION['cart'][$pid] = $qty1; } Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804760 Share on other sites More sharing options...
lional Posted April 8, 2009 Author Share Posted April 8, 2009 I have got it got so far, but no rows are being pulled. Do I need to use a foreach or can I do it the way that I have just done it I have got it got so far, but no rows are being pulled. Do I need to use a foreach or can I do it the way that I have just done it Thanks Lional Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804776 Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 Code? Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804781 Share on other sites More sharing options...
lional Posted April 8, 2009 Author Share Posted April 8, 2009 I get the following error Invalid argument supplied for foreach() Here is my code <body bgcolor="#32818B" topmargin="0"> <?php session_start(); include 'header.php'; $client_out = $_SESSION['clid']; include 'includes/conn_db.php'; $query_cl = "SELECT * from client_supps WHERE client_id = '$client_out'"; $result_cl = mysql_query($query_cl, $conn); while ($row_cl = mysql_fetch_assoc($result_cl)){ $supp_id_out = $row_cl["supp_id"]; $qty_out = $row_cl["qty"]; } foreach($supp_id_out as $key => $value) { $qty1 = $qty_out; $pid = $key; // add to the cart session variable $_SESSION['cart'][$pid] = $qty1; } // Display the cart if it is not empty // Retrieve all of the information for the products in the cart $query = 'SELECT * FROM supplements WHERE supp_id IN ('; foreach ($_SESSION['cart'] as $key => $value) { $query .= $key . ','; } $query = substr ($query, 0, -1) . ') '; $result = mysql_query($query); // create a table and a form print <<<TOP <table width="790" align="center" bgcolor="#b3314b" cellpadding="0" cellspacing="0"> <tr><td height="450"> <table summary="" width="560" align="center" bgcolor="#b3314b"> <tr> <td nowrap align="center"><font face="arial" size="2" color="white">></font></tr></table> <table summary="" align="center" width="560" border="1"> <tr> <td> <table border="0" width="560" cellpadding="3"bgcolor="#b3314b"> <tr> <td align="left" width="250"><font face="arial" size="2" color="white"><b>Supplement</b></font></td> <td align="right" width="120"><font face="arial" size="2" color="white"><b>Line Total</b></font></td> </tr> <form action="view_cart.php" method="post"> TOP; // Print each item $total = 0; // total cost of the order while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { /* if ($row['specials_discount'] > 0) { $list_price_out = $row['price'] / $row['specials_discount']; } else { $list_price_out = $row['price']; } */ $list_price_out = $row['price']; $list_price_out = number_format($list_price_out, 2, '.', ''); // Calculate the total and subtotals $subtotal = $_SESSION['cart'][$row['prod_id']] * $list_price_out; $subtotal = number_format($subtotal, 2, '.', ''); $total += $subtotal; $total = number_format($total, 2, '.', ''); if ($_SESSION['cart'][$row['prod_id']] > 0) { // print the row print <<<ROW <tr> <td align="left"><font face="arial" size="2" color="white">{$row['supplement']}</font></td> <td align="center"> <input type="text" size="3" name="product[{$row['supp_id']}]" value="{$_SESSION['cart'][$row['supp_id']]}"></td> <td align="right"><font face="arial" size="2" color="white">R $list_price_out </font></td> <td align="right"><font face="arial" size="2" color="white">R $subtotal</font></td> </tr> ROW; }} // end of the WHILE loop // print the footer and close the table and the form print <<<FOOTER <tr> <td></td><td></td><td></td> <td align="right" valign="top"><font face="arial" size="2" color="white"><b>Total:</b></font></td> <td align="right" valign="top"><font face="arial" size="2" color="white"><b>R $total</b></font><br><br></td> </tr> </table> <div align="center"> <table width="260" align="center"> <tr><td width="130" align="right"><input type="image" name="submit" src="images/update_cart.jpg" border="0"></form></td><td width="130" align="left"><form action="checkout.php" method="post"><input type="image" name="checkout" src="images/checkout.jpg" border="0"></form> </td><td width="85"></td></tr></div> FOOTER; /* print <<<EMP <table border="0" width="580" align="center"> <tr> <td> <table summary="" width="300" border="1" style="border-color:#960232" bgcolor="#f8f3c2" align="center"> <tr> <td> <table border="0" width="300" cellpadding="3"> <tr><td align="center"><p><font face="arial" size="2" color="#5e5d5d">Your cart is empty!</font> </td></tr></table></table></table> EMP; */ ?> </table> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804797 Share on other sites More sharing options...
revraz Posted April 8, 2009 Share Posted April 8, 2009 You didn't do what we suggested... and you didn't read what Maq said was your initial problem. Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804800 Share on other sites More sharing options...
lional Posted April 8, 2009 Author Share Posted April 8, 2009 I don't understand where it must be in teh while loop Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804809 Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 Above this line: while ($row_cl = mysql_fetch_assoc($result_cl)){ You need to add: $supp_id_out = array(); And change this line to: $supp_id_out[] = $row_cl["supp_id"]; Without initializing $supp_id_out as an array, it will always be a string with the value of the last record that's pulled from the database. Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804816 Share on other sites More sharing options...
lional Posted April 8, 2009 Author Share Posted April 8, 2009 The foreach seems to work but it is complaining about the select from the supplements table $query = 'SELECT * FROM supplements WHERE supp_id IN ('; foreach ($_SESSION['cart'] as $key => $value) { $query .= $key . ','; } $query = substr ($query, 0, -1) . ') '; $result = mysql_query($query); Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804827 Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 Change this line to: (notice the dot) $query .= substr ($query, 0, -1) . ') '; You can get descriptive SQL error messages by changing this line to: $result = mysql_query($query) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804828 Share on other sites More sharing options...
lional Posted April 8, 2009 Author Share Posted April 8, 2009 The error is Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\htdocs\metabolic-typing\order_supplements.php on line 56 Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804832 Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 That means your query is failing. Did you change the other line as well? Could you post your current code? Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804835 Share on other sites More sharing options...
lional Posted April 8, 2009 Author Share Posted April 8, 2009 Yes thanks for the help you are giving here is my code <body bgcolor="#32818B" topmargin="0"> <?php session_start(); include 'header.php'; $client_out = $_SESSION['clid']; include 'includes/conn_db.php'; $query_cl = "SELECT * from client_supps WHERE client_id = '$client_out'"; $result_cl = mysql_query($query_cl, $conn); $supp_id_out = array(); while ($row_cl = mysql_fetch_assoc($result_cl)){ $supp_id_out[] = $row_cl["supp_id"]; $qty_out = $row_cl["qty"]; } $qty1 = $qty_out; $pid = $key; // add to the cart session variable $_SESSION['cart'][$pid] = $qty1; // Display the cart if it is not empty // Retrieve all of the information for the products in the cart $query = 'SELECT * FROM supplements WHERE supp_id IN ('; foreach ($_SESSION['cart'] as $key => $value) { $query .= $key . ','; } $query .= substr ($query, 0, -1) . ') '; $result = mysql_query($query) or die(mysql_error()); // create a table and a form print <<<TOP <table width="790" align="center" bgcolor="#b3314b" cellpadding="0" cellspacing="0"> <tr><td height="450"> <table summary="" width="560" align="center" bgcolor="#b3314b"> <tr> <td nowrap align="center"><font face="arial" size="2" color="white">></font></tr></table> <table summary="" align="center" width="560" border="1"> <tr> <td> <table border="0" width="560" cellpadding="3"bgcolor="#b3314b"> <tr> <td align="left" width="250"><font face="arial" size="2" color="white"><b>Supplement</b></font></td> <td align="right" width="120"><font face="arial" size="2" color="white"><b>Line Total</b></font></td> </tr> <form action="view_cart.php" method="post"> TOP; // Print each item $total = 0; // total cost of the order while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { /* if ($row['specials_discount'] > 0) { $list_price_out = $row['price'] / $row['specials_discount']; } else { $list_price_out = $row['price']; } */ $list_price_out = $row['price']; $list_price_out = number_format($list_price_out, 2, '.', ''); // Calculate the total and subtotals $subtotal = $_SESSION['cart'][$row['prod_id']] * $list_price_out; $subtotal = number_format($subtotal, 2, '.', ''); $total += $subtotal; $total = number_format($total, 2, '.', ''); if ($_SESSION['cart'][$row['prod_id']] > 0) { // print the row print <<<ROW <tr> <td align="left"><font face="arial" size="2" color="white">{$row['supplement']}</font></td> <td align="center"> <input type="text" size="3" name="product[{$row['supp_id']}]" value="{$_SESSION['cart'][$row['supp_id']]}"></td> <td align="right"><font face="arial" size="2" color="white">R $list_price_out </font></td> <td align="right"><font face="arial" size="2" color="white">R $subtotal</font></td> </tr> ROW; }} // end of the WHILE loop // print the footer and close the table and the form print <<<FOOTER <tr> <td></td><td></td><td></td> <td align="right" valign="top"><font face="arial" size="2" color="white"><b>Total:</b></font></td> <td align="right" valign="top"><font face="arial" size="2" color="white"><b>R $total</b></font><br><br></td> </tr> </table> <div align="center"> <table width="260" align="center"> <tr><td width="130" align="right"><input type="image" name="submit" src="images/update_cart.jpg" border="0"></form></td><td width="130" align="left"><form action="checkout.php" method="post"><input type="image" name="checkout" src="images/checkout.jpg" border="0"></form> </td><td width="85"></td></tr></div> FOOTER; /* print <<<EMP <table border="0" width="580" align="center"> <tr> <td> <table summary="" width="300" border="1" style="border-color:#960232" bgcolor="#f8f3c2" align="center"> <tr> <td> <table border="0" width="300" cellpadding="3"> <tr><td align="center"><p><font face="arial" size="2" color="#5e5d5d">Your cart is empty!</font> </td></tr></table></table></table> EMP; */ ?> </table> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804838 Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 Now the foreach for $supp_id_out is missing... Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804841 Share on other sites More sharing options...
lional Posted April 8, 2009 Author Share Posted April 8, 2009 are you referring to this line foreach($supp_id_out as $key => $value) and where do I put it if it is Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804845 Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 are you referring to this line foreach($supp_id_out as $key => $value) and where do I put it if it is Where you had it before. I never told you to take it out. Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804848 Share on other sites More sharing options...
lional Posted April 8, 2009 Author Share Posted April 8, 2009 my code reads as <body bgcolor="#32818B" topmargin="0"> <?php session_start(); include 'header.php'; $client_out = $_SESSION['clid']; include 'includes/conn_db.php'; $query_cl = "SELECT * from client_supps WHERE client_id = '$client_out'"; $result_cl = mysql_query($query_cl, $conn); $supp_id_out = array(); while ($row_cl = mysql_fetch_assoc($result_cl)){ $supp_id_out[] = $row_cl["supp_id"]; $qty_out = $row_cl["qty"]; foreach($supp_id_out as $key => $value) { $qty1 = $qty_out; $pid = $key; // add to the cart session variable $_SESSION['cart'][$pid] = $qty1; }} // Display the cart if it is not empty // Retrieve all of the information for the products in the cart $query = 'SELECT * FROM supplements WHERE supp_id IN ('; foreach ($_SESSION['cart'] as $key => $value) { $query .= $key . ','; } $query .= substr ($query, 0, -1) . ') '; $result = mysql_query($query) or die(mysql_error()); // create a table and a form print <<<TOP <table width="790" align="center" bgcolor="#b3314b" cellpadding="0" cellspacing="0"> <tr><td height="450"> <table summary="" width="560" align="center" bgcolor="#b3314b"> <tr> <td nowrap align="center"><font face="arial" size="2" color="white">></font></tr></table> <table summary="" align="center" width="560" border="1"> <tr> <td> <table border="0" width="560" cellpadding="3"bgcolor="#b3314b"> <tr> <td align="left" width="250"><font face="arial" size="2" color="white"><b>Supplement</b></font></td> <td align="right" width="120"><font face="arial" size="2" color="white"><b>Line Total</b></font></td> </tr> <form action="view_cart.php" method="post"> TOP; // Print each item $total = 0; // total cost of the order while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { /* if ($row['specials_discount'] > 0) { $list_price_out = $row['price'] / $row['specials_discount']; } else { $list_price_out = $row['price']; } */ $list_price_out = $row['price']; $list_price_out = number_format($list_price_out, 2, '.', ''); // Calculate the total and subtotals $subtotal = $_SESSION['cart'][$row['prod_id']] * $list_price_out; $subtotal = number_format($subtotal, 2, '.', ''); $total += $subtotal; $total = number_format($total, 2, '.', ''); if ($_SESSION['cart'][$row['supp_id']] > 0) { // print the row print <<<ROW <tr> <td align="left"><font face="arial" size="2" color="white">{$row['supplement']}</font></td> <td align="center"> <input type="text" size="3" name="product[{$row['supp_id']}]" value="{$_SESSION['cart'][$row['supp_id']]}"></td> <td align="right"><font face="arial" size="2" color="white">R $list_price_out </font></td> <td align="right"><font face="arial" size="2" color="white">R $subtotal</font></td> </tr> ROW; }} // end of the WHILE loop // print the footer and close the table and the form print <<<FOOTER <tr> <td></td><td></td><td></td> <td align="right" valign="top"><font face="arial" size="2" color="white"><b>Total:</b></font></td> <td align="right" valign="top"><font face="arial" size="2" color="white"><b>R $total</b></font><br><br></td> </tr> </table> <div align="center"> <table width="260" align="center"> <tr><td width="130" align="right"><input type="image" name="submit" src="images/update_cart.jpg" border="0"></form></td><td width="130" align="left"><form action="checkout.php" method="post"><input type="image" name="checkout" src="images/checkout.jpg" border="0"></form> </td><td width="85"></td></tr></div> FOOTER; /* print <<<EMP <table border="0" width="580" align="center"> <tr> <td> <table summary="" width="300" border="1" style="border-color:#960232" bgcolor="#f8f3c2" align="center"> <tr> <td> <table border="0" width="300" cellpadding="3"> <tr><td align="center"><p><font face="arial" size="2" color="#5e5d5d">Your cart is empty!</font> </td></tr></table></table></table> EMP; */ ?> </table> </table> </body> </html> I am still getting the same mysql error Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804863 Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 You took it out of the WHILE loop again, and I think you meant you get the same foreach error. Use this: $supp_id_out = array(); while ($row_cl = mysql_fetch_assoc($result_cl)) { $supp_id_out[] = $row_cl["supp_id"]; $qty_out = $row_cl["qty"]; } foreach($supp_id_out as $key => $value) { $qty1 = $qty_out; $pid = $key; // add to the cart session variable $_SESSION['cart'][$pid] = $qty1; } Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804865 Share on other sites More sharing options...
lional Posted April 8, 2009 Author Share Posted April 8, 2009 I still get this error and this is my code <body bgcolor="#32818B" topmargin="0"> <?php session_start(); include 'header.php'; $client_out = $_SESSION['clid']; include 'includes/conn_db.php'; $query_cl = "SELECT * from client_supps WHERE client_id = '$client_out'"; $result_cl = mysql_query($query_cl, $conn); $supp_id_out = array(); while ($row_cl = mysql_fetch_assoc($result_cl)){ $supp_id_out[] = $row_cl["supp_id"]; $qty_out = $row_cl["qty"]; } foreach($supp_id_out as $key => $value) { $qty1 = $qty_out; $pid = $key; // add to the cart session variable $_SESSION['cart'][$pid] = $qty1; } // Display the cart if it is not empty // Retrieve all of the information for the products in the cart $query = 'SELECT * FROM supplements WHERE supp_id IN ('; foreach ($_SESSION['cart'] as $key => $value) { $query .= $key . ','; } $query .= substr ($query, 0, -1) . ') '; $result = mysql_query($query) or die(mysql_error()); // create a table and a form print <<<TOP <table width="790" align="center" bgcolor="#b3314b" cellpadding="0" cellspacing="0"> <tr><td height="450"> <table summary="" width="560" align="center" bgcolor="#b3314b"> <tr> <td nowrap align="center"><font face="arial" size="2" color="white">></font></tr></table> <table summary="" align="center" width="560" border="1"> <tr> <td> <table border="0" width="560" cellpadding="3"bgcolor="#b3314b"> <tr> <td align="left" width="250"><font face="arial" size="2" color="white"><b>Supplement</b></font></td> <td align="right" width="120"><font face="arial" size="2" color="white"><b>Line Total</b></font></td> </tr> <form action="view_cart.php" method="post"> TOP; // Print each item $total = 0; // total cost of the order while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { /* if ($row['specials_discount'] > 0) { $list_price_out = $row['price'] / $row['specials_discount']; } else { $list_price_out = $row['price']; } */ $list_price_out = $row['price']; $list_price_out = number_format($list_price_out, 2, '.', ''); // Calculate the total and subtotals $subtotal = $_SESSION['cart'][$row['prod_id']] * $list_price_out; $subtotal = number_format($subtotal, 2, '.', ''); $total += $subtotal; $total = number_format($total, 2, '.', ''); if ($_SESSION['cart'][$row['supp_id']] > 0) { // print the row print <<<ROW <tr> <td align="left"><font face="arial" size="2" color="white">{$row['supplement']}</font></td> <td align="center"> <input type="text" size="3" name="product[{$row['supp_id']}]" value="{$_SESSION['cart'][$row['supp_id']]}"></td> <td align="right"><font face="arial" size="2" color="white">R $list_price_out </font></td> <td align="right"><font face="arial" size="2" color="white">R $subtotal</font></td> </tr> ROW; }} // end of the WHILE loop // print the footer and close the table and the form print <<<FOOTER <tr> <td></td><td></td><td></td> <td align="right" valign="top"><font face="arial" size="2" color="white"><b>Total:</b></font></td> <td align="right" valign="top"><font face="arial" size="2" color="white"><b>R $total</b></font><br><br></td> </tr> </table> <div align="center"> <table width="260" align="center"> <tr><td width="130" align="right"><input type="image" name="submit" src="images/update_cart.jpg" border="0"></form></td><td width="130" align="left"><form action="checkout.php" method="post"><input type="image" name="checkout" src="images/checkout.jpg" border="0"></form> </td><td width="85"></td></tr></div> FOOTER; /* print <<<EMP <table border="0" width="580" align="center"> <tr> <td> <table summary="" width="300" border="1" style="border-color:#960232" bgcolor="#f8f3c2" align="center"> <tr> <td> <table border="0" width="300" cellpadding="3"> <tr><td align="center"><p><font face="arial" size="2" color="#5e5d5d">Your cart is empty!</font> </td></tr></table></table></table> EMP; */ ?> </table> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804883 Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 You should be using double quotes for your queries or you variables won't interpolate. Try this, and next time post the exact error message. $query = "SELECT * FROM supplements WHERE supp_id IN ('; foreach ($_SESSION['cart'] as $key => $value) { $query .= $key . ','; } $query .= substr ($query, 0, -1) . ') "; Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804885 Share on other sites More sharing options...
lional Posted April 8, 2009 Author Share Posted April 8, 2009 I now get Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in D:\htdocs\metabolic-typing\order_supplements.php on line 31 and this line is the foreach in the code below $query = "SELECT * FROM supplements WHERE supp_id IN ('; foreach ($_SESSION['cart'] as $key => $value) { $query .= $key . ','; } $query .= substr ($query, 0, -1) . ') "; $result = mysql_query($query) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804907 Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 The string weren't terminating properly. Change both of these lines: $query = "SELECT * FROM supplements WHERE supp_id IN ('"; $query .= substr ($query, 0, -1) . "') "; Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804921 Share on other sites More sharing options...
lional Posted April 8, 2009 Author Share Posted April 8, 2009 I get this when I run the script You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '59,41,25,22,,0,1,2,3')' at line 1 My code is <body bgcolor="#32818B" topmargin="0"> <?php session_start(); include 'header.php'; $client_out = $_SESSION['clid']; include 'includes/conn_db.php'; $query_cl = "SELECT * from client_supps WHERE client_id = '$client_out'"; $result_cl = mysql_query($query_cl, $conn); $supp_id_out = array(); while ($row_cl = mysql_fetch_assoc($result_cl)){ $supp_id_out[] = $row_cl["supp_id"]; $qty_out = $row_cl["qty"]; } foreach($supp_id_out as $key => $value) { $qty1 = $qty_out; $pid = $key; // add to the cart session variable $_SESSION['cart'][$pid] = $qty1; } // Display the cart if it is not empty // Retrieve all of the information for the products in the cart $query = "SELECT * FROM supplements WHERE supp_id IN ('"; foreach ($_SESSION['cart'] as $key => $value) { $query .= $key . ','; } $query .= substr ($query, 0, -1) . "') "; $result = mysql_query($query) or die(mysql_error()); // create a table and a form print <<<TOP <table width="790" align="center" bgcolor="#b3314b" cellpadding="0" cellspacing="0"> <tr><td height="450"> <table summary="" width="560" align="center" bgcolor="#b3314b"> <tr> <td nowrap align="center"><font face="arial" size="2" color="white">></font></tr></table> <table summary="" align="center" width="560" border="1"> <tr> <td> <table border="0" width="560" cellpadding="3"bgcolor="#b3314b"> <tr> <td align="left" width="250"><font face="arial" size="2" color="white"><b>Supplement</b></font></td> <td align="right" width="120"><font face="arial" size="2" color="white"><b>Line Total</b></font></td> </tr> <form action="view_cart.php" method="post"> TOP; // Print each item $total = 0; // total cost of the order while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { /* if ($row['specials_discount'] > 0) { $list_price_out = $row['price'] / $row['specials_discount']; } else { $list_price_out = $row['price']; } */ $list_price_out = $row['price']; $list_price_out = number_format($list_price_out, 2, '.', ''); // Calculate the total and subtotals $subtotal = $_SESSION['cart'][$row['prod_id']] * $list_price_out; $subtotal = number_format($subtotal, 2, '.', ''); $total += $subtotal; $total = number_format($total, 2, '.', ''); if ($_SESSION['cart'][$row['supp_id']] > 0) { // print the row print <<<ROW <tr> <td align="left"><font face="arial" size="2" color="white">{$row['supplement']}</font></td> <td align="center"> <input type="text" size="3" name="product[{$row['supp_id']}]" value="{$_SESSION['cart'][$row['supp_id']]}"></td> <td align="right"><font face="arial" size="2" color="white">R $list_price_out </font></td> <td align="right"><font face="arial" size="2" color="white">R $subtotal</font></td> </tr> ROW; }} // end of the WHILE loop // print the footer and close the table and the form print <<<FOOTER <tr> <td></td><td></td><td></td> <td align="right" valign="top"><font face="arial" size="2" color="white"><b>Total:</b></font></td> <td align="right" valign="top"><font face="arial" size="2" color="white"><b>R $total</b></font><br><br></td> </tr> </table> <div align="center"> <table width="260" align="center"> <tr><td width="130" align="right"><input type="image" name="submit" src="images/update_cart.jpg" border="0"></form></td><td width="130" align="left"><form action="checkout.php" method="post"><input type="image" name="checkout" src="images/checkout.jpg" border="0"></form> </td><td width="85"></td></tr></div> FOOTER; /* print <<<EMP <table border="0" width="580" align="center"> <tr> <td> <table summary="" width="300" border="1" style="border-color:#960232" bgcolor="#f8f3c2" align="center"> <tr> <td> <table border="0" width="300" cellpadding="3"> <tr><td align="center"><p><font face="arial" size="2" color="#5e5d5d">Your cart is empty!</font> </td></tr></table></table></table> EMP; */ ?> </table> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/153197-solved-pulling-product-list-from-database/#findComment-804934 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.