Jump to content

Not Working!


Dysan

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

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.