What is a more efficient way of writing this sample code? Its purpose is to create a variable for days of the week based on a numerical value. This data is obtained from user input checkboxes. If the user doesn't select any options, they will be assigned the Online value. Fiddle here - https://3v4l.org/Sghnh#v7.4.30
<?php
$classDays = [1,3,5];
if($classDays[0] == '') {
$classDaysD = 'Online';
} else {
if (in_array(1, $classDays)) {
$classDaysD = 'M';
}
if (in_array(2,$classDays)) {
$classDaysD .= ' T';
}
if (in_array(3,$classDays)) {
$classDaysD .= ' W';
}
if (in_array(4,$classDays)) {
$classDaysD .= ' Th';
}
if (in_array(5,$classDays)) {
$classDaysD .= ' F';
}
if (in_array(6,$classDays)) {
$classDaysD .= ' S';
}
if (in_array(7,$classDays)) {
$classDaysD .= ' Su';
}
if ($classDaysD == '') {
$classDaysD = 'M & W';
}
}
echo $classDaysD;
Here is the checkbox section of the form, viewable here http://jsfiddle.net/S9qrZ/1/:
Which days of the week are your classes for the syllabi you are about to create?<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="classDays[]" name="classDays[]" value="1">
<label class="form-check-label" for="classDays[]">Mon</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="classDays[]" name="classDays[]" value="2">
<label class="form-check-label" for="classDays[]">Tues</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="classDays[]" name="classDays[]" value="3">
<label class="form-check-label" for="classDays[]">Wed</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="classDays[]" name="classDays[]" value="4">
<label class="form-check-label" for="classDays[]">Thur</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="classDays[]" name="classDays[]" value="5">
<label class="form-check-label" for="classDays[]">Fri</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="classDays[]" name="classDays[]" value="6">
<label class="form-check-label" for="classDays[]">Sat</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="classDays[]" name="classDays[]" value="7">
<label class="form-check-label" for="classDays[]">Sun</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="checkbox" id="classDays[]" name="classDays[]" value="Online" checked>
<label class="form-check-label" for="classDays[]">Online (uncheck if face to face class)</label>
</div>
1 Answer 1
I can't get clear answers to my question in the comments, but I think I can try something. It would seem logical to define an array that contains the days of the week, like this:
const WEEKDAYS = ['M', 'T', 'W', 'Th', 'F', 'S', 'Su'];
I have to use an array because you don't use 2 or 3 letters, but a mixture of 1 and 2 letter, abbreviations for the days of the week.
Now, instead of using day numbers in the checkboxes, and then translate this to alphabetical week days, I would just use alphabetical week days in the checkboxes, like this:
foreach (WEEKDAYS as $weekdayKey => $weekday) {
echo '<div class="form-check form-check-inline">' . PHP_EOL .
' <input class="form-check-input" type="checkbox" id="classDays' . $weekdayKey . '" name="classDays[]" value="' . $weekday . '">' . PHP_EOL .
' <label class="form-check-label" for="classDays[]">' . PHP_EOL .
' ' . date('l', strtotime('2022-01-0' . ($weekdayKey + 3))) . PHP_EOL .
' </label>' . PHP_EOL .
'</div>' . PHP_EOL;
}
See: https://3v4l.org/armtj#v7.4.30
Note that date('l', strtotime('2022-01-0' . ($weekdayKey + 3)))
just gets the day of the week, in your local language, based on $weekdayKey
from 0 to 6.
Now when I process the form, I already have, more or less, the correct data. But validating user input is always important. This processing could look something like this:
if (isset($_POST['classDays']) {
$classDaysD = implode(' ', array_intersect($_POST['classDays'], WEEKDAYS));
} else {
$classDaysD = 'Online';
}
The usage of array_intersect() is there to make sure that only values from WEEKDAYS
are accepted.
I would strongly advice you to choose another name for the $classDaysD
variable. Something like $classDays
would do, in my code.
I don't know how to incorporate the 'M & W'
result.
$classDays
, perhaps you should solve this problem first? It would also be helpful if you gave a better description of what the code is used for and what it is suppose to accomplish. We can guess, but should we? Why should$classDaysD = ''
result in'M & W'
and$classDaysD = ['']
in'Online'
? That seems very arbitrary. \$\endgroup\$$classDays
and$classDaysD
. This is due to the small difference between these names. Sorry, for that. Still, I would like to know what input and output you expect. How can this expression:($classDaysD == '')
, ever be true? When should it be true? \$\endgroup\$