php - validate if table row has data in every fields

I want to validate if a current row in every field has data on it and if it has not then display no data. Please help me. Also it is advisable to use echo in displaying the table data or should i stick to the html. Thanks in advance! I've been searching the net and was not able to find some answers. lol
$ct_list = mysqli_query($conn,"select * from ct LEFT JOIN station on ct.station_id=station.station_id LEFT JOIN dilg_emp on dilg_emp.station_id=station.station_id where dilg_emp.dilg_emp_id= $session_id");
if (mysqli_num_rows($ct_list) > 0){
while ($ct_row_list = mysqli_fetch_array($ct_list)) {
$ct_FName = $ct_row_list['ct_FName'];
$ct_MName = $ct_row_list['ct_MName'];
$ct_LName = $ct_row_list['ct_LName'];
$ct_SName = $ct_row_list['ct_SName'];
$ct_address = $ct_row_list['ct_address'];
$ct_birthday = $ct_row_list['ct_birthday'];
$ct_age = $ct_row_list['ct_age'];
$ct_gender = $ct_row_list['ct_gender'];
$ct_level_educ = $ct_row_list['ct_level_educ'];
$ct_course = $ct_row_list['ct_course'];
$ct_school = $ct_row_list['ct_school'];
$ct_prev_covid = $ct_row_list['ct_prev_covid'];
$ct_training_date = $ct_row_list['ct_training_date'];
$ct_deployment_date = $ct_row_list['ct_deployment_date'];
$ct_prev_emp = $ct_row_list['ct_prev_emp'];
echo "<tr>
<td>".
$x++."
</td>
<td>
$ct_FName $ct_MName $ct_LName $ct_SName
</td>
<td>
$ct_address
</td>
<td>
$ct_birthday
</td>
<td>
$ct_age
</td>
<td>
$ct_gender
</td>
<td>
$ct_level_educ
</td>
<td>
$ct_course
</td>
<td>
$ct_school
</td>
<td>
$ct_prev_covid
</td>
<td>
$ct_training_date
</td>
<td>
$ct_deployment_date
</td>
<td>
$ct_prev_emp
</td>
</tr>";
}
}else{
echo "<tr><td><b>Nothing to display</b></td></tr>";
}
Answer
Solution:
You can loop over the row array, replacing empty values withNo data
.
$optional_fields = ["ct_SName"];
while ($ct_row_list = mysqli_fetch_array($ct_list)) {
foreach ($ct_row_list as $name => &$value) {
if (in_array($name, $optional_fields)) {
continue;
} elseif (empty($value)) {
$value = "No data";
}
}
// rest of your code goes here
Answer
Solution:
It's hard to tell exactly what you mean, if a row has any empty columns then skip that row?
If you select 15 columns in the query or if you know thatSELECT *
will return 15 columns, then filter out the empty ones and see if there are 15:
if (mysqli_num_rows($ct_list) > 0) {
while ($ct_row_list = mysqli_fetch_array($ct_list)) {
if (count(array_filter($ct_row)) == 15) {
echo "<tr>
<td>$x++</td>
<td>$ct_FName $ct_MName $ct_LName $ct_SName</td>"; //etc...
}
}
}
You could also check and skip:
if (count(array_filter($ct_row)) < 15) {
continue;
}
//echo your HTML
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: use the option --with-all-dependencies (-w) to allow upgrades, downgrades and removals for packages currently locked to specific versions.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.