0
votes

I have search page and search input in the header of my website, and when the user types in the query he'll be directed to search.php and i'll display whatever the result the user wants.

But when I have a lot of result I becomes really hard to display or read those results, so I need to make a pagination..

I'm using a pagination function in my website, but its not working when I'm dealing with this search page, I think the reason of that because i'm sending the same query in every page.. so Its showing the results of the page 1 in all pages..

Can anyone please explain to me how to make pagination for search page? by showing 15 results per-page for example?

This is the search form that I have:

<form class="navbar-form navbar-left" action="search" method="POST">
<div class="form-group">
    <input type="search" name="searchFor" class="form-control" placeholder="Search for Articles" dir="auto"
        <?php
            echo isset($_POST['searchFor']) && !empty($_POST['searchFor']) ?
            'value="' .  htmlentities($_POST['searchFor']) . '"' : ''
        ?>
    >
</div>
<button type="submit" class="btn btn-default">Search</button>

2

2 Answers

1
votes

I will show my example of pagination of my own project. I did put some commentary but they are in french because i m french but i think they will be easy to translate . I hope this will help you. I don't think you need my ''configuraiton.php'' it's for the local server connection anyway just try to understand my example . Ow and my search is on 3 criterion that's why i have a switch and case .

//This is foncitons.php purpose limit items and create page for pagination 

    <?php

if(isset($_GET['limite'])) 

    $limite=$_GET['limite'];
else   $limite=0;

function verifLimite($limite,$total,$nombre) {

 if(is_numeric($limite)) {

    if(($limite >=0) && ($limite <= $total) && (($limite%$nombre)==0)) {

       $valide = 1;

   }    

   else {

       $valide = 0;

   }

}

else {


    $valide = 0;

}

return $valide;

}


function displayNextPreviousButtons($nb,$page,$total,$limite) {
    $limiteSuivante = $limite + $nb;
    $limitePrecedente = $limite - $nb;


    echo  '<nav><ul class="pagination">'."\n";

    if($limite != 0) {
        echo '<li><a href="'.$page.'?limite='.$limitePrecedente.'" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>';
    }
    $nbpages=ceil($total/$nb);
    $numeroPages = 1;
    $compteurPages = 1;
    $limite  = 0;
    while($numeroPages <= $nbpages) {
        echo '<li><a href = "'.$page.'?limite='.$limite.'">'.$numeroPages.'</a></li>'."\n";
        $limite = $limite + $nb;
        $numeroPages = $numeroPages + 1;
        $compteurPages = $compteurPages + 1;
        if($compteurPages == 10) {
            $compteurPages = 1;
        }
    }


    if($limiteSuivante < $total) {
        echo '<li><a href="'.$page.'?limite='.$limiteSuivante.'" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>';
    }


    echo '</ul></nav>'."\n";
}

 ?>

This my search page for cars .

<?php

// Variables de connexion
include("configuration.php");

// Fonctions AffichePages, Bouton Suivant/Précédent, VérifLimite
include ("fonctions.php");

// Connexion à la base
$link = mysqli_connect($hote,$login,$pass,$base);






// Nombre d'entrées par page
$nombre=6;

// Au début limite inférieur = 0
if (!$limite) 
{
  $limite = 0;
}

// On recherche le lien de la page
$path_parts = pathinfo($_SERVER['PHP_SELF']);

$page = $path_parts["basename"];


// On vérifie si on a effectué une recherche et on compte le nombre de données a retourner
if (((isset($_REQUEST["marque"])) && ($_REQUEST["marque"] !="")) || ((isset($_REQUEST["modele"])) && ($_REQUEST["modele"] !=""))) {

  // Si on a effectué la recherche, on stock 
  $nom=$_REQUEST["marque"];
  $categorie=$_REQUEST["modele"];

  // On remplace les accents dans la variable $nom avec les codes appropriés
  $nom=str_replace("é", "&eacute", $nom);
  $nom=str_replace("è", "&egrave", $nom);
  $nom=str_replace("ê", "&ecirc", $nom);
  $nom=str_replace("à", "&agrave", $nom);
  $nom=str_replace("â", "&acirc", $nom);
  $nom=str_replace("ç", "&ccedil", $nom);


  if($nom !=""){
    $choix=1;
  }
  if($categorie !=""){
    $choix=2;
  }
  if($nom !="" and $categorie !=""){
    $choix=3;
  }
  Switch($choix){
   case 1:

   $sqlcount = "SELECT COUNT(*) FROM voitures WHERE marque LIKE '%$nom%'";
   $result = mysqli_query($link,$sqlcount);
   $row = mysqli_fetch_row($result);
   $total = $row[0];

            // On calcule la limite
   $verifLimite= verifLimite($limite,$total,$nombre);

   if(!$verifLimite)  {

    $limite = 0;

  }

            // On va lire les données de la table
  $sql = "SELECT idvoitures, marque, modele, LEFT(details,70), images, annee, lien, kilometrage, puissance, couleur FROM voitures WHERE marque LIKE '%$nom%' ORDER BY marque, modele LIMIT $limite, $nombre";
  break;

  case 2:
  $sqlcount = "SELECT COUNT(*) FROM voitures WHERE modele LIKE '$categorie'";
  $result = mysqli_query($link,$sqlcount);
  $row = mysqli_fetch_row($result);
  $total = $row[0];

            // On calcule la limite
  $verifLimite= verifLimite($limite,$total,$nombre);

  if(!$verifLimite)  {

    $limite = 0;

  }

            // On va lire les données de la table
  $sql = "SELECT idvoitures, marque, modele, LEFT(details,70), images, annee, lien, kilometrage, puissance, couleur FROM voitures WHERE modele LIKE '%$categorie%' ORDER BY marque, modele LIMIT $limite, $nombre";
  break;

  case 3:
  $sqlcount = "SELECT COUNT(*) FROM voitures WHERE marque LIKE '%$nom%' and modele LIKE '%$categorie%'";
  $result = mysqli_query($link,$sqlcount);
  $row = mysqli_fetch_row($result);
  $total = $row[0];

              // On calcule la limite
  $verifLimite= verifLimite($limite,$total,$nombre);

  if(!$verifLimite)  {

    $limite = 0;

  }

              // On va lire les données de la table
  $sql = "SELECT idvoitures, marque, modele, LEFT(details,70), images, annee, lien, kilometrage, puissance, couleur FROM voitures WHERE marque LIKE '%$nom%' and modele LIKE '%$categorie%' ORDER BY marque, modele LIMIT $limite, $nombre";

  break;
}

}

else
{
  $sqlcount = 'SELECT COUNT(*) FROM voitures';
  $result = mysqli_query($link,$sqlcount);
  $row = mysqli_fetch_row($result);

  $total = $row[0];

  // On calcule la limite
  $verifLimite= verifLimite($limite,$total,$nombre);

  if(!$verifLimite)  {

    $limite = 0;

  }

  // On va lire les données de la table
  $sql = "SELECT idvoitures, marque, modele, LEFT(details,70), images, annee, lien, kilometrage, puissance, couleur FROM voitures ORDER BY marque, modele LIMIT $limite, $nombre";
}







$result = mysqli_query($link,$sql);

echo '<div class="container">';
echo '<div class="row">';

// Variable qui compte le nombre d'affichage par rangée
$i = 0;
// On affiche les résultats à travers la boucle
if($total) {

  while($row = mysqli_fetch_array($result)) {

    $idvoitures=$row["idvoitures"];
    $marque=$row["marque"];
    $modele=$row["modele"];
    $details=$row["LEFT(details,70)"];
    $images=$row["images"];
    $couleur=$row["couleur"];
    $annee=$row["annee"];
    $kilometrage=$row["kilometrage"];
    $lien=$row["lien"];

    // On affiche 3 par rangée -> après chaque trois affichages on met une nouvelle rangée
    // if (($i%3 == 0) && ($i != 0))
    // {
    //   echo '<div class="row">';
    // }

    echo '<div class="col-md-4">';
    echo "<div class=\"view view-forth\">";
    echo "<img class=\"thumb\" src=\"images/$images\" />";
    echo "<div class=\"mask\">";
    echo "<h2>$marque $modele</h2>";
    echo "<p>$details...</p>";
    echo "<a href=\"afficheVoiture.php?id=$idvoitures\" class=\"info\">Découvrir</a>";
    echo "</div>";
    echo "</div>";
    echo "</div>";

    // On ferme la balise div à chaque fois qu'on crée une nouvelle rangée
    // if (($i%3 == 0) && ($i != 0))
    // {
    //   echo '</div>';
    // }

  }
}
echo '</div>';
echo '</div>';

?>
0
votes

I think this form does not need changes. You have to change search.php file (and file which show results). Add limit to sql-query and add pagination buttons to markup. Like this.