Forum Moderators: coopster
Up to that level, there is any problem because the program can retrieve a couple of related thesis with a good pagination system.
The problem however happens when i start clicking the "Next" and "Previous" links because the program returns nothing once i click those pagination links.
Here is the code. Please ignore the html part of the code because it is french, do consider only the PHP, code. If someone has a suggestion that can help navigate the different resultsets, i will be very grateful.
<?php
// inclure le fichier de connection
$connection = mysqli_connect("localhost", "ulbu", "ulbu", "annee2003_2004");
// Initialiser le nombre max de lignes à afficher
define("MAXSHOW", 5);
// Déterminer ce qui se passera si le button "submit" est cliqué
$motcle = isset($_POST['motcle'])? mysqli_real_escape_string($connection, strip_tags(trim($_POST["motcle"]))):"";
$faculte = isset($_POST['faculte'])? mysqli_real_escape_string($connection, strip_tags(trim($_POST["faculte"]))):"";
// Préparer la requete
$sql = "SELECT COUNT(*) FROM memoires WHERE sujet RLIKE '$motcle' AND faculte ='$faculte'";
// Envoyer requête
$result = mysqli_query($connection, $sql);
if($result)
{
$row = mysqli_fetch_row($result);
$totals = $row[0];
}
else{
$just_loaded = "<span>La page vient juste de se charger</<span>";
}
if(isset($totals) and $totals > 0)
$page_courante = isset($_GET["page_courante"])?$_GET["page_courante"]:0;
$numero_premiereligne = $page_courante*MAXSHOW;
$sql1 = "SELECT * FROM memoires WHERE sujet RLIKE '$motcle' AND faculte ='$faculte' LIMIT $numero_premiereligne,". MAXSHOW;
//exécuter la requête
$result1 = mysqli_query($connection, $sql1);
if(empty($motcle) ) {$warning= "<span style='color:red; font-family:arial; font-size:12px;'>Veuillez fournir un critère de recherche ici en haut, svp!</span>";}
?>
<html>
<head>
<title>
Formulaire de vérification de l'existence d'un mémoire
</title>
<!--<link href="css/ulbu.css" rel="stylesheet" type="text/css"/>-->
<style>html,body{
background-color:#dea;
height:100%;
margin:0;
overflow:auto;
}
body{
text-align:center;
color:#abc;
font-family: "Trebuchet MS",Arial, Tahoma, sans-serif;
color:#5b1c46;
margin:0 auto;
}
a{
text-decoration: none;
}
a:link{
color:RGB(10%,80%,20%);
}
a:visited{
color:RGB(40%,10%,60%);
}
a:active, a:focus{
color:#adf;
}
a:hover{
color:#339999;
}
div#conteneur{
width:1024px;
margin:0px auto;
padding:0 0;
background:#fff;
text-align:left;
padding: 10px 20px;
padding-bottom:0;
height:auto;
min-height:100%;
border:2px solid #ab4;
position:relative;
}
html > body div#conteneur{
height:auto;
min-height:80em;
}
div#header{
height:10%;
overflow:hidden;
background:;
width:1024px;
border-bottom:1px dashed gray;
}
div#wrapper{
width:100%;
position:relative;
}
div#menu{
width:200px;
float:left;
}
div#menu ul{
margin: 0 0;
padding:0;
text-align:right;
position:relative;
list-style-type:none;
}
div#menu ul li{
padding-right:15px;
font-variant:small-caps;
border-bottom:1px dotted RGB(90%,40%,20%);
padding:10px;
}
div#contenu{
float:right;
position:relative;
width:800px;
border-left:1px dashed #339999;
padding-left:20px;
}
div#contenu p{
margin-top:10px;
}
div#contenu form label{
width:100%;
display:block;
}
div#contenu form fieldset label, span{
display:block;
}
div#contenu form fieldset label span{
float:left;
width:45%;
text-align:right;
color:RGB(90%,40%,20%);
display:block;
}
div#contenu fieldset{
height:200px;
}
div#contenu form fieldset label span, label input{
display:inline;
}
div#contenu form fieldset label input{
margin-left:20px;
float:right;
}
div#contenu form fieldset label span, label select{
display:inline;
}
div#contenu form fieldset label select{
float:right;
}
div#contenu form fieldset label select option{
color:RGB(90%,40%,20%);
}
fieldset label#last{
position:relative;
top:50px;
}
fieldset label#second{
position:relative;
top:10px;
}
fieldset label#last input{
float:right;
right:400px;
position:absolute;
margin-top:40px;
}
html > body div#contenu{
height:auto;
min-height:75em;
margin-bottom:0;
}
div#menu, div#contenu{
display:inline;
}</style>
</head>
<body>
<div id="conteneur">
<div id="header">
</div> <!--fin div header -->
<div id="wrapper">
<div id="menu">
<!--<?php include("includes/menu.inc.php");?>-->
</div> <!--fin div menu -->
<div id="contenu">
<p>Bienvenu au module de recherche d'un mémoire. Cette page est une interface vous permettant de vérifier si le mémoire que vous voulez entreprendre d'ici peu n'a pas encore été traité par quelqu'un d'autre dans le passé!</p>
<form method="POST">
<fieldset>
<label for="motcle">
<span>Veuillez fournir votre mot clé de recherche:</span><input type="text" name="motcle" size="60" value="<?php if(isset($motcle)) echo $motcle;?>"></label>
<label id ="second" for="faculte"><span>Faculté:</span><select name="faculte"><option value="Informatique de Gestion">Informatique de gestion</option><option value="Communication">Communication</option><option value="Gestion et Administration">Gestion et Administration</option><option value="Droit">Droit</option> </select></label>
<label id="last"><input type="submit" name="rechercher" value="Rechercher" id="submit"></label></fieldset>
</form>
<?php
if(isset($warning) ) {echo $warning;}
if(isset($totals)){
echo "<p><b>$totals mémoires correspondent à vos critères de recherche. </b></p>" ;
echo "<p>----------------------------------------------------------------------------------------------------------------------</p>";
}
if($result1){
while($row = mysqli_fetch_assoc($result1)){
$auteurs = $row["auteurs"];
$sujet = $row["sujet"];
$directeur = $row["directeur"];
$faculte = $row["faculte"];
echo "<p><b>Sujet:</b> $sujet<br> <b>Auteurs:</b> $auteurs<br> <b>Directeur:</b> $directeur<br> <b>faculté:</b> $faculte</p>";
echo "*******************************************************************";
}
echo "<br>Enregistrement n° ". ($numero_premiereligne+1);
if($numero_premiereligne + 1 < $totals)
{
echo " à ";
if($numero_premiereligne + MAXSHOW < $totals)
{
echo $numero_premiereligne + MAXSHOW;
}
else {
echo " $totals ";
}
}
echo " sur $totals enregistrements. ";
// Mettre les liens de navigation
if($page_courante == 0){
echo '<a href="'.$_SERVER['PHP_SELF'].'?page_courante='.($page_courante + 1).'">Suivant ></a> ';
echo '<a href="'.$_SERVER['PHP_SELF'].'?page_courante='.(ceil($totals/MAXSHOW)-1).'">Fin »</a> ';
}
if($page_courante > 0){
echo "<a href='".$_SERVER['PHP_SELF']."?page_courante=".'0'."'>«Début </a> ";
echo "<a href='".$_SERVER['PHP_SELF']."?page_courante=".($page_courante-1)."'>«Précédent</a> ";
if($numero_premiereligne + MAXSHOW < $totals)
{
echo "<a href='".$_SERVER['PHP_SELF']."?page_courante=".($page_courante + 1)."'>Suivant ></a> ";
echo "<a href='".$_SERVER['PHP_SELF']."?page_courante=".(ceil($totals/MAXSHOW)-1)."'>Fin »</a> ";
}
}
}
else {echo "<span>There is no resultset!</span>";}
?>
</div> <!--fin div contenu -->
</div> <!--fin div wrapper -->
</div> <!--fin div conteneur -->
</body>
</html>
$page_courante = isset($_GET["page_courante"])?$_GET["page_courante"]:0;
$numero_premiereligne = $page_courante*MAXSHOW;
$sql1 = "SELECT * FROM memoires WHERE sujet RLIKE '$motcle' AND faculte ='$faculte' LIMIT $numero_premiereligne,". MAXSHOW;
echo '<!-- count ' . $page_courante . ' select ' . $sql1 . '-->';
And what do you get when you copy and paste the resulting $sql1 into mysql command line or phpMyAdmin?
There is no problem for the sql code because if run it on a database with enough data it returns a couple of records that contain the keyword it type in the search textbox.
The problem occurs when i click the "Suivant" link to see the rest of the resultset because it sends back an empty page which means there is a problem.
However, if i type again the same keyword in the textbox and hit again the search button, i am surprised to see that the page returns the result set i was supposed to get if the page had not returned an empty page in the previous stage. To remediate to this problem, i put a php code (see in the code) in the search textfield to always echo the value of the keyword that was passed to the script once it has been provided, but this seems not to yield the expected result and it is on this very point i would like to get your help.
the problem is that you are not passing the search parameters with the next button as the guys said already
you should make those changes for your code to work:
change these:
$motcle = isset($_POST['motcle'])? mysqli_real_escape_string($connection, strip_tags(trim($_POST["motcle"]))):"";
$faculte = isset($_POST['faculte'])? mysqli_real_escape_string($connection, strip_tags(trim($_POST["faculte"]))):"";
to:
$motcle = isset($_REQUEST['motcle'])? mysqli_real_escape_string($connection, strip_tags(trim($_REQUEST["motcle"]))):"";
$faculte = isset($_REQUEST['faculte'])? mysqli_real_escape_string($connection, strip_tags(trim($_REQUEST["faculte"]))):"";
u see here all i did is changed _POST to _REQUEST so it can work for both _POST and _GET
and also change this line:
echo '<a href="'.$_SERVER['PHP_SELF'].'?page_courante='.($page_courante + 1).'">Suivant ></a> ';
to this:
echo '<a href="'.$_SERVER['PHP_SELF'].'?page_courante='.($page_courante + 1). '&motcle=' . $_REQUEST['motcle'] . '&faculte=' . $_REQUEST['faculte'] . '">Suivant ></a> ';
i tested it and it works so you apply the same for the other links (previous,final, beginning .. etc)
the whole problem was that you are not passing the search parameters to the next page all you were passing is the (page_courante) so how can the query be built without those variables?
so the way to pass them is to put them in the link (next) but you were catching only $_POST variables so this will be solved when catching both GET and POST variables by using $_REQUEST instead
i hope i could help
Before i read your suggestion i changed $_POST to $_GET and passed in the other search parameters and now everything is all right. Thank you to let me know _REQUEST replaces both GET and POST