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
https://forums.phpfreaks.com/topic/79471-doesnt-work/
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
https://forums.phpfreaks.com/topic/79471-doesnt-work/#findComment-402410
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.