Jump to content

Doesn't Work


Dysan

Recommended Posts

Why doesn't the following code work.

 

I'm trying to test if an array exists, to insert data into it, else create the array. Only each time I access the page, the array contains no data. Why is this?

 

<?php
session_start();

$id = $_GET['id'];
$array = $_SESSION['ids'];
if (!isset($array))
{
$array = array();
}
else
{

$array[] = 'Data';
$_SESSION['ids'] = $array;
}


echo $_SESSION['ids'];

print_r($array);
?>

Link to comment
Share on other sites

You have problems with your logic.

 

I think this will do what you're trying to do:

<?php
session_start();
$id = (isset($_GET['id']))?$_GET['id']:'';
$array = (isset($_SESSION['ids']))?$_SESSION['ids']:array();
$array[] = $id;
$_SESSION['ids'] = $array;
echo '<pre>$_SESSION:' . print_r($_SESSION['ids'],true) . '</pre>';
echo '<pre>$array:' . print_r($array,true) . '</pre>';
?>

 

Ken

 

 

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.