This is a hard nut to crack. I've been trying to get this code of mine to stop acting strange for some time, but I can't seem to get it right.
I'm building an online tourist agency, and working at the tours management at the moment. At the tours page, the tour information is saved in different session arrays - one session array for which tour is selected, one for number of people of that same tour etc, etc... At the reservations page, I generate the saved tours through a php for-loop, checking the size of one of the session arrays (to find out how many tours are saved) and creating the correct number of tables.
Now by problem is at the remove button for the separate tours. Through the same for-loop, they're generated with names "removeTour0", "removeTour1", "removeTour2" etc... For some reason, with several tours saved, the same remove button can be used without problems only once! The information of the arrays IS removed, and the information of the other tours IS passed down to the next array element, but the last element of the array won't disappear! It simply stays as an empty element. This means, when I do the sizeof(), the same quantity of tours is still shown, since the array has the same quantity of elements.
An example: I have five tours saved. I remove the second tour - works fine. The third tour jumps down as "Tour #2", and a total of four tours are shown at the page. I press the same remove button again - the one for tour 2 - and the second tour (initially third) IS actually removed, the third tour (initially fourth) IS moved down to the place of #2, but table for FOUR tours still shows (though empty)! If I press the same button another time, it acts the same - tour removed, but session array size stays the same.
I understand the problem is at the part that manages the consequences of my button press, and also the rearranging of the session array indexes. Here's the code:
<?php
session_start();
$sessionSize = sizeof($_SESSION['tourID']);
echo "sessionSize = ". $sessionSize;
for ($tour = 0; $tour < $sessionSize; $tour++)
{
$stringButtonName = "removeTour" . $tour; //Creating string for the button name of the button that was pressed
if (ISSET($_POST[$stringButtonName])) //The user pressed the button to remove one of the tours
{
if ($sessionSize == 1) //If it was the last tour, destroy all sessions
{
session_unset();
session_destroy();
$sessionSize = 0;
}
else //It was NOT the last tour...
{
unset($_SESSION['tourID'][$tour]); //Emptying information of the selected tour
unset($_SESSION['tour_chosen'][$tour]);
unset($_SESSION['tour_regorpriv'][$tour]);
unset($_SESSION['tour_pax'][$tour]);
unset($_SESSION['boxlunch'][$tour]);
unset($_SESSION['boxlunch_veg'][$tour]);
unset($_SESSION['tour_dateY'][$tour]);
unset($_SESSION['tour_dateM'][$tour]);
unset($_SESSION['tour_dateD'][$tour]);
$_SESSION['tour_ID'] = array_values($_SESSION['tour_ID']); //Rearranging array indexes
$_SESSION['tour_chosen'] = array_values($_SESSION['tour_chosen']);
$_SESSION['tour_regorpriv'] = array_values($_SESSION['tour_regorpriv']);
$_SESSION['tour_pax'] = array_values($_SESSION['tour_pax']);
$_SESSION['boxlunch'] = array_values($_SESSION['boxlunch']);
$_SESSION['boxlunch_veg'] = array_values($_SESSION['boxlunch_veg']);
$_SESSION['tour_dateY'] = array_values($_SESSION['tour_dateY']);
$_SESSION['tour_dateM'] = array_values($_SESSION['tour_dateM']);
$_SESSION['tour_dateD'] = array_values($_SESSION['tour_dateD']);
$sessionSize = sizeof($_SESSION['tourID']); //Update size of array, i.e. quantity of tours
echo "sessionSize (new) = ". $sessionSize . "<br>";
}
}
}
?>
Does anyone have a clue? 😕