Jump to content

Does anyone know a tutorial where this is explained??


28rain

Recommended Posts

Basically im newish to php and im having a go at making a cart, and i have already made the infastructure of the other tables. The cart tutorials are really unclear to me so does anybody know a decent tuorial or explanation to this line of code...

<a href="cart.php?action=add_item&id=<?php echo $row["itemId"]; ?>&qty=1">Add Item</a>

 

Thanks ???

Looks like when someones clicks on that link it sends them to the processing page (cart.php) where it's going to add the item that they selected ($_GET['id']) with the quantity of 1 ($_GET['qty']).

 

It's utilizing the GET method.

 

The link is generated dynamically, and the value for the URL parameter 'id' is generated from the results of the query to the database.

the question marks seperates the file from the variables being set via the url...cart.php is the same as cart.php?

 

the variables are set via $_GET['variable']....so the 'action' variable is set to Add_item...the &'s seperate each variable and what they're set to...the file(cart.php) pulls those variables out of the url and uses them in the script.

The skeleton of your script probably looks something like this.  So first let me explain how it's being generated.

 

$sql = "SELECT * FROM items";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
?>
&qty=1">Add Item
}
?>

 

Your script queries the database and grabs all of the unique ID's for the items and assigns 'id' to it.  The '?' and '&' characters are how you append these values to the URL.  '?' is used for the very first one, and any additional values you must use the '&' symbol.

 

If you go to the cart.php page, and look for 'add_item', which is the action you're taking when you get there, then you will see $_GET['id'] and $_GET['qty'] which will be used to add that item to the customers cart (session or DB) with the quantity of 1.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.