Replace if loop is empty

2

Well, I've got a question because I was kind of "stuck" without 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 do not have data in the database, how do I put a text on the page to say: no data and take out the checkbox?

    
asked by anonymous 24.04.2015 / 19:18

1 answer

3

To do this, just check if the query result is empty or not, if it prints, 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 } ?>
    
24.04.2015 / 19:26