Log in

View Full Version : Calling functions



calumogg
12-21-2007, 06:32 PM
Hi, I have come across part of a shopping cart that I want to use, all I have is the functions the one below is just an example.

function delete_item($table, $session, $product) {
$query = "DELETE FROM $table WHERE session='$session' AND product='$product' ";
mysql_query($query);
}

But now I am stuck, I dont actually know how to use the functions! This is all the documentation that came with the code:

<?
include("shoppingcart.php");
$cart = new Cart;
$mysql_link = mysql_connect("localhost", "wwwrun", "");
$mysql_select_db("kmartShopper", $mysql_link) /* heh, use whatever database name you put the 2 tables under in place of kmartShopper */
?>

Please could someone post a example code that would use the delete_item function?

Thanks in advance for any help

djr33
12-21-2007, 06:54 PM
http://php-mysql-tutorial.com
read that... it'll help :)

You need to first connect to the database then choose the actual database name you are using.

After that, you can query the database for data.

The function is easy, just use, as the names of the variables say, the table name, the session name and the name of the product, and it should delete it.

delete_item('table', 'session', 'product');

calumogg
12-21-2007, 08:13 PM
I know what I need to use, but I will make a page that will display the contents of the cart and there will be a delete button that when a customer clicks the button it will delete the relevant item, but I dont know the code that I need to send the right data to the correct function bearing in mind that there are other functions as well.
Can I do it through by using something like: href=index.php?table=table1&session=1&product=product1

But then if I use the above example how would I tell the page to use thats data with the delete function and not some other function using the same data?

djr33
12-21-2007, 08:29 PM
Well, you can retrieve any of that data with $_GET['name']. So, $_GET['table'] would be table1.
Then just call delete_item($_GET['table',...);

One method would be using if isset($_GET[....., and that would check if you have a value to use.

Aside from that, I recommend any basic PHP tutorial which will show you typical if/else structures that should get your page functional.

You can't just "tell a page", so think about a way to send data, etc, that will be checked by the page.