Filter and pagination

Asked

Viewed 992 times

0

I developed a web page that uses paging and filter, both made in PHP. What makes after using the filter, and select for example page 2, the information will be lost because it will reload all the information.

I’ve been researching and found the following:

http://plnkr.co/edit/Q26WPjLqcOtS8p0mGF6b?p=preview

http://plnkr.co/edit/Wtkv71LIqUR4OhzhgpqL?p=preview

Is it possible to use something like this with database connection ?

Since I don’t know what to use for sure it will be necessary to edit tags and title

Current code

filter

  <FORM NAME ="form" METHOD ="POST" >




                   <select name="name" style='width:120px;' id="mySelect" >
                       <option value='' >Ordenar por:</option>
                       <option  value='cod_filme'>Mais recentes</option>
                       <option value='visualizacoes' >Mais vistos</option>
                   </select>



                   <select style='width:70px;' name="pesquisa" id="mySelect" >
                       <?php
                       error_reporting(E_ALL ^ E_DEPRECATED);
                       mysql_connect('127.0.0.1','root','');
                       mysql_select_db('trabalho_pratico');


                       $SQL = "SELECT * FROM ano";
                       $result = mysql_query($SQL);
                       print "<option value='' >Ano:</option>";
                       while ( $db_field = mysql_fetch_assoc($result) ) {
                           print "<option value=" . $db_field['ano'] . ">" . $db_field['ano'] . "</option>";

                       }

                       ?>
                   </select>

                   <select name="categ" style='width:130px;' id="mySelect">
                       <?php
                       error_reporting(E_ALL ^ E_DEPRECATED);
                       mysql_connect('127.0.0.1','root','');
                       mysql_select_db('trabalho_pratico');


                       $SQL = "SELECT * FROM categoria";
                       $result = mysql_query($SQL);
                       print "<option value='' >Categoria:</option>";
                       while ( $db_field = mysql_fetch_assoc($result) ) {
                           print "<option value=" . $db_field['categoria'] . ">" . $db_field['categoria'] . "</option>";

                       }

                       ?>
                   </select>
                   <td>
                       <input type='text' placeholder="Nome" name='procura'>
                   </td>
                   <input style='width:95px;'  type="submit" name="submitfiltro">
               </form>

Display of filtered content with paging

 if(($ano!="")&&($matr=="")&&($cat=="")&&($proc=="")){
                   $vari= "SELECT * FROM filme where ano=$ano";
                   $final_query="FROM filme where ano=$ano";
                   error_reporting(E_ALL ^ E_DEPRECATED);
                   mysql_connect('127.0.0.1','root','');
                   mysql_select_db('trabalho_pratico');
                   $maximo = 8;
                   $pagina = (isset($_GET["pagina"])) ? $_GET["pagina"] : null;
                   if($pagina == "") {
                       $pagina = "1";
                   }
                   $inicio = $pagina - 1;
                   $inicio = $maximo * $inicio;
                   $strCount = "SELECT COUNT(*) AS 'num_registros' $final_query";
                   $query = mysql_query($strCount);
                   $row = mysql_fetch_array($query);
                   $total = $row["num_registros"];
                   $SQL =$vari .  ' LIMIT '. $inicio. ' , '.$maximo;
                   $result = mysql_query($SQL);
                   while ( $db_field = mysql_fetch_assoc($result) ) {
                       $img=$db_field['imagem'];
                       echo '<a href="conteudo.php?$op='.$img.'"><img src="' . $img . '"></a>';
                   }
                   $menos = $pagina - 1;
                   $mais = $pagina + 1;

                   $pgs = ceil($total / $maximo);
                   echo "<br>";
                   if($pgs > 1 ) {

                       echo "<br />";

                       // Mostragem de pagina
                       if($menos > 0) {
                           echo "<a href=".$_SERVER['PHP_SELF']."?pagina=$menos>anterior</a>&nbsp; ";
                       }

                       // Listando as paginas
                       for($i=1;$i <= $pgs;$i++) {
                           if($i != $pagina) {
                               echo " <a href='?pagina=".($i)."'>$i</a> | ";
                           } else {
                               echo " <strong>".$i."</strong> | ";
                           }
                       }

                       if($mais <= $pgs) {
                           echo " <a href=".$_SERVER['PHP_SELF']."?pagina=$mais>próxima</a>";
                       }
                   }

Content displayed after filtering

inserir a descrição da imagem aqui

When I select the next page in the pagination

inserir a descrição da imagem aqui

  • Explain, what do you really want to do? You need to send the filter information to another page?

  • not what I have is a page where appear news and this page has pagination, the problem is that when putting the filter which is also in php the information when switching to page 2 is lost

  • page 2 you say, is the second page of pagination? Or another page?

  • Yes you can, use SQL from LIMIT (Read the tutorial: http://www.w3schools.com/sql/sql_top.asp) to work it and do the result php. Example: SELECT * FROM CADASTRO LIMIT 0 , 20 0 is start, 20 is line... next to him 21, 20 and next 41, 20.

1 answer

1


The idea of paging with filters is you "save" which filter the user selected for when they click on other pages, the filter is not lost.

A practical example

Let’s say I have a select containing the following values:

<select>
  <option value="1">Filtro padrão por id</option>
  <option value="2">Filtro por A-Z</option>
  <option value="3">Filtro por Z-A</option>
</select> 

When you click on one of them, the list is sorted with this value.

Already in paging, obligatorily you must in addition to inform the page, the filter that is currently selected.

<ul class="pagination">
  <li><a href="#">Pagina 1 e Filtro código ${De acordo com oq tiver na sessão}</a></li>
  <li><a href="#">Pagina 2 e Filtro código ${De acordo com oq tiver na sessão}</a></li>
  <li><a href="#">Pagina 3 e Filtro código ${De acordo com oq tiver na sessão}</a></li>
  <li><a href="#">Pagina 4 e Filtro código ${De acordo com oq tiver na sessão}</a></li>
  <li><a href="#">Pagina 5 e Filtro código ${De acordo com oq tiver na sessão}</a></li>
</ul>
  • I do not know if I understand. What you are referring to is in href beyond the n of the page should have a variable with filter value?

  • @saimita The html tags are just an example of the idea, you must adapt the idea of the way you are developing in your system :) . Yes, it should have the filter value along with the page.

  • I just have a problem, filtering the information remains on the same page, if for example you are on page two and filter per year will appear the filtered content but on page two in a certain way when clicking on Submit I will have to be able to change the page variable that is on the link to 1

  • @saimita Adapt to when you select another filter, reset the entire page, going back to the initial flow :)

  • @I suggest you visit some online store and see how they do this. For example the store.Xbox has this very visibly.

  • I set a variable when starting the page that makes it check whether the filter Submit was selected or not giving a value for each situation. Checking that the filter has been selected the page will be 1. Thanks for the help provided

Show 1 more comment

Browser other questions tagged

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