Hi everyone, so I have the below piece of code that I have developed, and I’m struggling to figure this out. What I need to make it do is save each clicked date seperately and uniquely so later it can be inserted into a mysql database table. I have tried using sessions, but still couldn’t figure out a way to create a unique session variable that carries that date. Bear in mind that there wont just be 10 boxes, there will be a calendar with endless days.
The part of the php array documentation that you should look at is the -‘Creating/modifying with square bracket syntax’. This will allow you to append the submitted dates to an array. The only value that your form should submit is the full Y-m-d chosen date (using a hidden field.) This will avoid ambiguity should anyone need to select dates that span more than a single month.
BTW - your $date value only works because you are testing during a month that has two digits in it. Your current code won’t work for months 1 - 9. You would need to insure that any date value has leading zeros for single digit values for both the month and day parts.
Variable and first-level $_SESSION associative array index names should indicate the meaning/purpose of the data in the variable. Your session index name should be something like ‘dates’ or ‘choosen_dates’ (for those that prefer underscores over camelCase.)
Your post method form processing code should be located above the start of the html document, since it is creating/modifying data on the server. You would then use the contents of the session variable at the appropriate point in the html document.
I have read and reread this and I don’t see how it ‘appends’.
Creating/modifying with square bracket syntax’. This will allow you to append the submitted dates to an array.
I get what you mean about the date numbers 1-9 not having the leading zero.
I cannot put the form anywhere else as each form refers to the specific number that it is on and is being generated within the while loop and the user needs to click on the box to send it.
Can no one offer up some code? I am really battling to understand everyone’s textual suggestions. I still have no clue how to add new session variables using an array. i have no idea how to implement anything like that into my code above. I have tried for 2 days now. I know I’m just not gonna get it without some extreme help at this point.
This is my progress. It kind of works, but hopefully I don’t hit a snag. I managed to work out the form code into a for loop, which i am very happy about. I still don’t know how to get the elseif statements into a for loop.
I honestly don’t know how to do the array route that has been suggested above. It doesn’t make sense to me at all.
This is how square-bracket syntax appends to an array.
If you’re just storing dates where something has been selected, for example a booking for that date, you could use the date as the array index.
$dates[$dayselected] = true;
and that way you’d have an array, indexed by date, containing elements just for those dates that you’ve added. You could then assign that array to a session variable, if you wanted to. (In that code, $dayselected is the date value from your form, whether the user typed it in or picked it from a date-picker.)
I can’t see why that alters where you put the form-processing code within your script.
That’s very unlikely, it’s just a case of figuring out how.
Out of interest, why don’t you want to put it into a database table straight away, even if it’s stored as “pending”?
I stated that the post method form processing code should be above the start of the html document. Not the form code.
You don’t. If you find yourself writing out repeated logic that only differs in a value it operates on, it is a sign you are doing this the hardest way possible. You are not using the computer as a tool to accomplish a task.
Your goal is to record/remember a submitted set of one or more dates. Arrays are for sets of things. For the moment, we will leave handling duplicates out of the problem. The session storage part of the post method form processing code needs to create an array that looks like -
$_SESSION['dates'] = some submitted full date Y-m-d;
$_SESSION['dates'] = some other submitted full date Y-m-d;
At its simplest, the code to do that would be - $_SESSION['dates'] = $_POST['date'];
After the end of the form processing code, $_SESSION[‘dates’] is an array that will contain the submitted dates. You can use in_array() to find out if any particular date exists in the array.