I have a system where there is a list of items. each has these three fields
id - unique to each item. The index field for the db
name - not unique
date - not unique
I just started using arrays, and am not that proficient. Also, I haven't worked on this for about three to four days, so I can't remember why i did one of these things. I will refer to that later.
So, this array will be opened up on start of the page and set to a session variable. call it array1.
this isn't reflected in the code below, but I will pull a list of items (list1) with the same fields above, reference them againgst the array1 above. Only the ones not in array1 will be displayed.
Each of these will be their own individual forms. when a user clicks on one of the items in this list, it will refresh the page, and add it to array1 which I will use to create list2. Now, I don't want to create duplicate iterations of the same item in array1, which could happen if they hit the refresh button.
So, I have this code below to try an prevent it. But what I get is weird. with 1 item in the list1, I get two iterations can click up two times and get results in list2. I don't know if it is because it is in array1 twice, or if it is going through the foreach function twice.
But to add even more confusion, if I have two items in list2, I can get a couple of different results by refreshing and clicking different combinations of the two items.
I hope that I have described everything properly. The part I can't remember why I created it that way is the structure of the array. And yes, I know I should create a function for adding to the array and call it from the two instances. I am just to impatient right now to get a working model. I will fix that later.
Also, I had put the id in both levels of the array in order to get the results I wanted. If it's not needed, I will remove it.
$array = $_SESSION['array'];
if (isset($_POST['id'])){
if (isset($_SESSION['array'])){
$exists = array_key_exists($_POST['id'], $array);
if ($exists == TRUE){
}else{
$array[] = array($_POST['id'] => array(
"name" => $_POST['name'],
"id" => $_POST['id'],
"date" => $_POST['date']
)
);
$_SESSION['array'] = $array;
}
}else{
$array[] = array($_POST['id'] => array( "name" => $_POST['name'],
"amount" => $_POST['amount'],
"id" => $_POST['id'],
"date" => $_POST['date']
)
);
$_SESSION['array'] = $array;
}
}
//this creates the display down below
if(count($array) > 0) {
foreach ($array as $array1) {
foreach ($array1 as $key=>$array2)
$display .= "<tr>
<td>" . $array2['id'] . "</td>" .
<td>" . $array2['name'] . "</td>" .
"<td>" . $array2['budget'] . "</td>" .
"<td>" . $array2['date'] . "</td>";
}
}
i think that when I was creating the array I was looking at it from this stand point. I need to search the array to see if the id is present. So, I wanted to set the key as the id. but, I don't think I understand the 'key' concept, because if I went with a single dimension array, wouldn't all of my variables be keys? I need to store the name and date as it pertains to the id. So, I created a multidimensional array to have the id as the key in the 1st dimension, then assign an array to each one of those. each of those arrays would have the name and date in them.
Can anyone help me sort this out? Thanks