Barand Posted October 19, 2012 Share Posted October 19, 2012 I just copied your code to run it. If you are only selecting a single product (WHERE id = $id), why are you trying to print 4 in a row? Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 19, 2012 Author Share Posted October 19, 2012 I just copied your code to run it. If you are only selecting a single product (WHERE id = $id), why are you trying to print 4 in a row? the script loops, and it pick up the products 1 by 1 but it's suppose to be sorted 4 on each row, if you understand what I mean? - Going out for lunsj, and will be back in a hour! -Thanks for helping me out Barand. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 19, 2012 Share Posted October 19, 2012 (edited) You need to fetch all the products in a single query (as Psycho told you). Try this <?php function cart() { foreach($_SESSION as $name => $value) { if ($value>0) { if (substr($name, 0, 5) =='cart_') { $ids[] = intval(substr($name, 5)); } } } $idlist = join (',', $ids); $get = mysql_query("SELECT product_id as id, name, price FROM products WHERE id IN ($idlist)"); $countxx=0; while ($get_row = mysql_fetch_assoc($get)) { $sub = $get_row['price']*$value; echo "<p style='width:15%; text-align:center; float:left;'>". $get_row['name'].' x'.$value.' * €'.number_format ($get_row['price'],2).' = €'.number_format($sub, 2). '<br />'. '<a href="cart.php?remove='.$id.'"><img src="subtract.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?add='.$id.'"><img src="pluss.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?delete='.$id.'"><img src="delete.png" border="0" style="height: 20px; width: 50px" ></a></p>'; $total += $sub; if (++$countxx % 3 == 0) echo "<div style='clear:both'></div>"; } echo "<div style='clear:both'></div>"; if (@$total==0) { echo '<div style="position:absolute; TOP:30px; LEFT:20px; WIDTH:200px; HEIGHT:100px" border="0">'. '"Your cart is empty."'. '</div>'; } else { echo '<div style="position:absolute; TOP:20px; LEFT:900px; WIDTH:200px; HEIGHT:100px" border="0">'. '<p>Total: €'.number_format ($total, 2).'</p>'. '</div>'; } } ?> Edited October 19, 2012 by Barand Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 19, 2012 Author Share Posted October 19, 2012 You need to fetch all the products in a single query (as Psycho told you). Try this <?php function cart() { foreach($_SESSION as $name => $value) { if ($value>0) { if (substr($name, 0, 5) =='cart_') { $ids[] = intval(substr($name, 5)); } } } $idlist = join (',', $ids); $get = mysql_query("SELECT product_id as id, name, price FROM products WHERE id IN ($idlist)"); $countxx=0; while ($get_row = mysql_fetch_assoc($get)) { $sub = $get_row['price']*$value; echo "<p style='width:15%; text-align:center; float:left;'>". $get_row['name'].' x'.$value.' * €'.number_format ($get_row['price'],2).' = €'.number_format($sub, 2). '<br />'. '<a href="cart.php?remove='.$id.'"><img src="subtract.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?add='.$id.'"><img src="pluss.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?delete='.$id.'"><img src="delete.png" border="0" style="height: 20px; width: 50px" ></a></p>'; $total += $sub; if (++$countxx % 3 == 0) echo "<div style='clear:both'></div>"; } echo "<div style='clear:both'></div>"; if (@$total==0) { echo '<div style="position:absolute; TOP:30px; LEFT:20px; WIDTH:200px; HEIGHT:100px" border="0">'. '"Your cart is empty."'. '</div>'; } else { echo '<div style="position:absolute; TOP:20px; LEFT:900px; WIDTH:200px; HEIGHT:100px" border="0">'. '<p>Total: €'.number_format ($total, 2).'</p>'. '</div>'; } } ?> Just used your code but hmm.. I see no difference at all Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 19, 2012 Author Share Posted October 19, 2012 (edited) Wait a few min, might have been my bad.. will check it out again. while ($get_row = mysql_fetch_assoc($get)) { Error Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\xampp\cart.php on line 138 Edited October 19, 2012 by hahaitwork Quote Link to comment Share on other sites More sharing options...
Barand Posted October 19, 2012 Share Posted October 19, 2012 (edited) Note that I altered the query columns to match my product table. Also need to amend the initial loop to pick up $values into the array as well as the ids Edited October 19, 2012 by Barand Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 19, 2012 Author Share Posted October 19, 2012 Note that I altered the query columns to match my product table. Also need to amend the initial loop to pick up $values into the array as well as the ids To be honest I'm not really sure what you want me to do ? - Sorry about me being a newbie, I just started with Php and this project is a bit over my league but I do understand more and more and I really want this finished, so thanks for bearing with me! Quote Link to comment Share on other sites More sharing options...
Barand Posted October 19, 2012 Share Posted October 19, 2012 try this function cart() { $ids = array(); foreach($_SESSION as $name => $value) { if ($value>0) { if (substr($name, 0, 5) =='cart_') { $ids[intval(substr($name, 5))] = $value; } } } $idlist = join (',', array_keys($ids)); $get = mysql_query("SELECT id, name, price FROM product WHERE product_id IN ($idlist)"); $countxx=0; while ($get_row = mysql_fetch_assoc($get)) { $sub = $get_row['price'] * $ids[$get_row['id']]; // get value from the array echo "<p style='width:15%; text-align:center; float:left;'>". $get_row['name'].' x'.$value.' * €'.number_format ($get_row['price'],2).' = €'.number_format($sub, 2). '<br />'. '<a href="cart.php?remove='.$id.'"><img src="subtract.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?add='.$id.'"><img src="pluss.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?delete='.$id.'"><img src="delete.png" border="0" style="height: 20px; width: 50px" ></a></p>'; $total += $sub; if (++$countxx % 4 == 0) echo "<div style='clear:both'></div>"; } echo "<div style='clear:both'></div>"; if ($total==0) { echo '<div style="position:absolute; TOP:30px; LEFT:20px; WIDTH:200px; HEIGHT:100px" border="0">'. '"Your cart is empty."'. '</div>'; } else { echo '<div style="position:absolute; TOP:20px; LEFT:900px; WIDTH:200px; HEIGHT:100px" border="0">'. '<p>Total: €'.number_format ($total, 2).'</p>'. '</div>'; } } Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 19, 2012 Author Share Posted October 19, 2012 try this function cart() { $ids = array(); foreach($_SESSION as $name => $value) { if ($value>0) { if (substr($name, 0, 5) =='cart_') { $ids[intval(substr($name, 5))] = $value; } } } $idlist = join (',', array_keys($ids)); $get = mysql_query("SELECT id, name, price FROM product WHERE product_id IN ($idlist)"); $countxx=0; while ($get_row = mysql_fetch_assoc($get)) { $sub = $get_row['price'] * $ids[$get_row['id']]; // get value from the array echo "<p style='width:15%; text-align:center; float:left;'>". $get_row['name'].' x'.$value.' * €'.number_format ($get_row['price'],2).' = €'.number_format($sub, 2). '<br />'. '<a href="cart.php?remove='.$id.'"><img src="subtract.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?add='.$id.'"><img src="pluss.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?delete='.$id.'"><img src="delete.png" border="0" style="height: 20px; width: 50px" ></a></p>'; $total += $sub; if (++$countxx % 4 == 0) echo "<div style='clear:both'></div>"; } echo "<div style='clear:both'></div>"; if ($total==0) { echo '<div style="position:absolute; TOP:30px; LEFT:20px; WIDTH:200px; HEIGHT:100px" border="0">'. '"Your cart is empty."'. '</div>'; } else { echo '<div style="position:absolute; TOP:20px; LEFT:900px; WIDTH:200px; HEIGHT:100px" border="0">'. '<p>Total: €'.number_format ($total, 2).'</p>'. '</div>'; } } Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\xampp\cart.php on line 139 line 139 = while ($get_row = mysql_fetch_assoc($get)) { Notice: Undefined variable: total in C:\xampp\htdocs\xampp\cart.php on line 153 line 153 = else { By the way, do u want my full code, if that makes it easier for you to see if there is any other reasons why it doesn't work? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 19, 2012 Share Posted October 19, 2012 I did warn you about my changing column names. Change this line $get = mysql_query("SELECT id, name, price FROM product WHERE product_id IN ($idlist)"); to $get = mysql_query("SELECT id, name, price FROM products WHERE id IN ($idlist)"); Put "$total = 0;" before the while loop to define the variable; Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 19, 2012 Author Share Posted October 19, 2012 I did warn you about my changing column names. Change this line $get = mysql_query("SELECT id, name, price FROM product WHERE product_id IN ($idlist)"); to $get = mysql_query("SELECT id, name, price FROM products WHERE id IN ($idlist)"); Put "$total = 0;" before the while loop to define the variable; ahh, thought you changed it back when you told me to try it.. my bad. Getting errors on this parts. Notice: Undefined variable: id in C:\xampp\htdocs\xampp\cart.php on line 145 Notice: Undefined variable: id in C:\xampp\htdocs\xampp\cart.php on line 146 Notice: Undefined variable: id in C:\xampp\htdocs\xampp\cart.php on line 147 Notice: Undefined variable: total in C:\xampp\htdocs\xampp\cart.php on line 148 '<a href="cart.php?remove='.$id.'"><img src="subtract.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?add='.$id.'"><img src="pluss.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?delete='.$id.'"><img src="delete.png" border="0" style="height: 20px; width: 50px" ></a></p>'; $total += $sub; I did a session destroy and then the error was line 140 and 154 Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\xampp\cart.php on line 140 Notice: Undefined variable: total in C:\xampp\htdocs\xampp\cart.php on line 154 Quote Link to comment Share on other sites More sharing options...
Barand Posted October 19, 2012 Share Posted October 19, 2012 Change those "$id" to "$get_row['id']". Did you put that "$total = 0" line in as suggested? Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 19, 2012 Author Share Posted October 19, 2012 (edited) Change those "$id" to "$get_row['id']". Did you put that "$total = 0" line in as suggested? Total = added '<a href="cart.php?remove='. "$get_row['id']".'"><img src="subtract.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?add='. "$get_row['id']".'"><img src="pluss.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?delete='."$get_row['id']".'"><img src="delete.png" border="0" style="height: 20px; width: 50px" ></a></p>'; $total += $sub; Is added And this is the new error. while ($get_row = mysql_fetch_assoc($get)) { Parse error: syntax error, unexpected 'while' (T_WHILE) in C:\xampp\htdocs\xampp\cart.php on line 141 Edited October 19, 2012 by hahaitwork Quote Link to comment Share on other sites More sharing options...
Barand Posted October 19, 2012 Share Posted October 19, 2012 Are you missing the semi-colon at end of line before the "while"? Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 19, 2012 Author Share Posted October 19, 2012 (edited) Are you missing the semi-colon at end of line before the "while"? while ($get_row = mysql_fetch_assoc($get)) { $sub = $get_row['price'] * $ids[$get_row['id']]; // get value from the array echo "<p style='width:15%; text-align:center; float:left;'>". $get_row['name'].' x'.$value.' * €'.number_format ($get_row['price'],2).' = €'.number_format($sub, 2). '<br />'. '<a href="cart.php?remove='. "$get_row['id']".'"><img src="subtract.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?add='. "$get_row['id']".'"><img src="pluss.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?delete='."$get_row['id']".'"><img src="delete.png" border="0" style="height: 20px; width: 50px" ></a></p>'; $total += $sub; if (++$countxx % 4 == 0) echo "<div style='clear:both'></div>"; } nope, it's there.. if i'm not totally lost ? >_< Edited October 19, 2012 by hahaitwork Quote Link to comment Share on other sites More sharing options...
Barand Posted October 20, 2012 Share Posted October 20, 2012 My data +----+-----------+--------+ | id | name | price | +----+-----------+--------+ | 1 | product a | 1.50 | | 2 | product b | 2.00 | | 3 | product c | 5.99 | | 4 | product d | 10.25 | | 5 | product e | 100.00 | | 6 | product f | 8.00 | | 7 | product g | 2.29 | | 8 | product h | 0.49 | +----+-----------+--------+ My session data $_SESSION['cart_1'] = 1; $_SESSION['cart_2'] = 3; $_SESSION['cart_4'] = 1; $_SESSION['cart_8'] = 1; $_SESSION['cart_6'] = 2; $_SESSION['cart_3'] = 1; My code function cart() { $ids = array(); foreach($_SESSION as $name => $value) { if ($value>0) { if (substr($name, 0, 5) =='cart_') { $ids[intval(substr($name, 5))] = $value; } } } $idlist = join (',', array_keys($ids)); $get = mysql_query("SELECT id, name, price FROM products WHERE id IN ($idlist)"); if (!$get) die(mysql_error() . "<pre>$idlist</pre>"); $countxx=0; $total = 0; while ($get_row = mysql_fetch_assoc($get)) { $sub = $get_row['price'] * $ids[$get_row['id']]; // get value from the array echo "<p style='width:15%; text-align:center; float:left;'>". $get_row['name'].' x '.$ids[$get_row['id']].' * €'.number_format ($get_row['price'],2).' = €'.number_format($sub, 2). '<br />'. '<a href="cart.php?remove='.$get_row['id'].'"><img src="subtract.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?add='.$get_row['id'].'"><img src="pluss.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?delete='.$get_row['id'].'"><img src="delete.png" border="0" style="height: 20px; width: 50px" ></a></p>'; $total += $sub; if (++$countxx % 4 == 0) echo "<div style='clear:both'></div>"; } echo "<div style='clear:both'></div>"; if ($total==0) { echo '<div style="position:absolute; TOP:30px; LEFT:20px; WIDTH:200px; HEIGHT:100px" border="0">'. '"Your cart is empty."'. '</div>'; } else { echo '<div style="position:absolute; TOP:20px; LEFT:900px; WIDTH:200px; HEIGHT:100px" border="0">'. '<p>Total: €'.number_format ($total, 2).'</p>'. '</div>'; } } output attached Quote Link to comment Share on other sites More sharing options...
Christian F. Posted October 20, 2012 Share Posted October 20, 2012 (edited) Before, not after... Also, please use the [code][/code] tags around your code, not the quote tags. Edited October 20, 2012 by Christian F. Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 22, 2012 Author Share Posted October 22, 2012 My data +----+-----------+--------+ | id | name | price | +----+-----------+--------+ | 1 | product a | 1.50 | | 2 | product b | 2.00 | | 3 | product c | 5.99 | | 4 | product d | 10.25 | | 5 | product e | 100.00 | | 6 | product f | 8.00 | | 7 | product g | 2.29 | | 8 | product h | 0.49 | +----+-----------+--------+ My session data $_SESSION['cart_1'] = 1; $_SESSION['cart_2'] = 3; $_SESSION['cart_4'] = 1; $_SESSION['cart_8'] = 1; $_SESSION['cart_6'] = 2; $_SESSION['cart_3'] = 1; My code function cart() { $ids = array(); foreach($_SESSION as $name => $value) { if ($value>0) { if (substr($name, 0, 5) =='cart_') { $ids[intval(substr($name, 5))] = $value; } } } $idlist = join (',', array_keys($ids)); $get = mysql_query("SELECT id, name, price FROM products WHERE id IN ($idlist)"); if (!$get) die(mysql_error() . "<pre>$idlist</pre>"); $countxx=0; $total = 0; while ($get_row = mysql_fetch_assoc($get)) { $sub = $get_row['price'] * $ids[$get_row['id']]; // get value from the array echo "<p style='width:15%; text-align:center; float:left;'>". $get_row['name'].' x '.$ids[$get_row['id']].' * €'.number_format ($get_row['price'],2).' = €'.number_format($sub, 2). '<br />'. '<a href="cart.php?remove='.$get_row['id'].'"><img src="subtract.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?add='.$get_row['id'].'"><img src="pluss.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?delete='.$get_row['id'].'"><img src="delete.png" border="0" style="height: 20px; width: 50px" ></a></p>'; $total += $sub; if (++$countxx % 4 == 0) echo "<div style='clear:both'></div>"; } echo "<div style='clear:both'></div>"; if ($total==0) { echo '<div style="position:absolute; TOP:30px; LEFT:20px; WIDTH:200px; HEIGHT:100px" border="0">'. '"Your cart is empty."'. '</div>'; } else { echo '<div style="position:absolute; TOP:20px; LEFT:900px; WIDTH:200px; HEIGHT:100px" border="0">'. '<p>Total: €'.number_format ($total, 2).'</p>'. '</div>'; } } output attached It works like a dream, will check it out some more before I say if it's finished or not thx again Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 22, 2012 Author Share Posted October 22, 2012 (edited) My data +----+-----------+--------+ | id | name | price | +----+-----------+--------+ | 1 | product a | 1.50 | | 2 | product b | 2.00 | | 3 | product c | 5.99 | | 4 | product d | 10.25 | | 5 | product e | 100.00 | | 6 | product f | 8.00 | | 7 | product g | 2.29 | | 8 | product h | 0.49 | +----+-----------+--------+ My session data $_SESSION['cart_1'] = 1; $_SESSION['cart_2'] = 3; $_SESSION['cart_4'] = 1; $_SESSION['cart_8'] = 1; $_SESSION['cart_6'] = 2; $_SESSION['cart_3'] = 1; My code function cart() { $ids = array(); foreach($_SESSION as $name => $value) { if ($value>0) { if (substr($name, 0, 5) =='cart_') { $ids[intval(substr($name, 5))] = $value; } } } $idlist = join (',', array_keys($ids)); $get = mysql_query("SELECT id, name, price FROM products WHERE id IN ($idlist)"); if (!$get) die(mysql_error() . "<pre>$idlist</pre>"); $countxx=0; $total = 0; while ($get_row = mysql_fetch_assoc($get)) { $sub = $get_row['price'] * $ids[$get_row['id']]; // get value from the array echo "<p style='width:15%; text-align:center; float:left;'>". $get_row['name'].' x '.$ids[$get_row['id']].' * €'.number_format ($get_row['price'],2).' = €'.number_format($sub, 2). '<br />'. '<a href="cart.php?remove='.$get_row['id'].'"><img src="subtract.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?add='.$get_row['id'].'"><img src="pluss.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?delete='.$get_row['id'].'"><img src="delete.png" border="0" style="height: 20px; width: 50px" ></a></p>'; $total += $sub; if (++$countxx % 4 == 0) echo "<div style='clear:both'></div>"; } echo "<div style='clear:both'></div>"; if ($total==0) { echo '<div style="position:absolute; TOP:30px; LEFT:20px; WIDTH:200px; HEIGHT:100px" border="0">'. '"Your cart is empty."'. '</div>'; } else { echo '<div style="position:absolute; TOP:20px; LEFT:900px; WIDTH:200px; HEIGHT:100px" border="0">'. '<p>Total: €'.number_format ($total, 2).'</p>'. '</div>'; } } output attached Okey.. when I removed all the items in my session, I got this error: 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 ')' at line 1 I got problems finding out where the mistake is.. I was also woundering that if I want another table to be loaded etc into my cart, is it only to copy my product code and paste in the name to the table into places like: The table name is at this moment "products" and "others" , can i just add others behind "FROM products,others" or so? The tables got the same names, id quantity etc.. but I will change the ID number of course so they don't match with each other $result = mysql_query("SELECT * FROM products"); $quantity = mysql_query ('SELECT id, quantity FROM products WHERE id=' .mysql_real_escape_string ((int) $_GET ['add'])); Edited October 22, 2012 by hahaitwork Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 22, 2012 Author Share Posted October 22, 2012 (edited) Okey.. when I removed all the items in my session, I got this error: 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 ')' at line 1 I got problems finding out where the mistake is.. I was also woundering that if I want another table to be loaded etc into my cart, is it only to copy my product code and paste in the name to the table into places like: The table name is at this moment "products" and "others" , can i just add others behind "FROM products,others" or so? The tables got the same names, id quantity etc.. but I will change the ID number of course so they don't match with each other $result = mysql_query("SELECT * FROM products"); $quantity = mysql_query ('SELECT id, quantity FROM products WHERE id=' .mysql_real_escape_string ((int) $_GET ['add'])); By the way.. I'm sure the mistake is in the cart script as if I remove it, the error fades (from index file) It have something with my session to do as well maybe.. Pretty sure it has something with this line to do: $get = mysql_query("SELECT id, name, price FROM products WHERE id IN ($idlist)"); WHERE id IN <------ Edited October 22, 2012 by hahaitwork Quote Link to comment Share on other sites More sharing options...
Barand Posted October 22, 2012 Share Posted October 22, 2012 if there are no items in the SESSION then $total = 0 Skip other processing in this case and show "Cart is empty" message Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 22, 2012 Author Share Posted October 22, 2012 if there are no items in the SESSION then $total = 0 Skip other processing in this case and show "Cart is empty" message But shouldn't the message be shown then ? if ($total==0) { echo '<div style="position:absolute; TOP:30px; LEFT:20px; WIDTH:200px; HEIGHT:100px" border="0">'. '"Your cart is empty"'. '</div>'; } Quote Link to comment Share on other sites More sharing options...
Barand Posted October 22, 2012 Share Posted October 22, 2012 If there are no items in the SESSION there is no point in processing the items. I have rearranged the code for you function cart() { $ids = array(); if (!empty($_SESSION)) { foreach($_SESSION as $name => $value) { if ($value>0) { if (substr($name, 0, 5) =='cart_') { $ids[intval(substr($name, 5))] = $value; } } } } if (!empty($ids)) { $idlist = join (',', array_keys($ids)); $get = mysql_query("SELECT id, name, price FROM products WHERE id IN ($idlist)"); if (!$get) die(mysql_error() . "<pre>$idlist</pre>"); $countxx=0; $total = 0; while ($get_row = mysql_fetch_assoc($get)) { $sub = $get_row['price'] * $ids[$get_row['id']]; // get value from the array echo "<p style='width:15%; text-align:center; float:left;'>". $get_row['name'].' x '.$ids[$get_row['id']].' * €'.number_format ($get_row['price'],2).' = €'.number_format($sub, 2). '<br />'. '<a href="cart.php?remove='.$get_row['id'].'"><img src="subtract.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?add='.$get_row['id'].'"><img src="pluss.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?delete='.$get_row['id'].'"><img src="delete.png" border="0" style="height: 20px; width: 50px" ></a></p>'; $total += $sub; if (++$countxx % 4 == 0) echo "<div style='clear:both'></div>"; } echo "<div style='clear:both'></div>"; echo '<div style="position:absolute; TOP:20px; LEFT:900px; WIDTH:200px; HEIGHT:100px" border="0">'. '<p>Total: €'.number_format ($total, 2).'</p>'. '</div>'; } else { echo '<div style="position:absolute; TOP:30px; LEFT:20px; WIDTH:200px; HEIGHT:100px" border="0">'. '"Your cart is empty."'. '</div>'; } } Quote Link to comment Share on other sites More sharing options...
hahaitwork Posted October 22, 2012 Author Share Posted October 22, 2012 (edited) If there are no items in the SESSION there is no point in processing the items. I have rearranged the code for you function cart() { $ids = array(); if (!empty($_SESSION)) { foreach($_SESSION as $name => $value) { if ($value>0) { if (substr($name, 0, 5) =='cart_') { $ids[intval(substr($name, 5))] = $value; } } } } if (!empty($ids)) { $idlist = join (',', array_keys($ids)); $get = mysql_query("SELECT id, name, price FROM products WHERE id IN ($idlist)"); if (!$get) die(mysql_error() . "<pre>$idlist</pre>"); $countxx=0; $total = 0; while ($get_row = mysql_fetch_assoc($get)) { $sub = $get_row['price'] * $ids[$get_row['id']]; // get value from the array echo "<p style='width:15%; text-align:center; float:left;'>". $get_row['name'].' x '.$ids[$get_row['id']].' * €'.number_format ($get_row['price'],2).' = €'.number_format($sub, 2). '<br />'. '<a href="cart.php?remove='.$get_row['id'].'"><img src="subtract.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?add='.$get_row['id'].'"><img src="pluss.png" border="0" style="height: 20px; width: 35px" ></a> <a href="cart.php?delete='.$get_row['id'].'"><img src="delete.png" border="0" style="height: 20px; width: 50px" ></a></p>'; $total += $sub; if (++$countxx % 4 == 0) echo "<div style='clear:both'></div>"; } echo "<div style='clear:both'></div>"; echo '<div style="position:absolute; TOP:20px; LEFT:900px; WIDTH:200px; HEIGHT:100px" border="0">'. '<p>Total: €'.number_format ($total, 2).'</p>'. '</div>'; } else { echo '<div style="position:absolute; TOP:30px; LEFT:20px; WIDTH:200px; HEIGHT:100px" border="0">'. '"Your cart is empty."'. '</div>'; } } Worked Thanks for the help so far Barand , you sure got some skills when it comes to coding Taking this topic as solved and hope to see you again later if I get into trouble Edited October 22, 2012 by hahaitwork Quote Link to comment Share on other sites More sharing options...
Interista Posted October 22, 2012 Share Posted October 22, 2012 Sorry for reply in this topic .. But I would to know how I can set sessions in products page that contains cart info . Quote Link to comment 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.