使用PHP和AJAX进行分页查询结果

时间:2013-12-05 16:34:09

标签: php mysql ajax pagination

我的html中有一个过滤器表单,通过ajax将数据发送到php以挂载查询。 我想从查询中分页结果。最好的方法是什么?

您可以查看site here(点击过滤器)

AJAX:

jQuery(document).ready(function(){
      jQuery('#filtros').submit(function(){
            var dados = jQuery( this ).serialize();//build a post data structure
            jQuery.ajax({
            type: "POST",
            url: "organiza_tab.php",
            dataType:"text"
            data: dados,//Form variables
            success: function( response )
            {
                $("#output").html( response );
            }
           });
        return false;
});});

这是我的php文件,我抓取表单数据并挂载查询

$deano = $_POST['deyear'];
$deano = $_POST['deyear'];
$demes = $_POST['demonth'];
$dedia = $_POST['deday'];
$ateano = $_POST['ateyear'];
$atemes = $_POST['atemonth'];
$atedia = $_POST['ateday'];
$asc = $_POST['direcao'];
$orderby = $_POST['ordenado'];

$dedata = $deano ."-". $demes ."-". $dedia;
$atedata = $ateano ."-". $atemes ."-". $atedia;



if( $_POST['loja']){ 
        for ($i=0; $i<sizeof($_POST['loja']); $i++){
            $where_loja[$i] = " `loja` = '{$_POST['loja'][$i]}'";
        }
    }

if( sizeof( $where_loja ) ){
    $query[] = "(".implode( ' OR ',$where_loja ).")";
} 

$sql = "SELECT *
        FROM vendas WHERE";

 if(sizeof($query)>1){
    $sql .= $query[0];
    for($i=1; $i<sizeof($query);$i++){
    $sql.= ' AND '. $query[$i];
    }

    }else{

    $sql.= $query[0];   
    } 

    if( $_POST['loja']){ 
        $sql.=" AND ";
    }

    $sql .= " ( data > '$dedata' AND data <  '$atedata'  )";
    $sql .= " ORDER BY " . $orderby ." ". $asc ;

这是分页本身。问题是,当我更改页面时,URL也会被更改,并且表单不会被提交。我需要提交表单来执行查询。我怎样才能做到这一点?考虑到我需要提交表单,分页查询结果的最佳方法是什么。

  //items per page
  $quantidade = 30;
  //current page
  $pagina     = (isset($_POST['pagina'])) ? (int)$_POST['pagina'] : 1;
  //Calcula a pagina de qual valor ser? exibido
  $inicio     = ($quantidade * $pagina) - $quantidade;
  $sql .= " LIMIT " . $inicio . " , " . $quantidade ;

  $qr  = mysql_query($sql) or die(mysql_error());

  echo "<table id='tab_vendas' border='1' width='100%'>";
  echo "<tr><td>Data</td><td>Loja</td><td>Total (AKZ)</td><td>Total (USD)</td>     <td>Multicaixa</td><td>Saidas</td><td>Visa</td></tr>";

  $num_rows = mysql_num_rows($qr);
  if($num_rows > 0){
    while($ln = mysql_fetch_assoc($qr)){
          echo "<tr><td>" . $ln['data']."</td>"; 
          echo "<td>" . $ln['loja']."</td>"; 
          echo "<td>" . $ln['totalkz']."</td>";
          echo "<td>" . $ln['totaldollar']."</td>";
          echo "<td>" . $ln['multicaixa']."</td>";
          echo "<td>" . $ln['saidas']."</td>";
          echo "<td>" . $ln['visa']."</td></tr>"; 
    }
  }else{
       echo "sorry no data";
  }
echo"</table></div>";


  //SQL para saber o total
  $sqlTotal   = "SELECT id FROM vendas";
  //Executa o SQL
  $qrTotal    = mysql_query($sqlTotal) or die(mysql_error());
  //Total de Registro na tabela
  $numTotal   = mysql_num_rows($qrTotal);
  //O calculo do Total de p?gina ser exibido
  $totalPagina= ceil($numTotal/$quantidade);

   $exibir = 3;

   $anterior  = (($pagina - 1) == 0) ? 1 : $pagina - 1;

   $posterior = (($pagina+1) >= $totalPagina) ? $totalPagina : $pagina+1;

   echo "<div id='paginacao'><a href='?pagina=1'>Primeira</a> | ";
   echo "<a href=\"?pagina=$anterior\"><<</a> | ";

   for($i = $pagina-$exibir; $i <= $pagina-1; $i++){
            if($i > 0)
             echo '<a href="?pagina='.$i.'"> '.$i.' </a>';
  }

   echo '<a href="?pagina='.$pagina.'"><strong>['.$pagina.']</strong></a>';

   for($i = $pagina+1; $i < $pagina+$exibir; $i++){
            if($i <= $totalPagina)
            echo '<a href="?pagina='.$i.'"> '.$i.' </a>';
  }

  echo " | <a href=\"?pagina=$posterior\">>></a> | ";
  echo "  <a href=\"?pagina=$totalPagina\">Ultima</a></div>";

1 个答案:

答案 0 :(得分:0)

您需要将表单值附加到您的分页链接,例如:

'&deyear='.$deano'&amp;deano='.$deano'

然后,您需要修改定义表单变量的代码,例如:

$deano = $_POST['deyear'];
$deano = $_POST['deyear'];

...以便检查$ _GET和$ _POST。

您的网站上也有很多HTML错误,例如:你正在使用错误的语法。