Query in returned query

Asked

Viewed 34 times

1

I am creating my table with paging in this way and with the possibility of searching with a input:

<?php 
$itens_por_pagina = 5;
$pagina = intval($_GET['pagina']);
if (!$pagina) {
    $pc = "0";
} else {
    $pc = $pagina;
}
$inicio = $pc - 0;
$inicio = $inicio * $itens_por_pagina;
$query = "SELECT A.id, A.nome, B.nome AS Nomes FROM raddb.usuarios AS A LEFT OUTER JOIN raddb.niveis_acessos AS B ON B.id = A.niveis_acesso_id ORDER BY Nome ASC LIMIT $inicio, $itens_por_pagina";
$result = $conn->query($query) or die($conn->error);
$produto = $result->fetch_assoc();
$num = $result->num_rows;
$num_total = $conn->query("SELECT A.id, A.nome, B.nome AS Nomes FROM raddb.usuarios AS A LEFT OUTER JOIN raddb.niveis_acessos AS B ON B.id = A.niveis_acesso_id ORDER BY Nome ASC")->num_rows;
$num_paginas = ceil($num_total / $itens_por_pagina);
?>
<div id="employee_table">
    <h1>Utilizadores</h1>
    <?php if ($num > 0) { ?>
        <table class="table table-responsive lista-clientes">
            <thead>
            <span class="glyphicon glyphicon-filter" data-toggle="modal" data-original-title="Filter"></span><input type="text" class="input-search" alt="lista-clientes"/>
            <tr>
                <th>Nome</th>
                <th>Nível de Acesso</th>
                <th>Ações</th>
                <th>Eliminar</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <?php do{ ?>

                <td><?php echo $produto["nome"]; ?></td>
                <td><?php echo $produto["Nomes"]; ?></td>
                <td>
                    <button type="button" name="view" id="<?php echo $produto["id"]; ?>" data-toggle="modal" href="#dataModal" class="btn btn-warning btn-sm view_data"/>
                    <span class="glyphicon glyphicon-edit"></span></button>
                </td>
                <td>
                    <button type="button" id="<?php echo $produto["id"]; ?>" class="btn btn-dander btn-sm delete"/>
                    <span class="glyphicon glyphicon-trash"></span></button>
                </td>
            </tr>

            <?php } while ($produto = $result->fetch_assoc()); ?>
            </tbody>

        </table>

        <nav>
            <ul class="pagination">
                <li>
                    <a href="?pagina=0" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>
                <?php
                for ($i = 0; $i < $num_paginas; $i++) {
                    $estilo = "";
                    if ($pc == $i) {
                        $estilo = "class=\"active\"";
                    }
                    ?>
                    <li <?php echo $estilo; ?> ><a href="?pagina=<?php echo $i; ?>"><?php echo $i + 1; ?></a></li>
                <?php } ?>
                <li>
                    <a href="?pagina=<?php echo $num_paginas - 1; ?>" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
            </ul>
        </nav>
    <?php } ?>
</div>

input research:

<span class="glyphicon glyphicon-filter" data-toggle="modal" data-original-title="Filter"></span><input type="text" class="input-search" alt="lista-clientes"/>

Function to search in my table columns:

$(function(){
    $(".input-search").keyup(function(){
        //pega o css da tabela 
        var tabela = $(this).attr('alt');
        if( $(this).val() != ""){
            $("."+tabela+" tbody>tr").hide();
            $("."+tabela+" td:contains-ci('" + $(this).val() + "')").parent("tr").show();
        } else{
            $("."+tabela+" tbody>tr").show();
        }
    }); 
});
$.extend($.expr[":"], {
    "contains-ci": function(elem, i, match, array) {
        return (elem.textContent || elem.innerText || $(elem).text() || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
    }
});

So far everything works well. Now what I want is, that my table returns 5 rows on each page, and when I search on input only does the search on these 5 lines and intended it to do the search on all lines that the query returns.

  • Vc uses some plugin to make the pagination or table?

  • @Sam no, I just used that code

  • 2

    Boy, the problem is that paging only generates in HTML the lines of the page, that is, the other lines of the other pages do not exist in HTML, so there is no way to find them. Maybe you should use the Datatables that already comes with paging and filter all together.

No answers

Browser other questions tagged

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