quarta-feira, 6 de novembro de 2019

usersListar.php

<?php
#usersListar.php
require_once("restrict.php");
require_once("connection.php");
$table="v1_users";
$sql="SELECT count(idUser) as total FROM $table";
$queryTotal=mysqli_query($connect, $sql);
$fetchTotal=mysqli_fetch_assoc($queryTotal);
$totalReg=$fetchTotal['total'];

if(!isset($_GET['prev']) or !isset($_GET['next'])){
$inicio=0;
}

if(isset($_GET['limite'])){
  $_SESSION['limite']=$_GET['limite'];
  $limite=$_SESSION['limite'];
}
if(!isset($_SESSION['limite'])){
 $limite=3;
}
if(!isset($_GET['limite']) and isset($_SESSION['limite'])){
 $limite=$_SESSION['limite'];
}

$totalPage=ceil($totalReg/$limite);

if(isset($_GET['next'])){
$inicio=$_GET['inicio'];
$inicio=$inicio+$limite;
}

if(isset($_GET['prev'])){
$inicio=$_GET['inicio'];
$inicio=$inicio-$limite;
}

if(isset($_GET['end'])){
$inicio=$totalReg-$limite;
}

$sql="SELECT * FROM $table LIMIT $inicio,$limite";
$query=mysqli_query($connect,$sql) or die("Erro na query SELECT * FROM $table");
$total=mysqli_num_rows($query);
if($total>0){
    $fetch=mysqli_fetch_assoc($query);
}
?>
    <!DOCTYPE html>
    <html lang="pt">

    <head>
        <meta charset="UTF-8">
        <title>Listagem de utilizadores</title>
    </head>

    <body>

        <?php if($total>0){ ?>
            <h1>Foram encontrados <?php echo $totalReg;?> registos.</h1>
            <form id="formLimite">
     <select name="limite" onchange="limitar()">
<option value="<?php echo $limite;?>"><?php echo $limite;?></option>
         <?php if($limite!=3){?>           
        <option value="3">3</option>
         <?php }?>
         <?php if($limite!=5){?>
        <option value="5">5</option>
        <?php }?>
        <?php if($limite!=10){?>
        <option value="10">10</option>
         <?php }?>
         <?php if($limite!=25){?>
        <option value="25">25</option>
                <?php }?>
                </select>
            </form>
                     
                        <table>
                <tr>
                    <th>#</th>
                    <th>Username</th>
                    <th>Nome</th>
                    <th>Apelido</th>
                    <th>Privilegio</th>
                    <th>Estado</th>
                    <th>Editar</th>
                    <th>Remover</th>
                </tr>
                <tr>
                    <td colspan="8">
                        <hr>
                    </td>
                </tr>
                <?php $i=$inicio; do { $i++;?>
                    <tr>
                        <td>
                            <?php echo $i;?>
                        </td>
                        <td>
                            <?php echo $fetch['username'];?>
                        </td>
                        <td>
                            <?php echo $fetch['nome'];?>
                        </td>
                        <td>
                            <?php echo $fetch['apelido'];?>
                        </td>
                        <td>
                            <?php echo $fetch['privilegio'];?>
                        </td>
                        <td>
                            <?php
$idUser=$fetch['idUser'];
if($fetch['estado']==1){
echo "<a style='color:Green' href='usersEstado.php?alterar&estado=0&idUser=$idUser'>Ativo</a>";}
else
    {
echo "<a style='color:red' href='usersEstado.php?alterar&estado=1&idUser=$idUser'>Inativo</a>";}
                           
                            ?>
                        </td>
                        <td><a href="usersEditar.php?idUser=<?php echo $idUser;?>">Editar</a></td>
                       
<td>
   <?php if($idUser==$_SESSION['idUser']){?>
    Remover<?php }?>
   
    <?php if($idUser!=$_SESSION['idUser']){?>
                  <a href="" onclick="remover(<?php echo $idUser;?>)">Remover</a>
                  <?php } ?>
                 
                   </td>
                    </tr>
                    <?php } while($fetch=mysqli_fetch_assoc($query));?>
<tr>
                    <td colspan="8">
                        <hr>
                    </td>
                </tr>
                <tr>
                    <td colspan="8">
<a<?php if($inicio>=$limite){?>
   href="?home"
   <?php }?> >Home</a>
                        <a<?php if($inicio>=$limite){?>
   href="?prev&inicio=<?php echo $inicio;?>"
   <?php }?>>Prev</a>
<a
<?php if($inicio<$totalReg-$limite){?>
href="?next&inicio=<?php echo $inicio;?>"
<?php }?>
>Next</a>
                     
<a
   <?php if($inicio<$totalReg-$limite){?>
href="?end"
   <?php }?>
>End</a>
                       
                    </td>
                </tr>
            </table>
            <?php } ?>

                <?php if($total==0){ ?>
                    <p>Não foram encontrados registos</p>
                    <?php } ?>
                   
                   
                    <script>
                        function remover(i) {
if (confirm("Apagar o registo ?!?!")) {window.open("usersRemover.php?remover&idUser="+i);                                                                      }
}

            function limitar() { document.getElementById("formLimite").submit();
            }
         </script>
                   
    </body>

    </html>

    <?php
mysqli_close($connect);
mysqli_free_result($query);
?>

Sem comentários:

Enviar um comentário