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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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