分页php多次查询同时

时间:2013-02-21 23:03:53

标签: php sql

您好我的问题如下: 我根据结果页面分了.. 问题是afiserea结果 我有以下代码:

<?php
  include('ttt_files/paginare.php');  
  $rezultate_pe_pagina = '2';
  $statement = "`anunt_standard` where `activat` = 0  AND prs_prv='$filtru_p' ";
  /* Inceputul stabiliri functiei numaratorelor */
  $page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
  $limit = $rezultate_pe_pagina;
  $startpoint = ($page * $limit) - $limit;
  //Query pentru afisere si paginare
  /* Sfarsitul stabiliri functiei numaratorelor */
  $url = "{$server_url}anunturi/f/{$_GET['f']}/page/";
  echo "<div id='asezare_butoane'>".pagination($statement,$limit,$page,$url)."</div>";


  //afiseaza inregistrarile platite
  $query_pay = mysql_query("
                 SELECT * FROM {$statement} AND activat_plata = '1' 
                 AND sters = '0' ORDER BY data_adaugare DESC  LIMIT {$startpoint} ,
                 {$limit}");

  echo "
  <table border='0' width='950px' 
    style='border-collapse:separate; border-spacing:0px  5px;'>";         
  $i = 0;     
  while ($row = mysql_fetch_assoc($query_pay)) {
        include('continut_platit.php');              
  }             
  echo "</table>";
  //afiseaza inregistrarile normale
  $query = mysql_query("
                 SELECT * FROM {$statement} AND sters = '0' 
                 AND activat_plata = '0' 
                 ORDER BY data_adaugare DESC 
                 LIMIT {$startpoint} , {$limit}");

   echo "
       <table border='0' width='950px' 
          style='border-collapse:separate; border-spacing:0px 5px;'>";   
                 $i = 0;
      while ($row = mysql_fetch_assoc($query)) {
            include('continut.php');

       }

       echo "</table>";

       //afiseaza inregistrarile sterse
       $query_del = mysql_query("
              SELECT * FROM {$statement} 
              AND sters = '1' 
              ORDER BY data_adaugare DESC 
              LIMIT {$startpoint} , {$limit}");

       echo "<table border='0' width='950px' style='border-collapse:separate; border-spacing:0px 5px;'>";

                 $i = 0;

                   while ($row = mysql_fetch_assoc($query_del)) {
                        include('continut_sters.php');

                        }

                 echo "</table>";
?>   

问题是它每页不显示2个结果,因为它现在设置显示每个查询的结果2,即粘贴总共6个结果: 2 - $ query_pay 2 - $查询 2 - $ query_del 但是打电话弄清楚如何让它只显示2例如是第一页如果添加2条记录'付费'只显示在那些2上..或者如果你只有2条记录,查询只显示那些2 :( 分页功能

    <?php
   function pagination($query, $per_page = 10,$page = 1, $url = '?'){  
           $query = "SELECT COUNT(*) as `num` FROM {$query}";
           $row = mysql_fetch_array(mysql_query($query));
           $total = $row['num'];
                $adjacents = "2";
           $page = ($page == 0 ? 1 : $page);
           $start = ($page - 1) * $per_page;                                       

           $prev = $page - 1;                                   
           $next = $page + 1;
                $lastpage = ceil($total/$per_page);
           $lpm1 = $lastpage - 1;

           $pagination = "";
           if($lastpage > 1)
           {  
                  $pagination .= "<ul class='pagination'>";
                                        //$pagination .= "<li class='details'>Pagina $page din $lastpage</li>";
                  if ($lastpage < 7 + ($adjacents * 2))
                  {  
                         for ($counter = 1; $counter <= $lastpage; $counter++)
                         {
                                if ($counter == $page)
                                   $pagination.= "<li><a class='current'>$counter</a></li>";
                                else
                                   $pagination.= "<li><a href='{$url}$counter/'>$counter</a></li>";                       
                         }
                  }
                  elseif($lastpage > 5 + ($adjacents * 2))
                  {
                         if($page < 1 + ($adjacents * 2))        
                         {
                                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                                {
                                   if ($counter == $page)
                                          $pagination.= "<li><a class='current'>$counter</a></li>";
                                   else
                                          $pagination.= "<li><a href='{$url}$counter/'>$counter</a></li>";                        
                                }
                                $pagination.= "<li class='dot'>...</li>";
                                $pagination.= "<li><a href='{$url}$lpm1/'>$lpm1</a></li>";
                                $pagination.= "<li><a href='{$url}$lastpage/'>$lastpage</a></li>";       
                         }
                         elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
                         {
                                $pagination.= "<li><a href='{$url}1/'>1</a></li>";
                                $pagination.= "<li><a href='{$url}2/'>2</a></li>";
                                $pagination.= "<li class='dot'>...</li>";
                                for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                                {
                                   if ($counter == $page)
                                          $pagination.= "<li><a class='current'>$counter</a></li>";
                                   else
                                          $pagination.= "<li><a href='{$url}$counter/'>$counter</a></li>";                        
                                }
                                $pagination.= "<li class='dot'>..</li>";
                                $pagination.= "<li><a href='{$url}$lpm1/'>$lpm1</a></li>";
                                $pagination.= "<li><a href='{$url}$lastpage/'>$lastpage</a></li>";       
                         }
                         else
                         {
                                $pagination.= "<li><a href='{$url}1/'>1</a></li>";
                                $pagination.= "<li><a href='{$url}2/'>2</a></li>";
                                $pagination.= "<li class='dot'>..</li>";
                                for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                                {
                                   if ($counter == $page)
                                          $pagination.= "<li><a class='current'>$counter</a></li>";
                                   else
                                          $pagination.= "<li><a href='{$url}$counter/'>$counter</a></li>";                        
                                }
                         }
                  }

                  if ($page < $counter - 1){

                 $pagination.= "<li><a href='{$url}$next/'>Urmatoarea</a></li>";
                                //$pagination.= "<li><a href='{$url}page=$lastpage'>Ultima</a></li>";
                  }else{
                         $pagination.= "<li><a class='current'>Urmatoarea</a></li>";
                                //$pagination.= "<li><a class='current'>Ultima</a></li>";
                        }
                  $pagination.= "</ul>\n";       
           }


                return $pagination;
        }
?>

我想要计算上面的总结果,并创造一个nustiu海报的条件,但如何设置条件。 或者将3放在查询结果和数字之上,然后我在3:01收集并分享他们的工作呢?但是去一个功能而不知道如何思考


mysql_query("SELECT u.* FROM (
                                (SELECT * FROM anunt_standard WHERE `activat_plata`='1' AND `sters`='0' AND `activat`='0' ORDER BY data_adaugare DESC)
                                UNION
                                (SELECT * FROM anunt_standard WHERE `activat_plata`='0' AND `sters`='0' AND `activat`='0' ORDER BY data_adaugare DESC)
                                UNION
                                (SELECT * FROM anunt_standard WHERE `sters`='1' AND `activat`='0' ORDER BY data_adaugare DESC)
                                ) As u  LIMIT {$startpoint} , {$limit}")or die(mysql_error());

感谢您的想法,但问题是他们无法按您想要的顺序显示...我的意思是我写的没有:

ORDER BY data_adaugare DESC

1 个答案:

答案 0 :(得分:0)

如果您熟悉SQL中的UNION,则可能需要尝试使用LIMIT x,y创建单个查询。如果您之前没有使用过UNION,请查看http://www.w3schools.com/sql/sql_union.asp

你会想要这样的东西:

SELECT u.* FROM (
    (SELECT... 1st query)
    UNION 
    (SELECT... 2nd query)
    UNION
    (SELECT... 3rd query)
) AS u LIMIT {$startpoint} , {$limit} 

在回复您的第二篇文章时,UNION会对一组行进行操作,这会产生不可预测的排序。你想在UNION的末尾加上“ORDER BY”。

另一种选择是将每个选择包含在另一个选择中,这似乎很愚蠢,但它使排序工作。请注意,UNION ALL允许重复结果,因此如果任何联合查询返回同一行,则会重复该行。

SELECT u.* FROM (
    SELECT * FROM (SELECT * FROM anunt_standard
                   WHERE `activat_plata`='1' AND `sters`='0' AND `activat`='0'
                   ORDER BY data_adaugare DESC) A
    UNION ALL
    SELECT * FROM (SELECT * FROM anunt_standard
                   WHERE `activat_plata`='0' AND `sters`='0' AND `activat`='0'
                   ORDER BY data_adaugare DESC) B
    UNION ALL
    SELECT * FROM (SELECT * FROM anunt_standard
                   WHERE `sters`='1' AND `activat`='0'
                   ORDER BY data_adaugare DESC) C
) As u LIMIT {$startpoint} , {$limit}