Dysan Posted November 23, 2007 Share Posted November 23, 2007 How do I call the writeShoppingCart function, if the user enters the .php file directly by typing the file path in the browser address bar? How do I call the writeShoppingCart function, upon the add case in the switch statment being called? <?php function writeShoppingCart() { } $action = $_GET['action']; switch ($action) { case 'add': break; } function displayShoppingCartItems() { } ?> Link to comment https://forums.phpfreaks.com/topic/78540-call-function/ Share on other sites More sharing options...
GingerRobot Posted November 23, 2007 Share Posted November 23, 2007 As you would any other fuction? Or have i misunderstood: <?php function writeShoppingCart() { } $action = $_GET['action']; switch ($action) { case 'add': writeShoppingCart(); break; } function displayShoppingCartItems() { } ?> Link to comment https://forums.phpfreaks.com/topic/78540-call-function/#findComment-397458 Share on other sites More sharing options...
binarymonkey Posted November 23, 2007 Share Posted November 23, 2007 Do you mean: <?php # functions must be declared before calling them function writeShoppingCart() { } function displayShoppingCartItems() { } $action = $_GET['action']; switch ($action) { case 'add': writeShoppingCart(); break; default: # the below is executed if action was not found by any other explicit case condition displayShoppingCartItems(); break; } ?> Link to comment https://forums.phpfreaks.com/topic/78540-call-function/#findComment-397465 Share on other sites More sharing options...
Dysan Posted November 23, 2007 Author Share Posted November 23, 2007 Sort Off. How do I do it how they've done it here: http://www.thewatchmakerproject.com/journal/276/building-a-simple-php-shopping-cart You simply click on the link, at it take you to the cart.php page, and displays whats in the shopping cart. # function writeShoppingCart() { $cart = $_SESSION['cart']; if (!$cart) { return '<p>You have no items in your shopping cart</p>'; } else { // Parse the cart session variable $items = explode(',',$cart); $s = (count($items) > 1) ? 's':''; return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>'; } } Link to comment https://forums.phpfreaks.com/topic/78540-call-function/#findComment-397476 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.