php - Retrieve shopping cart items from database -
i'm trying implement shopping cart website. page loops through products in database , displays them on page 'add' button. can add item if logged in.
products table
| id | name | price | quantity | description | image_name |
once add item stores in cart table. 'cartproduct int' same 'id' in products table.
cart table
| cartid | cartproduct | productcount | uniqueid | username | date
if click add on same product, productcount increment.
so if testuser adds cartproduct 1 & 2 , 2 again.
i'm unsure on how display products username 'testuser' , need productcount * price.
at moment i'm trying loop through each cartproduct logged in user, select id products table id = cartproduct (as same). can grab price , details of product.
at moment selecting cart username = username logged in. can see outcome, displaying it.
$sql = "select cartproduct cart username ='" . $name . "'"; $result = mysql_query($sql); $c = 0; $n = 2; // each nth iteration new table row while ($db_field = mysql_fetch_assoc($result)) { $res = mysql_query($sql); $row = mysql_fetch_assoc($res); $productno = $row['cartproduct']; if ($c % $n == 0 && $c != 0) { // if $c divisible $n... echo '<div class="row" ></div>'; } $c++; echo "<br>count: " .$c; ?> <h2> <?php echo "product no: " . $productno ?> </a></h2> <?php
i add 3 items cart. cartproduct 1, 2, 2 :- output keep getting:
product no: 1
product no: 1
i should see product 1 , product 2?
am making more complicated should be, , tables correct?
there easier way?
many replies!
first, in cart table change username userid. next 1 step modify sql query @ "join products" connect products table , easly product price.
but in general, use pdo driver more safeties , clearlies queries , php code. read relations in db (rdbm).
Comments
Post a Comment