Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shopping Cart 1

Status
Not open for further replies.

LWolf

Programmer
Feb 3, 2008
77
US
I am trying to build a shopping cart into my application but all the applications I have found seem to be leaving out how the cart array works. I have found a ton of examples about product and adding, subtracting or emptying. But for someone learning php this is a little frustrating. Does anyone know a good tutorial about building a shopping cart?

Thanks.
K
 
Again, mysqli is different to mysql. You can't mix libraries.

if you are using mysql_fetch_row you need to be using mysql_connect (without the i) NOT the mysql[highlight #D3D7CF]i[/highlight]_connect function.

Conversely if you want to keep using mysql[highlight #D3D7CF]i[/highlight]_connect then use mysql[highlight #D3D7CF]i[/highlight]_fetch_row and other mysqli functions instead.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Remember I am a self study (but an old asp programmer). I was told to use pdo so I am trying to teach myself pdo. The reason I have both on the same page is because I am using someone else's code and modifying it. I figured changing the function to pdo was fine and the rest of the modified code would work, hence the mysql connection. I will drop the mysql and recode the line to pdo. Update to come...
 
recoded, the snip you posted above would look like this

Code:
foreach($_SESSION['cart'] as $product_id => $quantity) {	
    $sql = "
SELECT Vendor, Product, Aisle, Bay, Price 
FROM Products 
WHERE idProducts = ?";
    $s = $pdo->prepare($sql);
    if($s === false):
      print_r($pdo->errorInfo());
      die;
    endif;
    $result = $s->execute(array($product_id));
    if($result == =false): 
     print_r($s->errorInfo());
     die;
    endif;
					
    //Only display the row if there is a product (though there should always be as we have already checked)
    if(FALSE != ($row = $s->fetchObject())):
      $line_cost = $row->Price * $quantity;		//work out the line cost
      $total += $line_cost;			//add to the total cost
      echo "<tr>"; //show this information in table cells
      echo "<td align=\"center\">{$row->Product}</td>";
      //along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product
       echo "<td align=\"center\">$quantity <a href=\"$_SERVER[PHP_SELF]?action=remove&id=$product_id\">X</a></td>";
       echo "<td align=\"center\">$line_cost</td>";	
       echo "</tr>";
     endif;
endforeach;

note that you can also do the above as a single query rather than looping and hitting the database in each iteration. You may get performance benefits from doing so.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top