Jump to content

Recommended Posts

Why doesn't the amount of values in the array get displayed, to reflect what in the array, upon calling the writeShoppingCart() function? - The amount stays at 0, Why is this?

 

<?php
session_start();

function writeShoppingCart()
{
echo count($array);
}

if(isset($_SESSION['ids']) && is_array($_SESSION['ids']))
{
    $array = $_SESSION['ids'];
}
else
{
    $array = array();
}


if (!in_array($_GET['id'], $array))
{
$array[] = $_GET['id'];
}
else
{
  echo "ID Exists";
}
$_SESSION['ids'] = $array;
writeShoppingCart();
print_r($array);
?>

Link to comment
https://forums.phpfreaks.com/topic/78469-not-working/
Share on other sites

I'm going to assume because you ended the function right after that first echo statement....? Is the error that when you call the function, you get "0" or the fact that the array is not being built?

 

Also, you need to pass the variable $array into the function in order for it to access it..

 

try adding a global:

 

function writeShoppingCart()
{
global $array;

Link to comment
https://forums.phpfreaks.com/topic/78469-not-working/#findComment-397072
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.