PHP Pagination for MySQL database - display the first page by default - php

Code:
$totalItemsRequired = 8;
$query = "SELECT * FROM fruits ORDER BY origin ASC ";
$result = mysqli_query($connection, $query);
if ($result == false)
{
echo "<p>Selecting all fruits failed.</p>";
}
else
{
$totalRecords = mysqli_num_rows($result);
$totalPages = ceil($totalRecords / $totalItemsRequired);
echo "<p>Page: ";
for ($i = 1; $i <= $totalPages; $i++)
{
echo "<a href='?page=" . $i . "'>" . $i . "</a> ";
}
echo "</p>";
if (isset($_GET['page']))
{
$currentPageNum = $_GET['page'];
$offset = ($currentPageNum - 1) * $totalItemsRequired;
$query = "SELECT * FROM fruits " . "LIMIT " . $offset . ", " . $totalItemsRequired;
$result = mysqli_query($connection, $query);
if ($result == false)
{
echo "<p>Selecting subset (page) of fruits failed.</p>";
}
else
{
echo "<ul>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo "<li>" . $row["id"] . "," . $row["name"] . ", " . $row["origin"] . ", " . $row["stock"] . "</li>";
}
echo "</ul>";
}
}
else
{
echo
// By default, load first page of records here
}
}
?>
Hi, I'm trying to load the first page of records by default where I have put the comment. There are no problems with the code, and the initial output is good. I'm just not sure how to show by default. Can anyone help please?
EDIT: Can anyone help?

Why not use LIMIT clause in your MySQL statement?
SELECT * FROM fruits ORDER BY origin ASC LIMIT 8 //First 8 rows of the table
SELECT * FROM fruits ORDER BY origin ASC LIMIT 8,8 //Second 8 rows
SELECT * FROM fruits ORDER BY origin ASC LIMIT 16,8 //Third 8 rows
In General:
LIMIT offset,row_count will return row_count rows starting from offset+1 row.

Instead of if statement, you should just be able to change
$currentPageNum = $_GET['page'];
$offset = ($currentPageNum - 1) * $totalItemsRequired;
to
$currentPageNum = isset($_GET['page']) ? $_GET['page'] : 0;
$offset = $currentPageNum > 0 ? (($currentPageNum - 1) * $totalItemsRequired) : 0;
Full code:
<?php
$totalItemsRequired = 8;
$query = "SELECT * FROM fruits ORDER BY origin ASC ";
$result = mysqli_query($connection, $query);
if ($result == false)
{
echo "<p>Selecting all fruits failed.</p>";
}
else
{
$totalRecords = mysqli_num_rows($result);
$totalPages = ceil($totalRecords / $totalItemsRequired);
echo "<p>Page: ";
for ($i = 1; $i <= $totalPages; $i++)
{
echo "<a href='?page=" . $i . "'>" . $i . "</a> ";
}
echo "</p>";
$currentPageNum = isset($_GET['page']) ? $_GET['page'] : 0;
$offset = $currentPageNum > 0 ? (($currentPageNum - 1) * $totalItemsRequired) : 0;
$query = "SELECT * FROM fruits " . "LIMIT " . $offset . ", " . $totalItemsRequired;
$result = mysqli_query($connection, $query);
if ($result == false)
{
echo "<p>Selecting subset (page) of fruits failed.</p>";
}
else
{
echo "<ul>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo "<li>" . $row["id"] . "," . $row["name"] . ", " . $row["origin"] . ", " . $row["stock"] . "</li>";
}
echo "</ul>";
}
}
}
?>

Related

Check if two ranges intersect in a database with php

I have a database that has 2 columns Left_From and Left_To I need to basically check both columns with each row in the database to see if there is any overlapping ranges. So lets say there are 37 rows returned I need to check each row 37 times. I have tried array_intersect() with ranges and multiple loops. I have also tried BETWEEN in mysql but that does do what I need it to either. When I tried the between method I have two loops that I thought would take these two
$newstart[$crow] = $row['LEFT_FROM'];
$newend[$crow] = $row['LEFT_TO'];
and compare them to the new data that would be looped through 37 times because of $row2.
//attempt at BETWEEN
$newstart = array();
$newend = array();
$crow = 0;
while ($row = mysqli_fetch_array($get)) {
$newstart[$crow] = $row['LEFT_FROM'];
$newend[$crow] = $row['LEFT_TO'];
while ($row2 = mysqli_fetch_array($get)) {
$newsql = "SELECT * FROM database+table WHERE tablename = 'something' AND
LEFT_FROM BETWEEN " . $newstart[$crow] . " AND " . $newend[$crow] . " OR
LEFT_TO BETWEEN " . $newstart[$crow] . " AND " . $newend[$crow] . " OR ".
$newstart[$crow] . " BETWEEN " . "LEFT_FROM" . " AND ". " LEFT_TO";
$result = mysqli_query($GLOBALS['Con'], $newsql);
if (!$result) {
echo "Error: " . mysqli_error($GLOBALS['Con']) . "<br>";
}
if (!empty($result)) {
echo "Overlap: ". "Row2 LEFT_FROM: " . $row2['LEFT_FROM'] . " NewStart: " . $newstart[$crow] . " Row2 LEFT_TO: " . $row2['LEFT_TO'] . " Newend: " . $newend[$crow] . "<br>" . "Crow: " . $crow . "<br>" ;
}
}
echo "Crow: " . $crow . "<br>";
$crow++;
}
atempt at array_intersect all variables in arrays are set to 0
$result = mysqli_query($GLOBALS['con'], $select);
$Rows = $result->num_rows;
$Rows2 = $Rows;
$Rows3 = $Rows;
$Rows4 = $Rows;
$Rows5 = $Rows;
$range1 = array();
$range2 = array();
$range3 = array();
$range4 = array();
while ($counter <= $Rows) {
$range1[$crow] = $leftfrom[$add];
$range2[$crow] = $leftto[$add];
$counter++;
$road++;
$crow++;
}
while ($counter2 <= $Rows2) {
$range3[$crow2] = $leftfrom[$add2];
$range4[$crow2] = $leftto[$add2];
$counter2++;
$road3++;
$crow2++;
}
$Combined1 = array();
$Combined1 = array();
while ($counter3 <= $Rows3) {
$Combined1[$crow5] = range($range1[$crow3], $range2[$crow3]);
$Combined2[$crow6] = range($range3[$crow3], $range4[$crow3]);
$crow5++;
$crow6++;
$crow3++;
$counter3++;
}
$check1 = 0;
while ($check1 <= $Rows4) {
$GLOBALS['check2'] = 0;
$Rows6 = $GLOBALS['check2'] + 1;
while ($GLOBALS['check2'] <= $Rows5) {
$results = array_intersect($Combined1[$check1],$Combined2[$Rows6]);
if ($results) {
$start = reset($results);
$end = end($results);
echo "These are overlapping" . "<br>";
echo "Start of overlap: " . $start . " Rows#: " . $check1 . " Bad Row: " . $GLOBALS['check2'];
echo "<br>";
echo "end of overlap: " . $end;
echo "<br>" . $GLOBALS['check2'] ;
$GLOBALS['check2']++;
}else{
echo "<br>" . $check1 . " No duplicates" . "<br>";
$GLOBALS['check2']++;
}
}
$check1++;
}
You don't need a loop in PHP, you can do it entirely by joining the table with itself.
SELECT *
FROM yourTable AS t1
JOIN yourTable AS t2
ON t1.id < t2.id
AND (t1.left_from <= t2.left_to AND t2.left_from <= t1.left_to)
t1.id < t2.id keeps it from treating a row as overlapping with itself (and using < rather than != keeps it from showing the same pair of rows twice). Replace id with the primary key of your table.
To check, if two number ranges overlap, you could use the simple test x1 < y2 AND y1 < x2. This could be combined in a single sql statement.
SELECT
t1.LEFT_FROM as LEFT_FROM_1,
t1.LEFT_TO as LEFT_TO_1,
t2.LEFT_FROM as LEFT_FROM_2,
t2.LEFT_TO as LEFT_TO_2
FROM
t t1, t t2
WHERE
t1.LEFT_FROM < t2.LEFT_TO AND t2.LEFT_FROM < t1.LEFT_TO
This query would return all overlapping ranges.

PHP Simple Pagination

the below code is getting some values from DB by "select option form" , i recently added Pagination snip to limit the results, when i run the code it fetch 5 recorders as defined,but didn't show the remaining number of pages.
what im doing wrong here ?
<?php
$per_page = 5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = 1;
}
$start_from = ($page - 1) * $per_page;
if (!empty($_POST['form_val']) && isset($_POST['form_val'])) {
$_POST['form_val'] = 0;
$sql = "SELECT u.log_id , u.user_name, s.site, u.date ,u.comment , l.location, e.picture FROM `pool` u, `location_all` l , `site_all` s JOIN db2.user e
where l.location_id = u.location and s.site_id = u.site and e.user_id = u.user_id";
if (!empty($_POST['Location']) && isset($_POST['Location'])) {
$sql = $sql . " AND location =" . $_POST['Location'];
}
$strtdate = $_POST['Sday'];
$enddate = $_POST['Eday'];
if (!empty($_POST['Sday']) && isset($_POST['Sday']) && !empty($_POST['Eday']) && isset($_POST['Eday'])) {
$sql = $sql . " AND date between '" . $strtdate . "' and '" . $enddate . "'";
} elseif (!empty($_POST['Sday']) && isset($_POST['Sday'])) {
$sql = $sql . " AND date>='" . $strtdate . "'";
} elseif (!empty($_POST['Eday']) && isset($_POST['Eday']))
$sql = $sql . " AND date<='" . $enddate . "'";
}
if (!empty($_POST['Site']) && isset($_POST['Site'])) {
$sql = $sql . " AND u.site=" . $_POST['Site'];
}
$sql = $sql . " LIMIT $start_from, $per_page";
if (mysqli_query($conn, $sql)) {
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) >= 1) {
$rowcount = mysqli_num_rows($result);
echo '<legend> ' . $rowcount . ' Records Found !!!</legend>';
echo '<br><br>';
echo "<table class='srchtable'>
<tr>
<th>Picture</th>
<th>Date</th>
<th>User Name</th>
<th>country</th>
<th>Location</th>
<th>Site</th>
<th>Comment</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td> <img src='" . $row['picture'] . "' alt='' style='width:70%; height:auto; border-radius: 50%;'> </td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['user_name'] . "</td>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['site'] . "</td>";
echo "<td>" . $row['comment'] . "</td>";
echo "</tr>";
}
echo "</table>";
$total_pages = ceil($rowcount / $per_page);
echo "<center><a href='?page=1'>" . 'First Page' . "</a> ";
for ($i = 1; $i <= $total_pages; $i++) {
echo "<a href='?page=" . $i . "'>" . $i . "</a> ";
}
echo "<a href='?page=$total_pages'>" . 'Last Page' . "</a></center> ";
} else {
echo '<p>No Results Found !!!</p>';
}
}
}
?>
As I said in my comments, for displaying pagination links:
You're counting total number of rows but incorporating LIMIT and OFFSET clauses in your SELECT query, this won't give the correct number of row count. Your SELECT query should not contain this part, ... LIMIT $start_from, $per_page.
Since you're filtering the results based on several $_POST data, you should incorporate those conditions in your pagination links as well, otherwise when you visit a different page(through pagination link), you won't get the desired result, and that's because $_POST data will not be retained when you hop from page to page. Better that you change the method of your <form> from POST to GET, because in this way it'd be easier for you to catch and manipulate things when you hop from one page to another using pagination links.
So based on the above points, your code should be like this:
$per_page = 5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = 1;
}
$start_from = ($page - 1) * $per_page;
if (!empty($_GET['form_val']) && isset($_GET['form_val'])) {
$_GET['form_val'] = 0;
$sql = "SELECT u.log_id , u.user_name, s.site, u.date ,u.comment , l.location, e.picture FROM `pool` u, `location_all` l , `site_all` s JOIN db2.user e
where l.location_id = u.location and s.site_id = u.site and e.user_id = u.user_id";
if (!empty($_GET['Location']) && isset($_GET['Location'])) {
$sql = $sql . " AND location =" . $_GET['Location'];
}
$strtdate = $_GET['Sday'];
$enddate = $_GET['Eday'];
if (!empty($_GET['Sday']) && isset($_GET['Sday']) && !empty($_GET['Eday']) && isset($_GET['Eday'])) {
$sql = $sql . " AND date between '" . $strtdate . "' and '" . $enddate . "'";
} elseif (!empty($_GET['Sday']) && isset($_GET['Sday'])) {
$sql = $sql . " AND date>='" . $strtdate . "'";
} elseif (!empty($_GET['Eday']) && isset($_GET['Eday'])) {
$sql = $sql . " AND date<='" . $enddate . "'";
}
if (!empty($_GET['Site']) && isset($_GET['Site'])) {
$sql = $sql . " AND u.site=" . $_GET['Site'];
}
$data_query = $sql . " LIMIT $start_from, $per_page";
$result = mysqli_query($conn, $data_query);
if (mysqli_num_rows($result) >= 1) {
$rowcount = mysqli_num_rows($result);
echo '<legend> ' . $rowcount . ' Records Found !!!</legend>';
echo '<br><br>';
echo "<table class='srchtable'>
<tr>
<th>Picture</th>
<th>Date</th>
<th>User Name</th>
<th>country</th>
<th>Location</th>
<th>Site</th>
<th>Comment</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td> <img src='" . $row['picture'] . "' alt='' style='width:70%; height:auto; border-radius: 50%;'> </td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['user_name'] . "</td>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['site'] . "</td>";
echo "<td>" . $row['comment'] . "</td>";
echo "</tr>";
}
echo "</table>";
$query_result = mysqli_query($conn, $sql);
$total_rows = mysqli_num_rows($query_result);
$total_pages = ceil($total_rows / $per_page);
parse_str($_SERVER["QUERY_STRING"], $url_array);
unset($url_array['page']);
$url = http_build_query($url_array);
?>
<center>First Page
<?php
for ($i = 1; $i <= $total_pages; $i++) {
?>
<?php echo $i; ?>
<?php
}
?>
Last Page</center>
<?php
} else {
echo '<p>No Results Found !!!</p>';
}
}

Else statement doesn't work in option select

I am trying to implement a dropdown search option. All my search results are working. All the commands that I have assigned to if statements work, but when it does to else it deosn't work.
Here is my code:
if(isset($_REQUEST['submit'])){
$opt = $_POST['opt'];
if($opt==1){//if opt = 1
$sqle = "SELECT * FROM tbl_events WHERE title LIKE '%{$keywords}%'";
$resulte = mysql_query($sqle,$con) or die(mysql_error());
while($row=mysql_fetch_array($resulte)){
echo "<h4>" . $row['title'] . "</h4><br/>";
echo "<p>" . $row['description'] . "<p>";
}
}else if($opt==2){//if opt = 2
$sqls = "SELECT * FROM tbl_games WHERE games_name LIKE '%{$keywords}%'";
$results = mysql_query($sqls,$con)or die(mysql_error());
while($row=mysql_fetch_array($results)){
echo "<h4>" . $row['games_name'] . "</h4><br/>";
echo "<p>" . $row['description'] . "<p>";
}
}else{
echo "Your Searched keyword did not match";
}
}
What to do?
Try this: Take a flag to check if record exists.
$flag = false;
if($opt==1){//if opt = 1
$sqle = "SELECT * FROM tbl_events WHERE title LIKE '%{$keywords}%'";
$resulte = mysql_query($sqle,$con) or die(mysql_error());
if(mysql_num_rows($resulte) > 0) {
$flag = true;
while($row=mysql_fetch_array($resulte)){
echo "<h4>" . $row['title'] . "</h4><br/>";
echo "<p>" . $row['description'] . "<p>";
}
}
}else if($opt==2){//if opt = 2
$sqls = "SELECT * FROM tbl_games WHERE games_name LIKE '%{$keywords}%'";
$results = mysql_query($sqls,$con)or die(mysql_error());
if(mysql_num_rows($resulte) > 0) {
$flag = true;
while($row=mysql_fetch_array($results)){
echo "<h4>" . $row['games_name'] . "</h4><br/>";
echo "<p>" . $row['description'] . "<p>";
}
}
}
if(!$flag){
echo "Your Searched keyword did not match";
}

PHP $_GET from url on same page

I'm doing some pagination in PHP but i run into some problems. I did pagination, and everything is working well but what i wanted is to add a form on the page in which user could type in how much items he wants on each page. The problem is, when i enter number of pages (eg. 3), it works fine for the first time and lists 3 items, but when i click on "next page" it again lists default number of elements (2). I've searched everything and anything but can't really find what seems to be a problem. I'm kinda new to php so i guess that i don't get some things yet, but I hope it's not too much of a trouble for you to take look at this and tell me what you think. Thanks in advance!
I did it like this:
form:
<?php
print("
<form style='padding-left:5px;' method='get'>Broj komponenti po stranici:<input type='text' name='cpp' /><input class='button3' type='submit' name='cppb' value='Promijeni' /></form>
");
check if there is some thing stored in cpp:
$rec_limit = ($_GET['cpp']);
if(!(isset($_GET['cpp']))) { $rec_limit = 2; } //if there is no cpp set, let it be 2 (default)
right here is database and pagination part (i don't think that this is the part that makes troubles):
$sql = "SELECT count(id) FROM komponenta ";
$retval = mysql_query($sql, $conn);
if(!$retval) {
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval, MYSQL_NUM );
$rec_count = $row[0];
$max_pages = ($rec_count / $rec_limit) -1;
if(isset($_GET{'page'}) ) {
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
} else {
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT * ".
"FROM komponenta ".
"LIMIT $offset, $rec_limit";
$retval = mysql_query($sql, $conn);
if(!$retval) {
die('Could not get data: ' . mysql_error());
}
print("<TABLE class='tablica' border=‘1’>");
print("<TR>");
print("<TD></TD>");
print("<TD>Vrsta</TD>");
print("<TD>Proizvođač</TD>");
print("<TD>Frekv</TD>");
print("<TD>Izlazna</TD>");
print("<TD>Dobavljač</TD>");
print("<TD>Status</TD>");
print("<TD>Datum kupnje</TD>");
print("<TD>Datum zaprimanja</TD>");
print("<TD>Opis</TD>");
print("<TD>Napomena</TD>");
print("<TD>Komada</TD>");
print("<TD>Metara</TD>");
print("</TR>");
$br=0;
while($row = mysql_fetch_array($retval, MYSQL_NUM)) {
print("<TR>");
print("<TD>". $br . "</TD>");
print("<TD>". $row["1"]. "</TD>");
print("<TD>" . $row["2"]. "</TD>");
print("<TD>" . $row["3"]. "</TD>");
print("<TD>" . $row["4"]. "</TD>");
print("<TD>" . $row["5"]. "</TD>");
print("<TD>" . $row["6"]. "</TD>");
print("<TD>" . $row["7"]. "</TD>");
print("<TD>" . $row["8"]. "</TD>");
print("<TD>" . $row["9"]. "</TD>");
print("<TD>" . $row["10"]. "</TD>");
print("<TD>" . $row["11"]. "</TD>");
print("<TD>" . $row["12"]. "</TD>" );
print("</TR>");
$br++;
}
print("</TABLE>");
and here is the part that bothers me:
if( $page >= $max_pages ) {
$last = $page - 2;
echo "<b class='paragraf1'>Page: ". $page ." </b><a class='button3' href=\"protected_page.php?page=$last?cpp=$rec_limit\"><text class='buttontxt'>Last " . $rec_limit . " Records</text></a>";
/*with ?cpp=$rec_limit i set the url
(when i press "next") to have for eg ..."?cpp=3"... and it does appear so,
but the $_GET['cpp'] from the beggining of code obviously doesn't read this..
Well, at least it doesn't store it into $rec_limit*/
} else if($page > 0) {
$last = $page - 2;
echo "<b class='paragraf1'>Page: ". $page ." </b><a class='button3' href=\"protected_page.php?page=$last?cpp=$rec_limit\"><text class='buttontxt'>Last " . $rec_limit . " Records</text></a> ";
echo "<a class='button3' href=\"protected_page.php?page=$page?cpp=$rec_limit\"><text class='buttontxt'>Next " . $rec_limit . " Records</text></a>";
} else if($page == 0) {
echo "<b class='paragraf1'>Page: ". $page ." </b><a class='button3' href=\"protected_page.php?page=$page?cpp=$rec_limit\"><text class='buttontxt'>Next " . $rec_limit . " Records</text></a>";
}
protected_page.php?page=$page?cpp=$rec_limit
replace the 2nd ? by &
protected_page.php?page=$page&cpp=$rec_limit
When you're passing variables in the url after the script name there is a ? then between each variable and the other it is and & that separates.
and at your place I would replace that too:
if(!(isset($_GET['cpp']))){$rec_limit=2;}
by:
if(!isset($_GET['cpp']) || (int)$rec_limit<=0){ $rec_limit=2; }

Retrieving records from the table with reference

i have this code of Pagination, actually it will retrieve all the records in my table student, what i want is to retrieve records with a reference by year level. here is the code:
thanks for your help!
<?php
if (!isset($_POST['level'])) {
$_POST['level'] = "undefine";
}
$level = $_POST['level'];
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("kp_and_harang") or die(mysql_error());
if (!(isset($_GET['pagenum']))) {
$pagenum = 1;
} else {
$pagenum = $_GET['pagenum'];
}
$data = mysql_query("SELECT id,surname,firstname,middlename,level FROM students") or die(mysql_error());
$rows = mysql_num_rows($data);
$page_rows = 2;
$last = ceil($rows / $page_rows);
if ($pagenum < 1) {
$pagenum = 1;
} elseif ($pagenum > $last) {
$pagenum = $last;
}
$max = 'limit ' . ($pagenum - 1) * $page_rows . ',' . $page_rows;
$data_p = mysql_query("SELECT *,LPAD(id,4,'0') as id FROM students $max ") or die(mysql_error());
while ($info = mysql_fetch_array($data_p)) {
echo "<tr class='tr1'>";
echo "<center>";
echo "<td class='1'>" . $info['id'] . "</td>";
echo "<td class='1'>" . $info['surname'] . ", " . $info['firstname'] . " " . $info['middlename'] . "</td>";
echo "<td class='1'>" . $info['level'] . "</td>";
echo "</center>";
echo "</tr>";
echo "<br>";
}
echo "<p>";
echo " --Page $pagenum of $last-- <p>";
if ($pagenum == 1) {
} else {
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo "----";
$previous = $pagenum - 1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}
echo " ---- ";
if ($pagenum == $last) {
} else {
$next = $pagenum + 1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo "----";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?>
I have rewrote your code to use mysqli.
As well, you just needed to add a "where" to you sql query.
<?php
if (!isset($_POST['level'])) {
$_POST['level'] = "undefined";
}
$mysqli = new mysqli("localhost", "user", "password", "database");
$level = $mysqli->real_escape_string($_POST['level']);
if (!(isset($_GET['pagenum']))) {
$pagenum = 1;
} else {
$pagenum = $_GET['pagenum'];
}
$query = "SELECT id,surname,firstname,middlename,level FROM students";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$rows = $result->num_rows;
$page_rows = 2;
$last = ceil($rows / $page_rows);
if ($pagenum < 1) {
$pagenum = 1;
} elseif ($pagenum > $last) {
$pagenum = $last;
}
$max = 'limit ' . ($pagenum - 1) * $page_rows . ',' . $page_rows;
if ($level == "undefined"){
$query = "SELECT *,LPAD(id,4,'0') as id FROM students $max" ORDER BY level;}
else{
$query = "SELECT *,LPAD(id,4,'0') as id FROM students $max where level = '$level' ORDER BY level";}
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
while ($info = $result->fetch_array(MYSQLI_BOTH)) {
echo "<tr class='tr1'>";
echo "<center>";
echo "<td class='1'>" . $info['id'] . "</td>";
echo "<td class='1'>" . $info['surname'] . ", " . $info['firstname'] . " " . $info['middlename'] . "</td>";
echo "<td class='1'>" . $info['level'] . "</td>";
echo "</center>";
echo "</tr>";
echo "<br>";
}
echo "<p>";
echo " --Page $pagenum of $last-- <p>";
if ($pagenum == 1) {
} else {
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo "----";
$previous = $pagenum - 1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}
echo " ---- ";
if ($pagenum == $last) {
} else {
$next = $pagenum + 1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo "----";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?>
As well, this code was meant to match your code as close as possible, however... a much better way would be to make level an int value, as well using a case with default being the line $query = "SELECT *,LPAD(id,4,'0') as id FROM students $max";.

Categories