Galera, esse é meu primeiro post, estou inciando em PHP.
Preciso fazer uma consulta no meu BD e no caso de mais de 15 resultados ele divida essa busca em mais páginas.
O script que eu estou usando faz isso numa boa, mas quando eu vou pra segunda página ele perde a consulta que eu fiz e me dá uma tabela sem resultados nenhum.
//Página com o formulário de busca
<?php
$pagina = 'b2_0_pesquisa.php';
include('inc_check_access.php');
include('inc_abre.php');
include('inc_menu.php');
if( isset($_POST['pesq']) )
{
//foi apertado botão submit
$pesq = $_POST['pesq'];
$coluna = $_POST['coluna'];
}
else
{
//primeira vez
$pesq = '';
$coluna = '';
}
$sel_codigo = ($coluna=='codigo')?'selected="selected"':'';
$sel_ssop = ($coluna=='ssop')?'selected="selected"':'';
$sel_unidade = ($coluna=='unidade')?'selected="selected"':'';
$sel_tipo = ($coluna=='tipoitem')?'selected="selected"':'';
$sel_item = ($coluna=='item')?'selected="selected"':'';
$sel_disc = ($coluna=='disciplina')?'selected="selected"':'';
$sel_desc = ($coluna=='descricao')?'selected="selected"':'';
$sel_classe = ($coluna=='classependencia')?'selected="selected"':'';
$sel_indpor = ($coluna=='indicadopor')?'selected="selected"':'';
$sel_resp = ($coluna=='responsavel')?'selected="selected"':'';
$sel_status = ($coluna=='status')?'selected="selected"':'';
?>
<div id="pesquisar">
<form action="b2_0_pesquisa.php" method="POST" >
<p>
<span>Escolha o filtro: </span>
<select size="1" name="coluna" >
<option value="codigo" <?php echo $sel_codigo ?> >Número de pendência</option>
<option value="ssop" <?php echo $sel_ssop ?> >SSOP</option>
<option value="unidade" <?php echo $sel_unidade ?> >Unidade</option>
<option value="tipoitem" <?php echo $sel_tipo ?> >Tipo</option>
<option value="item" <?php echo $sel_item ?> >Item</option>
<option value="disciplina" <?php echo $sel_disc ?> >Disciplina</option>
<option value="descricao" <?php echo $sel_desc ?> >Descrição</option>
<option value="classependencia" <?php echo $sel_classe ?> >Classificação</option>
<option value="indicadopor" <?php echo $sel_indpor ?> >Responsável pela indicação</option>
<option value="responsavel" <?php echo $sel_resp ?> >Responsável pela ação</option>
<option value="status" <?php echo $sel_status ?> >Por situação</option>
</select>
</p>
<p>
<span>Pesquisar: </span><input type="text" name="pesq" value="<?php echo $pesq?>" maxlength="30" size="40"/>
</p>
<p>
<input type="submit" value="Pesquisar">
<input type="reset" value="Cancelar">
<input type="button" value="Limpar" onclick="self.location='b2_0_pesquisa.php'" >
</p>
</form>
</div>
<?php
$pesq = trim($pesq);
if( $pesq != '')
{
//pesquisar e listar registros encontrados ......
include_once('inc_conexao.php');
$sqlcount = "SELECT COUNT(*)
pendencia,datacadastro,ssop,unidade,tipoitem,item,disciplina,
descricao,classependencia,indicadopor,responsavel,status
FROM registro WHERE $coluna LIKE '%$pesq%'";
include_once('b2_1_0_monta_pesquisa.php');
}
?>
</body>
</html>
//Agora, o include que monta a tabela.
<?php
$query = mysql_query($sqlcount,$conexao);
$row = mysql_fetch_row($query);
$rows = $row[0];
$page_rows = 15;
$last = ceil($rows/$page_rows);
if($last < 1){
$last=1; }
$pagenum=1;
if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']); }
if($pagenum < 1){
$pagenum = 1; }
else if ($pagenum > $last){
$pagenum = $last; }
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$sqlcount = "SELECT
pendencia,datacadastro,ssop,unidade,tipoitem,item,disciplina,descricao,
classependencia,indicadopor,responsavel,status
FROM registro WHERE $coluna LIKE '%$pesq%'
ORDER BY pendencia DESC $limit";
$query = mysql_query($sqlcount,$conexao);
$textline1 = "Foram localizados <b>$rows</b> resultados.";
$textline2 = "Página <b>$pagenum</b> de <b>$last</b>";
$paginationCtrls = '';
if($last !=1){
if ($pagenum > 1){
$previous = $pagenum - 1;
$paginationCtrls .='<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Anterior</a> ';
for ($i = $pagenum - 4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .='<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> ';
}
}
}
$paginationCtrls .=''.$pagenum.' ';
for ($i = $pagenum + 1; $i <= $last; $i++){
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> ';
if($i >= $pagenum+4){
break;
}
}
if ($pagenum !=$last){
$next = $pagenum + 1;
$paginationCtrls .= ' <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Próxima</a> ';
}
}
?>
<br />
<div class="mid_center">
<h2>Consultar Pendências</h2><br />
<h3 class="h3_table"><?php echo $textline1; ?></h3>
<table>
<tr>
<th>Pendência</th>
<th>Data de cadastro</th>
<th>SSOP</th>
<th>Unidade</th>
<th>Tipo de item</th>
<th>Item</th>
<th>Disciplina<h>
<th>Descrição</th>
<th>Classificação da pendência</a></th>
<th>Indicado por</th>
<th>Setor responsável</th>
<th>Observação</th>
<th>Situação</th>
</tr>
<?php
//pendencia,datacadastro,ssop,unidade,tipoitem,item,disciplina,descricao,classependencia,indicadopor,responsavel,status
while($row = mysql_fetch_row($query))
{
echo "<tr>";
echo "<td><a href='2_2_0_info_pendencia.php?codigo=$row[0]' name='$row[0]' title='Apresentar informações sobre esta pendência.'>$row[0]</a></td>";
echo "<td>$row[1]</td>";
echo "<td>$row[2];</td>";
echo "<td>$row[3];</td>";
echo "<td>$row[4];</td>";
echo "<td>$row[5]</td>";
echo "<td>$row[6]</td>";
$row_descricao = $row[7];
if(strlen($row_descricao) > 30){
$row_descricao = substr($row_descricao, 0,30) ." ...";
}
echo "<td>$row_descricao</td>";
echo "<td>$row[8]</td>";
echo "<td>$row[9]</td>";
echo "<td>$row[10]</td>";
echo "<td>obs</td>";
echo "<td>$row[11]</td>";
echo "</tr>"; }
?>
</table>
<div id="pagination">
<span class="floatleft"><?php echo $textline2; ?></span>
<span class="floatright"><?php echo $paginationCtrls; ?></span>
</div>
</div>
<?php
include('inc_fecha.php');
?>
Valeu galera, obrigado!