Reputation: 979
I have my database like this:
table: users
column: day1, day2, day3, day4, day5
row: null, 1, 1, 1, null
here is what im trying to do:
if($row['day1'] == null) {
$totalNullDays = '1' // '1' is for day1
}
if($row['day2'] == null) {
$totalNullDays = '2' // '2' is for day2
}
etc...
At the end, i want something like this in this format:
$totalNullDays = '1, 5'; // because day1 and day5 are null.
what is the best way to do this? thanks.
Upvotes: 0
Views: 30
Reputation: 146
I would do this way:
$arrTotalNullDays = array();
if($row['day1'] == null) {
$arrTotalNullDays[] = '1' // '1' is for day1
}
if($row['day2'] == null) {
$arrTotalNullDays[] = '2' // '2' is for day2
}
$totalNullDays = implode(', ', arrTotalNullDays);
Upvotes: 1
Reputation: 780869
Use an array, and then combine them with implode()
. And rather than repeat all the same code for each day, use a loop.
$nullDays = array();
for ($i = 1; $i <= 5; $i++) {
if ($row['day' . $i] === null) {
$nullDays[] = $i;
}
}
$totalNullDays = implode(', ', $nullDays);
Upvotes: 2