Replace if loop is empty

Asked

Viewed 76 times

2

Good people, I have a question because I was a little "stuck" not knowing how to do it. I have the following code:

 <?
 foreach($data as $index => $row) { 
 $className = $index % 2 == 0 ? "class" : "class-1" ?>

<form action="" name="Insert" method="post">
 <div class="selector">
  <input name="selector[]" class="selector" type="checkbox" value="<?php echo $row['ID']; ?>" />
 </div>

 <div id="add-bars" class="<? echo $className ?>">
  <img src="<? echo $row['image']; ?>.png" class="img-responsive"/>
  <h6>
   <? echo $row['Title']; ?>

   </div>
  </h6>
 </div>

<?
 }
 ?>

 <div class="btn-padding">
  <div class="btn-group dropup pull-right">
   <button type="button" class="btn btn-primary">Seleciona uma acção</button>
   <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
    <span class="caret"></span>
    <span class="sr-only">Toggle Dropdown</span>
   </button>
   <ul class="dropdown-menu dropdown-menu-right" role="menu">
    <li><a href="" class="selectall-button" onClick="return false;">Selecionar tudo / Nao selecionar</a></li>
    <li class="divider"></li>
    <li><a href="#" type="submit" class="delete-button" id="games-delete-button" onclick="javascript:document.InsertGame.submit();">Eliminar</a></li>
   </ul>
  </div>
 </div>
</form>

 <?
 $edittable=$_POST['selector'];
 $N = count($edittable);
 for($i=0; $i < $N; $i++)
 {
 $result = $db->prepare("DELETE FROM table WHERE ID= :ID");
 $result->bindParam(':ID', $edittable[$i]);
 $result->execute();
 }
 } 
?>

My question is.. When I have no data in the database how do I put a text on the page saying: no data and take out the options box?

  • You want to remove only the checkboxes?

  • I want you to take this div btn-padding and puts a text saying: sem dados

1 answer

3


For this just check whether the result of the query is empty or not, if it is, print that there are no records:

<?php

} 

if(empty($data)){
    echo 'SEM DADOS';
}else{
?>

<div class="btn-padding">
  <div class="btn-group dropup pull-right">
   <button type="button" class="btn btn-primary">Seleciona uma acção</button>
   <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
    <span class="caret"></span>
    <span class="sr-only">Toggle Dropdown</span>
   </button>
   <ul class="dropdown-menu dropdown-menu-right" role="menu">
    <li><a href="" class="selectall-button" onClick="return false;">Selecionar tudo / Nao selecionar</a></li>
    <li class="divider"></li>
    <li><a href="#" type="submit" class="delete-button" id="games-delete-button" onclick="javascript:document.InsertGame.submit();">Eliminar</a></li>
   </ul>
  </div>
 </div>
<?php } ?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.