Search code examples
phpif-statementis-empty

Trying to create an if statement after the while loop or something to do something else if the result is empty. Is this possible?


Trying to create an if statement after the while loop to do something else if the result is empty. Is this possible? I want to show different buttons if the result is empty.

<?php
while ($row = mysqli_fetch_array($result)){
?>

<tbody>
<form action = "create.php" method="POST">
<tr><td><input type="text" name="term" value="<?php echo $row['id']; ?>"       style="border: 0px solid #000000;" readonly></td>
<td><button type="submit" class="btn btn-primary" >Create</button> </td>
</form>
</tr><td><button type="back" class="btn btn-primary" onClick="location.href='index.php'" >Back</button> </td>
<td></td>
</form><tr>
</tr>
</tbody>

<?php
};
?>

Solution

  • Try this

    $row_count = mysqli_num_rows($result);
    if($row_count == 0){
    ?>
        <tbody>
        <form action = "create.php" method="POST">
        <tr><td><input type="text" name="term" value="<?php echo $row['id']; ?>"       style="border: 0px solid #000000;" readonly></td>
        <td><button type="submit" class="btn btn-primary" >Create</button> </td>
        </form>
        </tr><td><button type="back" class="btn btn-primary" onClick="location.href='index.php'" >Back</button> </td>
        <td></td>
        </form><tr>
        </tr>
        </tbody>
    <?php
    }
    else
    {
        <?php
        while ($row = mysqli_fetch_array($result)){
    
    
        //Do your action
    
    
    
        ?>
    
        <?php
        }
    }
    ?>