variable does not load in pagination

Asked

Viewed 103 times

0

I’m trying to make the search pagination of my site from a search form but I’m not getting the variable that comes via POST to click on the other pages and not be empty. The variable is $search. How do I make this variable click on the pagination and is not empty?

Search form:

<form name="Busca" method="post" action="index.php?pg=pesquisa2">
    <label>
        <input type="text" name="busca" placeholder=" O que você procura?" />
    </label>            

    <select id="estados" name="estados" id="form_idade">
        <option value="" selected="selected"> Selecione o estado </option>
        <?php
        $sql = mysql_query("SELECT * FROM estados");

        while($row = mysql_fetch_array($sql)){
            $id = $row['cod_estados'];
            $sigla = $row['sigla'];
            $nome = $row['nome'];
        ?>

            <option value="<?php echo $id ?>">
                <?php echo $nome ?>-<?php echo $sigla ?>
            </option> 

        <?php
        }
        ?>
    </select>
    </label>

    <label>
        <select id="cidades" name="cidades">
            <option value="" selected="selected"> Selecione a cidade</option>                   
        </select>

        <input type="image" src="01-Imagens/pesquisar03_btn.png" name="executar" id="executar" value="Ir" />
</form>  

Paging:

<?php
$busca=$_POST['busca']; 
$anuncioEstado = strip_tags(trim($_POST['estados']));
$anuncioCidade = strip_tags(trim($_POST['cidades']));       
?>


<?php   
if(!empty($_POST['busca']) && ($anuncioEstado == null) &&($anuncioCidade == null)){


    $pag = "$_GET[pag]";
    if($pag >= '1'){
        $pag = $pag;
    }else{
        $pag = '1';
    }

    $maximo = '6'; //RESULTADOS POR PÁGINA
    $inicio = ($pag * $maximo) - $maximo;


    $sql = mysql_query("SELECT * FROM fj_cadastroanuncio WHERE anunciopalavraChave LIKE '%".$busca."%'
                        AND anuncioTipoPlano LIKE '%PN%' AND anuncioStatus = 'completo' ORDER BY RAND() LIMIT ".$inicio.",".$maximo);
    $row = mysql_num_rows($sql);

if ($row > 0){
?>      



<?php

$sql_res = mysql_query("SELECT * FROM fj_cadastroanuncio WHERE anunciopalavraChave LIKE '%".$busca."%' 
                        AND anuncioTipoPlano LIKE '%PN%' AND anuncioStatus = 'completo' ORDER BY RAND()");
$total = mysql_num_rows($sql_res);

$paginas = ceil($total/$maximo);
$links = '6'; 


for ($i = $pag-$links; $i <= $pag-1; $i++){
    if ($i <= 0){
    }else{
        echo"<a href=\"index.php?pg=pesquisa2&amp;pag=$i\">$i</a>&nbsp;&nbsp;&nbsp;";
    }
}echo "$pag &nbsp;&nbsp;&nbsp;";

for($i = $pag +1; $i <= $pag+$links; $i++){
    if($i > $paginas){
    }else{
        echo "<a href=\"index.php?pg=pesquisa2&amp;pag=$i\">$i</a>&nbsp;&nbsp;&nbsp;";
    }
}
?>
  • in my view, order by Rand does not go much with paging, and as for the search problem to come blank, all form fields should be recreated as hidden fields, so they go to the next pages...

  • I forgot to mention, your script is vulnerable, I advise you to treat the variables before including them in the query, and review the use of mysql_* that are obsolete...

1 answer

1

I would change $_POST['busca'] for $_REQUEST['busca'], so the variable can receive both data by post how much for get.

It would also send the search data in the pagination link:

index.php?pg=pesquisa2&pag=$i&busca=$busca

I hope I’ve helped.

  • 1

    Helped yes friend, I decided to send the search data in the pagination link as your tip. Thanks for the help.

  • I’m glad I helped. I could mark my post as an answer?

  • this is an alternative solution, but otherwise it should also work.

Browser other questions tagged

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