Home > Software design >  how to add message when record Not exists....?
how to add message when record Not exists....?

Time:12-13

This is my full codes of HTML PHP table page .. this is working, but when the not exists recode not display anything.. so I want to display message when record Not exists..

<!DOCTYPE html>
<html>
<body>

<table>
    <tr>
      <td>Subjects</td>
      <td>Grades</td>
    </tr>
<?php
    $i=0;
    while($row = mysqli_fetch_array($results_teach)) {
    if($row["Sub_1"] != "" && $row["Sub_Grades_1"] != "") { 
?>
<tr>
    <td><?php echo $row["Sub_1"]; ?></td>
    <td><?php echo $row["Sub_Grades_1"]; ?></td>
</tr>
<?php
      }
    $i  ;
    }
?>
</table>
</body>
</html> 

CodePudding user response:

You have a counter called $i. You can simply check its value:

<!DOCTYPE html>
<html>
<body>

<table>
    <tr>
      <td>Subjects</td>
      <td>Grades</td>
    </tr>
<?php
    $i=0;
    while($row = mysqli_fetch_array($results_teach)) {
    if($row["Sub_1"] != "" && $row["Sub_Grades_1"] != "") { 
?>
<tr>
    <td><?php echo $row["Sub_1"]; ?></td>
    <td><?php echo $row["Sub_Grades_1"]; ?></td>
</tr>
<?php
      }
    $i  ;
    }
?>
</table>
    <?php
    if ($i === 0) {
        ?>
            Does not exist
        <?php
    }
    ?>
</body>
</html> 

CodePudding user response:

You can check the count of the fetched data via mysqli_num_rows function. If no data is fetched the count will be zero and you can display an error message

if(mysqli_num_rows($results_teach) < 0){
   echo "The error message"
}else{
    $i=0;
    //code
}
  • Related