Jump to content

Call Function


Dysan

Recommended Posts

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

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

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

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.