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>
$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\$