Jump to content

Display 4 Items Each Row


hahaitwork

Recommended Posts

  • Replies 50
  • Created
  • Last Reply

Top Posters In This Topic

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.

Link to comment
Share on other sites

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 by Barand
Link to comment
Share on other sites

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 :(

Link to comment
Share on other sites

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 by hahaitwork
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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>';
   }
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by hahaitwork
Link to comment
Share on other sites

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 by hahaitwork
Link to comment
Share on other sites

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

post-3105-0-80681800-1350725467_thumb.png

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by hahaitwork
Link to comment
Share on other sites

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 by hahaitwork
Link to comment
Share on other sites

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>';
    } 

Link to comment
Share on other sites

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>';
   }
}

Link to comment
Share on other sites

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 by hahaitwork
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.