I am doing a pagination in PHP from records in Database MySQL. I have in total 7 records to display, I have set numbers per page (resultsPerPage) as 2, so I will be displaying 4 pages. I am displaying results from two queries:
- First is table tblPaciente which shows information from my client.
- Second is table tblEvolucion which shows information which I required to apply the pagination. (7 records to display)
I am displaying a query from table tblEvolucion and displaying order by clmSerie column descending.
- Page 1 I want to display records with Description: "Record 7" and "Record 6"
- Page 2 I want to display records with Description: "Record 5" and "Record 4"
- Page 3 I want to display records with Description: "Record 3" and "Record 2"
- Page 4 I want to display record with Description: "Record 1"
My problem is when I press "Previous Page", "Next Page" or any of the displaying pages (1,2,3,4), the form is being reset and is showing me nothing but the FORM (id=frmPacienteBuscar) empty, with no results. I required to display it to me next page and remains table results (id=table1) from the first query and also remain the form (id=frmPacienteBuscar).
If I search again in the form after it has been reset it will access to the page that I went before. For example: - First I load the page. - Then I press page number 3 - Then everything is blank and I have to retype inputs with ids (id="searchByRut" and id="dv") - Then I will be accessing immediately from page number 3
I have tried to pass in the HREF of my "Next Page" the variable $searchByRut, with is required for me to search in table tblPaciente and tblEvolucion but it is saying to me that is undefined index after I press a link to access any page. Here:
$outputTblEvolucion .= " <a href='{$_SERVER['PHP_SELF']}?currentPage=$prevpage'> Previous Page </a> ";
I am trying to capture it as follows at the beginning of my PHP code:
$searchByRut = $_POST["searchByRut"];
I assume that I required to pass as POST in the HREF of every link to pages the RUT but I am not able to catch it and also not sure if that is the problem. Please let me know any clarification, thanks in advance,
testPaginization3.php
<?php
include_once('funcionesComunes.php');
require_once('mysqli_connect.php');
$outputTblEvolucion = '';
$outputTblPaciente = $outputData = '';
$searchByRut_error = '';
$searchByRut = '';
$dv_input = '';
$iCompleteFirstQuery = $iHaveToDoSecondQuery = 0;
$countDataFromTblPaciente = 0;
$searchByRut = $_POST["searchByRut"];
if(isset($_POST['searchByRut']))
{
//there are just validation if my id entered to search is valid, please ignore this part:
$error = '';
$dv = $_POST["dv"];
$dv_input = $_POST["dv"];
$searchByRut = $_POST["searchByRut"];
echo "<br>". "searchByRut={" . $searchByRut ."}";
if ($dv == "k")
$dv = "K";
$searchByRut_error = rutValidar($searchByRut, $dv, $error);
//here I am checking if any error occurred in the previous validation
if ($searchByRut_error == '')
{
$rut = $_POST['searchByRut'];
$queryDatosPaciente = mysqli_query($dbc,
"SELECT CONCAT(pa.clmNombres, ' ', pa.clmApellidos) as clmNombres
,pa.clmEmail
FROM tblPaciente pa
WHERE pa.clmRut = '$rut'
"
) or die('Error while searching!: {' . mysqli_error($dbc) . '}');
$countDataFromTblPaciente = mysqli_num_rows($queryDatosPaciente);
$iCompleteFirstQuery = 1;
}
}
//this variable iCompleteFirstQuery will tell me that I have to execute second query in detail table tblEvolucion, because I found already the main record in customer table tblPaciente
if ($iCompleteFirstQuery == 1)
{
if($countDataFromTblPaciente == 0)
{
$outputTblPaciente = 'It did not find client!';
$iHaveToDoSecondQuery = 0;
}
else
{
//Here I just build the Table with result from query of customer tblPaciente
$iHaveToDoSecondQuery = 1;
$outputTblPaciente .= "<table id='table1' border='1' align='left' cellspacing='5' cellpadding='8'>";
$outputTblPaciente .= "<tr><td align='left'><b>Name </b></td>
<td align='left'><b>Email </b></td>
</tr>";
while($row = mysqli_fetch_array($queryDatosPaciente))
{
$nombres = utf8_encode($row['clmNombres']);
$email = $row['clmEmail'];
$outputTblPaciente .= "<td align='left'> $nombres </td>
<td align='left'> $email </td>";
$outputTblPaciente .= "</tr></table><br><br><br><br><br>";
}
}
}
//In this IF condition is the part that I required to make as pagination, for the result of table tblEvolucion
if ($iHaveToDoSecondQuery == 1)
{
$sqlCount = "SELECT COUNT(*)
FROM tblEvolucion ev
WHERE ev.clmRut = '$rut'";
$resultadoQueryCount = mysqli_query($dbc,$sqlCount) or die('Error to select!: {' . mysqli_error($dbc) . '}');
$r = mysqli_fetch_row($resultadoQueryCount);
$countRecordsFound = $r[0];
echo "<br>". "countRecordsFound={" . $countRecordsFound ."}";
echo "<br><br>";
$resultsPerPage = 2;
$totalPages = ceil($countRecordsFound / $resultsPerPage);
// get the current page or set a default
if (isset($_GET['currentPage']) && is_numeric($_GET['currentPage']))
{
// cast var as int
$currentPage = (int) $_GET['currentPage'];
}
else
{
// default page num
$currentPage = 1;
}
// if current page is greater than total pages...
if ($currentPage > $totalPages)
{
// set current page to last page
$currentPage = $totalPages;
}
// if current page is less than first page...
if ($currentPage < 1)
{
// set current page to first page
$currentPage = 1;
}
// the offset of the list, based on current page
$offset = ($currentPage - 1) * $resultsPerPage;
// get the info from the db
$sqlDespliegaDatos ="SELECT ev.clmDescripcion
FROM tblEvolucion ev
WHERE ev.clmRut = '$rut'
ORDER by ev.clmSerie DESC
LIMIT $offset
,$resultsPerPage";
$resultadoQueryDespliegaDatos = mysqli_query($dbc,$sqlDespliegaDatos) or die('Error to select!: {' . mysqli_error($dbc) . '}');
$iHaveToDoSecondQuery = 1;
if($countRecordsFound == 0)
{
$outputData = 'No results from Table tblEvolucion!';
}
else
{
//From there I build the table with my records from tblEvolucion wish I required to be with pagination
$outputData = '';
$outputData .= '<table border=1 align="left" cellspacing="5" cellpadding="8">
<tr><td align="left"><b>Description </b></td>
</tr>';
while ($list = mysqli_fetch_assoc($resultadoQueryDespliegaDatos))
{
$descripcion = utf8_encode($list['clmDescripcion']);
$outputData .= '<tr>';
$outputData .= "<td align='left'><textarea id='descripcion' name='descripcion' tabindex='4' cols='50' rows='6' maxlength='1000'
value = '".$descripcion."'
readonly>".($descripcion)."</textarea>
</td>";
$outputData .= '</tr>';
}
$outputData .= '</table>';
$outputTblEvolucion = '';
/****** build the pagination links ******/
// range of num links to show
$range = 3;
// if not on page 1, don't show back links
if ($currentPage > 1)
{
// show << link to go back to page 1
$outputTblEvolucion .= " <a href='{$_SERVER['PHP_SELF']}?currentPage=1'> << </a> ";
// get previous page num
$prevpage = $currentPage - 1;
// show < link to go back to 1 page
$outputTblEvolucion .= " <a href='{$_SERVER['PHP_SELF']}?currentPage=$prevpage'> Previous Page </a> ";
}
// loop to show links to range of pages around current page
for ($x = ($currentPage - $range); $x < (($currentPage + $range) + 1); $x++)
{
// if it's a valid page number...
if (($x > 0) && ($x <= $totalPages))
{
// if we're on current page...
if ($x == $currentPage)
{
// 'highlight' it but don't make a link
$outputTblEvolucion .= " [<b>$x</b>] ";
// if not current page...
} else
{
// make it a link
$outputTblEvolucion .= " <a href='{$_SERVER['PHP_SELF']}?currentPage=$x'>$x</a> ";
}
}
}
// if not on last page, show forward and last page links
if ($currentPage != $totalPages)
{
// get next page
$nextpage = $currentPage + 1;
// echo forward link for next page
//Here is which I think I am doing something wrong and I though to pass also my variable searchByRut but I am not able to received after pressing click on "Next Page"
$outputTblEvolucion .= " <a href='{$_SERVER['PHP_SELF']}?currentPage=$nextpage"
."&searchByRut=$searchByRut
'>Next Page</a> ";
// echo forward link for lastpage
$outputTblEvolucion .= " <a href='{$_SERVER['PHP_SELF']}?currentPage=$totalPages'>>></a> ";
} // end if
}
}
?>
<html>
<head>
<title>Pagination with Form</title>
</head>
<body>
<h1>Pagination with Form</h1>
<form id="frmPacienteBuscar" action="" method="post">
<fieldset>
<span class="error"><?= $searchByRut_error ?></span><br>
RUT: <input name="searchByRut" id="searchByRut" type="text" placeholder="Search by RUT.." tabindex="2" size="15" maxlength="8"
value="<?= $searchByRut ?>" > -
<input name="dv" id="dv" type="text" value="<?= $dv_input ?>" size="1" tabindex="2" maxlength="1"
value="<?= $dv_input ?>" > Sample: 12345678-1<br>
<button name="buscar" type="submit" id="contact-submit" data-submit="...Sending">Search</button><br>
</fieldset>
</form>
<?php
/*Here I will be printing the result of:
- the main table from tblPaciente
- the table which I required to do pagination tblEvolucion
- the links to navigate to the next and previous pages
*/
echo $outputTblPaciente;
echo "<br>";
echo $outputData;
echo "<br>";
echo $outputTblEvolucion;
?>
</body>
</html>