链接php mysql分页无法正常工作

时间:2012-09-18 03:23:12

标签: php mysql pagination

我正在尝试对我的结果进行分页,限制正常,页面数量已正确设置,但是分页的链接不起作用,我一直在寻找一段时间,没有,¿看看我告诉我我做错了什么?感谢

<?php
include("config/conexion.php");
$limit = 3;

if(isset($_GET['pag'])){
$pag= $_GET['pag'];
}else{
$pag=1;
}
$offset = ($pag-1) * $limit;


$sql = "SELECT SQL_CALC_FOUND_ROWS id, nombre, local, telefono, celular, email FROM almacenes WHERE id_cat = '".$_GET["id"]."'";
$sqlTotal = "SELECT FOUND_ROWS() as total";
$currentid = $_GET["id"];
$rs = mysql_query($sql);
$rsTotal = mysql_query($sqlTotal);

$rowTotal = mysql_fetch_assoc($rsTotal);
// Total de registros sin limit
$total = $rowTotal["total"];

?>
 <?php if($_GET["id"]){  $cat = mysql_query("SELECT * FROM almacenes WHERE id_cat = '".$_GET["id"]."' ORDER BY id ASC LIMIT $offset, $limit");  if(mysql_num_rows($cat)>0){

while($row = mysql_fetch_object($cat)){ ?>   

<div class="almacenbox">
    <div class="shadow"></div>
    <div class="white">
        <div class="image"><img src=almacenes/local_111.jpg></div>
        <div class="title"><?php echo $row->nombre?></div>
        <div class="text">Local: <?php echo $row->local?></div>
        <div class="text">Teléfono: <?php echo $row->telefono?></div>
        <div class="text">Celular: <?php echo $row->celular?></div>
        <div class="text"><?php echo $row->email?></div>
    </div>
    </div>
    <?php } ?>
    <?php } }else{ echo "<p>No hay resultados para mostrar</p>"; }?>
    <table border="1" bordercolor="#000">
  <tfoot>
  <tr>
     <td colspan="2">
  <?php
     $totalPag = ceil($total/$limit);
     $links = array();
     for( $i=1; $i<=$totalPag ; $i++)
     {
        $links[] = "<a href=almacenes.php?id=$currentid?pag=$i\>$i</a>"; 
     }
   echo implode(" - ", $links);
  ?>
     </td>
  </tr>
  </tfoot> </table>   

1 个答案:

答案 0 :(得分:1)

$links[] = "<a href=almacenes.php?id=$currentid?pag=$i\>$i</a>";

应该是

$links[] = '<a href="almacenes.php?id=$currentid&amp;pag=$i">$i</a>';

查询字符串以?开头,但在第一个字符串后,任何名称 - 值对都需要&符号。

另外,您不应将用户数据直接放入查询中。这使您对SQL注入攻击持开放态度。请考虑使用mysql_real_escape_string或切换到mysqli library