Our organisation offers morning & afternoon ski tours at several different levels. Not all tour levels occur every session and sometimes there may be more than one tour at a given level at the same time. I want to display guest numbers by date, type of tour and time of day. I have no problem retrieving the numbers, my problem is the display. Where there is no data I can't figure out how to make it show a blank cell, or one with a 0 in it.
The image shows a sample of what I'm getting, which I modified a bit to show what I want.
Hopefully this will clarify my issue. On Mar 28 there were guests for all 5 types of tours in the morning and afternoon. On Mar 29 no one took the BlGrn Tour so its amID value of 2 does not exist in the database for that date (see below), nor does the pmID value of 1 for the Grn tour appear for that afternoon and all the cells with data get shifted to the left.
memberID amID pmID date guestsAM guestsPM statsID
191 5 5 28/03/2016 2 1 17322
192 4 3 28/03/2016 4 4 17323
52 0 1 28/03/2016 0 2 17324
408 5 4 28/03/2016 2 7 17325
370 0 2 28/03/2016 0 2 17326
624 3 3 28/03/2016 3 5 17327
480 1 2 28/03/2016 3 5 17329
571 4 0 28/03/2016 3 0 17330
427 3 0 28/03/2016 2 0 17331
528 2 0 28/03/2016 3 0 17332
13 4 2 29/03/2016 3 4 17333
179 5 0 29/03/2016 3 0 17334
147 1 0 29/03/2016 3 0 17335
571 0 4 29/03/2016 0 2 17336
426 0 3 29/03/2016 0 2 17338
100 0 3 29/03/2016 0 3 17339
165 3 0 29/03/2016 5 0 17340
480 5 0 29/03/2016 1 0 17341
427 1 5 29/03/2016 2 4 17342
613 3 0 29/03/2016 4 0 17343
This is the code which retrieves the morning data. If there is more than one tour of a given type at the same time this code successfully adds up the guest numbers for those tours.
$sql = "SELECT *, SUM(guestsAM) as guestsam FROM stats GROUP BY date, amID";
$mar_am_tour = mysqli_query($dbc,$sql) or die(mysqli_error());
This is one variant of the code that I have tried to display the data for one tour type. If amID occurs for that date it works like a charm. If not it simply moves what should be in the next cell into the empty space. (see the image)
while($row = mysqli_fetch_array($mar_am_tour)) {
if ($row[date] == '2016-03-29') {
if ($row[amID] == 1) {
echo '<td class="members">' . $row[guestsam] . '</td>';
} else {
if ($row[amID] == '') {
echo '<td class="members"></td>';
}
}
}
I've also tried replacing Srow[amID] with $row[guestsam] == 0 and $row[guestsam] == '' in the 'else' statement and reversing the order of the if statements. Replacing the if statement after the "else" with
echo '<td class="members"></td>';
results in an empty cell between each cell with data.
I think what I need is something along the lines of
if ($row[amID] == 1 does not exist for this date ) { echo '';
I found this
function is_blank($value) {
return empty($value) && !is_numeric($value);
}
on "How to implement is_empty" but I have no idea if it's what I need nor how to use it.
As mentioned by RST, check for value that $row[amID] has when the cell needs to be empty. Also, your code has trivial mistake
while($row = mysqli_fetch_array($mar_am_tour)) {
if ($row['date'] == '2016-03-29') {
if ($row['amID'] == 1) {
echo '<td class="members">' . $row['guestsam'] . '</td>';
} else {
if (!empty($row['amID'])) {
echo '<td class="members"></td>';
}
/*what if $row['amID'] != '' .
*Either put an else block here or remove the if block and just
*echo '<td class="members"></td>'
*/
}
}