pagination issue with user display per page - php

I was following a tutorial on basic php tables to show the users with pagination:
http://www.killersites.com/community/index.php?/topic/1969-basic-php-system-vieweditdeleteadd-records/
the problem that i have is my platform does not support MySQL so i change it to MySQLi
the second problem is that the it should display only one user per page but it just show all of them in 1 page and when click on for example on page 2 it is show me error page
this is an image to better description :
http://store2.up-00.com/2014-02/1391920754996.jpg
and this is the code that i am using :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
</head>
<body>
<?php
/*
VIEW-PAGINATED.PHP
Displays all data from 'players' table
This is a modified version of view.php that includes pagination
*/
// connect to the database
include('config.php');
// number of results to show per page
$per_page = 1;
// figure out the total pages in the database
$result = mysqli_query($connecDB,"SELECT * FROM users");
$total_results = mysqli_num_rows($result);
$total_pages = ceil($total_results / $per_page);
// check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
echo "<p> | <b>View Page:</b> ";
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a href='admin_user_list.php?page=$i'>$i</a> ";
}
echo "</p>";
// display data in table
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
// echo out the contents of each row into a table
echo "<tr>";
while($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
$fname = $row['first_name'];
$lname = $row['last_name'];
echo "<tr><td>";
echo $id;
echo "</td>";
echo "<td>";
echo $fname;
echo "<td>";
echo $lname;
echo "</td></tr>";
}
echo "</td></tr>";
}
// close table>
echo "</table>";
// pagination
?>
<p>Add a new record</p>
</body>
</html>

I couldn't fix your existing script, so I found a pagination script that I modified and has worked for me.
You will undoubtingly want to modify it, but it works.
Just change the DB credentials and other things you will find throughout the script.
There are a few comments in it also on commented-out options.
<?php
$DB_HOST = "xxx";
$DB_NAME = "xxx";
$DB_PASS = "xxx";
$DB_USER = "xxx";
$db = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($db->connect_errno > 0) {
die('Connection failed [' . $db->connect_error . ']');
}
$sql2 = "SELECT COUNT(id) FROM users ";
$query2 = mysqli_query($db, $sql2);
$row = mysqli_fetch_row($query2);
// Here we have the total row count
$rows = $row[0];
// This is the number of results we want displayed per page
$page_rows = 1;
// This tells us the page number of our last page
$last = ceil($rows/$page_rows);
// This makes sure $last cannot be less than 1
if($last < 1){
$last = 1;
}
// Establish the $pagenum variable
$pagenum = 1; // do not change this
// Get pagenum from URL vars if it is present, else it is = 1
if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
// This makes sure the page number isn't below 1, or more than our $last page
if ($pagenum < 1) {
$pagenum = 1;
} else if ($pagenum > $last) {
$pagenum = $last;
}
// This sets the range of rows to query for the chosen $pagenum
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
// This is your query again, it is for grabbing just one page worth of rows by applying $limit
$sql = "SELECT id, first_name, last_name FROM users ORDER BY id ASC $limit";
$query = mysqli_query($db, $sql);
// This shows the user what page they are on, and the total number of pages
$textline1 = "Names (<b>$rows</b>)";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
// Establish the $paginationCtrls variable
$paginationCtrls = '';
// If there is more than 1 page worth of results
if($last != 1){
/* First we check if we are on page one. If we are then we don't need a link to
the previous page or the first page so we do nothing. If we aren't then we
generate links to the first page, and to the previous page. */
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= 'Previous ';
// Render clickable number links that should appear on the left of the target page number
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= ''.$i.' ';
}
}
}
// Render the target page number, but without it being a link
$paginationCtrls .= ''.$pagenum.' ';
// Render clickable number links that should appear on the right of the target page number
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= ''.$i.' ';
if($i >= $pagenum+4){
break;
}
}
// This does the same as above, only checking if we are on the last page, and then generating the "Next"
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= ' Next ';
}
}
$dynamicList = '';
// display data in table
echo "<table border='1' cellpadding='10'>";
// echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th>";
echo "<tr>";
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
/*
$id = $row["id"];
$product_name = $row["first_name"];
$price = $row["last_name"];
*/
$id = $row['id'];
$fname = $row['first_name'];
$lname = $row['last_name'];
echo "<tr><td>";
echo $id;
echo "</td>";
echo "<td>";
echo $fname;
echo "<td>";
echo $lname;
echo "</td></tr>";
// you can use and modify this below
// the <!-- and --> tags can be taken out. Those are regular HTML comment tags.
$dynamicList .= "
<!--
<li><div class='product'>
<a href='pager.php?id=$id' class='info'>
<span class='holder'>
<img src='images/$id.jpg' alt='$product_name' />
<span class='book-name'>$product_name</span>
</a>
<a href='pager.php?id=$id' class='buy-btn'> (link) <span class='price'>$price</span></a>
</div>
</li>
-->
";
}
// Close your database connection
mysqli_close($db);
?>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body{ font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;}
div#pagination_controls{font-size:21px;}
div#pagination_controls > a{ color:#06F; }
div#pagination_controls > a:visited{ color:#06F; }
</style>
</head>
<body>
<div>
<h2><?php echo $textline1; ?> Paged</h2>
<p><?php echo $textline2; ?></p>
<p><?php echo $dynamicList; ?></p>
<div id="pagination_controls"><?php echo $paginationCtrls; ?></div>
</div>
</body>
</html>

Related

How to Create a pagination of 5 page numbers where the selected page appears in the middle if possible?

I'm trying to create a pagination where the selected page is at the center (if possible). However my code does not work. Below the image shows the output when selecting different pages.
In the first 2 page it works well, however problem starts when clicked on page 6, it shows page 8 however only 7 pages exist. When clicked on page 7 the page 7 marks also vanishes.
In the middle 2 pages the selected page no appears in the middle, which is correct however.
<?php
include 'backend_connection.php';
$sql = "SELECT count(`id`) FROM `activity`";
$query = mysqli_query($con, $sql);
$row = mysqli_fetch_row($query);
$totalRows = $row[0];
$page_rows = 5;
$lastPage = ceil($totalRows/$page_rows);
if($lastPage < 1){
$lastPage = 1;
}
if(isset($_GET['page_no'])){
$currPage = preg_replace('[^0-9]', '', $_GET['page_no']);
} else {
$currPage = 1;
}
if ($currPage < 1) {
$currPage = 1;
} else if ($currPage > $lastPage) {
$currPage = $lastPage;
}
$limit = 'LIMIT ' .($currPage - 1) * $page_rows .',' .$page_rows;
$sql = "SELECT `id`, `name` FROM `activity` ORDER BY name $limit";
$query = mysqli_query($con, $sql);
$paginationCtrls = '';
if($lastPage != 1){
if ($lastPage>1 && $lastPage<5) {
for($i=1;$i<=$lastPage;$i++) {
if ($i==$currPage)
$paginationCtrls .= '<b>'.$i.'</b> ';
else
$paginationCtrls .= ''.$i.' ';
}
} else {
if ($currPage>3)
$firstPage=$currPage-2;
else
$firstPage=1;
$endPage=$firstPage+5;
if ($currPage>$lastPage-1) {
$firstPage=$currPage-5;
$endPage=$lastPage;
}
for($i=$firstPage;$i<$endPage;$i++) {
if ($i==$currPage)
$paginationCtrls .= '<b>'.$i.'</b> ';
else
$paginationCtrls .= ''.$i.' ';
}
}
}
$list = '';
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$name = $row["name"];
$list .= '<p><a href="details.php?id='.$id.'">'.$name.'</p>';
}
mysqli_close($con);
?>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
div#pagination_controls { font-size:21px; }
div#pagination_controls > a{ color:#06F; min-width: 70px; }
div#pagination_controls > a:visited{ color:#06F; min-width: 70px;}
</style>
</head>
<body>
<div>
<h2><?php echo 'Total rows '.$totalRows; ?></h2>
<p><?php echo 'Pages '.$currPage.' of '.$lastPage; ?></p>
<p><?php echo $list; ?></p>
<div id="pagination_controls"><?php echo $paginationCtrls; ?></div>
</div>
</body>
</html>

Links does not show correct data

I am facing a problem, and i don't know how to fix it.
If i click a link generated from this php, the results are same as displayed on 1st page. For example view.php?page=2 displays same data as view.php?page=1 or 3, etc.
That happens for 'view.php?ID=' aswell.
<?php
require_once('connection.php');
require_once('auth.php');
echo "<br /><br /><font color=red size='6'><center>Facturi emise</center></font><br /><br />";
$query = "SELECT * FROM out_fact ORDER BY ID DESC LIMIT 0, 20";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
if (mysql_num_rows($result) > 0) {
echo "<table cellpadding=10 border=1 align=center>";
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>"."<a href=javascript:window.open('view.php?ID=".$row[0]."','NAME','location=false')><font color=blue size='4'><center>".$row[1]." din data de ".$row[2]."</a></center>"."</font></td>";
echo "<td>"."<font color=red size='4'><center>".$row[3]." ".$row[12]."</center></font></td>";
echo "</tr>";
};
echo "</table>";
}
else {
echo "No rows found!";
}
echo "<tr><td> <a href='view.php?ID=".$row[0]."'>".$row[1]."</a></td></tr>";
mysql_free_result($result);
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 20;
$sql = "SELECT COUNT(ID) FROM out_fact";
$query = mysql_query($sql);
$result = mysql_fetch_row($query);
$total_records = $result[0];
$total_pages = ceil($total_records / 20);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='view.php?page=".$i."'>".$i."</a> ";
};
?>
How to post correct data, or what's missing. I'm new in php coding...
Thanks in advance.
Try at least using MySQLi instead of deprecated MySQL. And what you are trying to do is called Pagination.
connection.php:
<?php
$con=mysqli_connect("YourHost","YourUsername","YourPassword","YourDatabase"); /* REPLACE THE NECESSARY HOST, USERNAME, PASSWORD, AND DATABASE */
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
?>
Your main file:
<?php
include('connection.php');
require_once('auth.php');
echo "<br><br><font color=red size='6'><center>Facturi emise</center></font><br><br>";
$query = "SELECT * FROM out_fact ORDER BY ID DESC";
$result = mysqli_query($con,$query);
$count=mysqli_num_rows($result);
$r = mysqli_fetch_row($result);
$numrows = $r[0];
$rowsperpage = 10; /* NUMBER OF ROWS TO SHOW PER PAGE */
$totalpages = ceil($count / $rowsperpage);
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
$currentpage = (int) $_GET['currentpage'];
} else {
$currentpage = 1;
}
if ($currentpage > $totalpages) {
$currentpage = $totalpages;
}
if ($currentpage < 1) {
$currentpage = 1;
}
$offset = ($currentpage - 1) * $rowsperpage;
$result=mysqli_query($con,"SELECT * FROM out_fact ORDER BY ID DESC LIMIT $offset,$rowsperpage");
echo "<table cellpadding=10 border=1 align=center>";
while($row = mysqli_fetch_array($result)){
/* JUST REPLACE THE NECESSARY DATA YOU WANT TO INPUT INSIDE THIS WHILE LOOP */
$rowdata=mysqli_real_escape_string($con,$row[0]);
echo "<tr>";
echo "<td><a href='view.php?ID=$rowdata&location=false' target='_blank'><font color=blue size='4'><center>".$row[1]." din data de ".$row[2]."</a></center>"."</font></td>"; /* JUST REPLACE THE NECESSARY DATA IN THE LINK IF YOU MUST */
echo "<td>"."<font color=red size='4'><center>".$row[3]." ".$row[12]."</center></font></td>";
echo "</tr>";
} /* END OF WHILE LOOP */
if($count==0){
echo "<tr><td></td><td>No results found.</td><td></td></tr>";
}
else { /* START OF PAGINATION LINK */
echo '<tr height="30px;" valign="bottom"><td></td><td>';
echo "<table style='border-collapse:separate; border-spacing:3px;'><tr>";
/****** build the pagination links ******/
$range = 2;
if ($currentpage > 1) {
$prevpage = $currentpage - 1;
echo "<td style='width:70px; background-color:fff; border:solid #08c 1px; font-size:14px;' align='center'> <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage' style='background-color:fff;'>Previous</a> </td>";
}
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo "<td style='width:20px; background-color:fff; font-size:14px; border:solid #ccc 2px;' align='center'> <font color='#ccc'><b>$x</b></font> </td>";
} else {
echo "<td style='width:20px; background-color:fff; font-size:14px; border:solid #08c 1px;' align='center'> <a href='{$_SERVER['PHP_SELF']}?currentpage=$x' style='background-color:fff;'>$x</a> </td>";
}
}
}
if ($currentpage != $totalpages) {
$nextpage = $currentpage + 1;
echo "<td style='width:70px; background-color:fff; font-size:14px; border:solid #08c 1px;' align='center'> <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage' style='background-color:fff;'>Next</a> </td>";
} // end if
/****** end build pagination links ******/
echo "</tr></table></td></tr>";
} /* END OF ELSE IF COUNT 0 */
echo '</table>';
?>

PHP MySQL Paging

I tried so many different solutions. Im new to php since 1 week, used ASP 12 years ago so I hope I can get some help.
Everything down here works fine. But there are around 1000 rows in the db and I need to split them up in pages.
<?php
$con = mysqli_connect("localhost","test","test","test")or die('could not connect to database');
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "<table border='0'>
<tr>
<th>Img:</th>
<th>Text:</th>
</tr>";
$result = mysqli_query($con,"SELECT Jokes.ID, Categories.CategoryName, Jokes.CategoryID, Jokes.JokeText FROM Jokes LEFT JOIN Categories ON Jokes.CategoryID = Categories.ID ORDER BY Jokes.JokeText");
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td align='center'><img src='webimg/" . $row['CategoryName'] . ".png' height='35' width='35'></td>";
echo "<td align='left' width='80%'>" . $row['JokeText'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Kind Regards.
You can use MySQL LIMIT for that.
I've used to do it like this:
Get the total number of rows you're paging and have a parameter like in the URL, i.e. "/p/5" or ?page=5 (I will use this for reference, easier to write code and for you to understand) for page no. 5, also do a failsafe, like this:
Say you have 10 records per page:
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$records_per_page = 10;
And, in your SQL you will have something like this
$start = ($page-1) * $records_per_page;
$result = mysql_query("select * from table limit {$start}, {$records_per_page}");
Kind of crude, but you should get the point and be on the right path.
For building your pagination links... that's totally up to you. You should get the total amount of rows with a "select count (PRIMARY_KEY) from table" query prior, so you can calculate the max number of pages.
What you're searching for is the LIMIT of mysql.
The usage is very simple.
For example:
"SELECT * FROM tablename LIMIT 3"
This will give you the first three results.
In your case, you need an offset, depends on the current page:
"SELECT * FROM tablename LIMIT offset,results"
The offset can be calculated, depends on how many results you want each page.
"SELECT * FROM tablename LIMIT 20,10"
This will display 10 results, start at result 20. This could be for the 3. site if you want 10 results each site.
<?php
$per_page = 10; //no. of results to display in one page
$pages_query = mysql_query("SELECT COUNT('id') FROM JOKES");//Or whatever field. this is just to check the number of results.
$pages = ceil(mysql_result($pages_query, 0) / $per_page);//to get the total no. of pages that will be there. For example if u have 60 results than no. og pages will be 60/10=6
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;// if the page variable in your url is set that means that u have clicked a page number. So it will be taking that page number and displaying those results
$start = ($page - 1) * $per_page;//to get the starting result of that page. If you are in say 6th page, then the starting element would be 6-1*10=50. This makes sure that the results in the previous pages are not displayed
$query = mysql_query("SELECT * FROM JOKES LIMIT $start, $per_page");//set the limit of results that page
while($query_row = mysql_fetch_assoc($query)){
echo " ur table names ";
}
$prev = $page - 1;//to set the prev page variable
$next = $page + 1;//to set the next page variable
if(!($page<=1)){
echo "<a href='Yourpagename.php?page=$prev'>Prev</a> ";
}//does not displays the prev variable if you are already one first page
if($pages>=1 && $page<=$pages){
for($x=1;$x<=$pages;$x++){
echo ($x == $page) ? '<strong>'.$x.'</strong> ' : ''.$x.' ';
}//display all the pages. Display the current page as bold
}
if(!($page>=$pages)){
echo "<a href='Your page name.php?page=$next'>Next</a>";
}//do not display next vvariable if your are in the last page
?>
Thx for all the answers. I came over this tutorial and it worked: http://www.developphp.com/view.php?tid=1349
<?php
include_once("mysqli_connection.php");
$sql = "SELECT COUNT(id) FROM testimonials WHERE approved='1'";
$query = mysqli_query($db_conx, $sql);
$row = mysqli_fetch_row($query);
$rows = $row[0];
$page_rows = 10;
$last = ceil($rows/$page_rows);
if($last < 1){
$last = 1;
}
$pagenum = 1;
if(isset($_GET['pn'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
}
if ($pagenum < 1) {
$pagenum = 1;
} else if ($pagenum > $last) {
$pagenum = $last;
}
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$sql = "SELECT id, firstname, lastname, datemade FROM testimonials WHERE approved='1' ORDER BY id DESC $limit";
$query = mysqli_query($db_conx, $sql);
$textline1 = "Testimonials (<b>$rows</b>)";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
$paginationCtrls = '';
if($last != 1){
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= 'Previous ';
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= ''.$i.' ';
}
}
}
$paginationCtrls .= ''.$pagenum.' ';
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= ''.$i.' ';
if($i >= $pagenum+4){
break;
}
}
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= ' Next ';
}
}
$list = '';
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$firstname = $row["firstname"];
$lastname = $row["lastname"];
$datemade = $row["datemade"];
$datemade = strftime("%b %d, %Y", strtotime($datemade));
$list .= '<p>'.$firstname.' '.$lastname.' Testimonial - Click the link to view this testimonial<br>Written '.$datemade.'</p>';
}
mysqli_close($db_conx);
?>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body{ font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;}
div#pagination_controls{font-size:21px;}
div#pagination_controls > a{ color:#06F; }
div#pagination_controls > a:visited{ color:#06F; }
</style>
</head>
<body>
<div>
<h2><?php echo $textline1; ?> Paged</h2>
<p><?php echo $textline2; ?></p>
<p><?php echo $list; ?></p>
<div id="pagination_controls"><?php echo $paginationCtrls; ?></div>
</div>
</body>
</html>

showing users within the tables

I was following a tutorial on basic php tables to show the users
http://www.killersites.com/community/index.php?/topic/1969-basic-php-system-vieweditdeleteadd-records/
the problem that i have is my platform does not support MySQL so i change it to MySQL
the second problem is that the users i displaying a above the table
this is an image to better description :
http://store2.up-00.com/2014-02/1391915459381.jpg
and this is the code that i am using :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
</head>
<body>
<?php
/*
VIEW-PAGINATED.PHP
Displays all data from 'players' table
This is a modified version of view.php that includes pagination
*/
// connect to the database
include('config.php');
// number of results to show per page
$per_page = 1;
// figure out the total pages in the database
$result = mysqli_query($connecDB,"SELECT * FROM users");
$total_results = mysqli_num_rows($result);
$total_pages = ceil($total_results / $per_page);
// check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
echo "<p> | <b>View Page:</b> ";
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a href='admin_user_list.php?page=$i'>$i</a> ";
}
echo "</p>";
// display data in table
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
// echo out the contents of each row into a table
echo "<tr>";
while($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
$fname = $row['first_name'];
echo $id;
echo $fname;
}
// echo '<td>Edit</td>';
// echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
// pagination
?>
<p>Add a new record</p>
</body>
</html>
Thank you!!
Addendum to Edit #3
Should you want to add extra cells later on, just follow the same convention.
// echo out the contents of each row into a table
echo "<tr>";
while($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
$fname = $row['first_name'];
$lname = $row['last_name'];
echo "<tr><td>"; // do not modify
echo $id;
echo "</td>"; // do not modify
echo "<td>";
echo $fname;
echo "<td>";
echo $lname;
// there should not be anything below this
// besides the echo "</td></tr>";
echo "</td></tr>"; // do not modify
}
Edit 3
I added $lname = $row['lname']; for your Last Name column and echo $lname; and a few other things. So you may need to modify that $lname = $row['lname']; to suit yours.
Try this now:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
</head>
<body>
<?php
/*
VIEW-PAGINATED.PHP
Displays all data from 'players' table
This is a modified version of view.php that includes pagination
*/
// connect to the database
include('config.php');
// number of results to show per page
$per_page = 1;
// figure out the total pages in the database
$result = mysqli_query($db,"SELECT * FROM animals");
$total_results = mysqli_num_rows($result);
$total_pages = ceil($total_results / $per_page);
// check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
echo "<p> | <b>View Page:</b> ";
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a href='admin_user_list.php?page=$i'>$i</a> ";
}
echo "</p>";
// display data in table
echo "<table border='1' cellpadding='10'>";
// echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
// echo out the contents of each row into a table
echo "<tr>";
while($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
$fname = $row['name'];
$lname = $row['lname'];
echo "<tr><td>";
echo $id;
echo "</td>";
echo "<td>";
echo $fname;
echo "<td>";
echo $lname;
echo "</td></tr>";
}
// echo '<td>Edit</td>';
// echo '<td>Delete</td>';
echo "</td></tr>";
}
// close table>
echo "</table>";
// pagination
?>
<p>Add a new record</p>
</body>
</html>
EDIT 2
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
</head>
<body>
<?php
/*
VIEW-PAGINATED.PHP
Displays all data from 'players' table
This is a modified version of view.php that includes pagination
*/
// connect to the database
include('config.php');
// number of results to show per page
$per_page = 1;
// figure out the total pages in the database
$result = mysqli_query($connecDB,"SELECT * FROM users");
$total_results = mysqli_num_rows($result);
$total_pages = ceil($total_results / $per_page);
// check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
echo "<p> | <b>View Page:</b> ";
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a href='admin_user_list.php?page=$i'>$i</a> ";
}
echo "</p>";
// display data in table
echo "<table border='1' cellpadding='10'>";
// echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
// echo out the contents of each row into a table
echo "<tr>";
while($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
$fname = $row['first_name'];
echo "<td>";
echo $id;
echo "</td>";
echo "<td>";
echo $fname;
echo "</td>";
}
// echo '<td>Edit</td>';
// echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
// pagination
?>
<p>Add a new record</p>
</body>
</html>
EDIT 1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
</head>
<body>
<?php
/*
VIEW-PAGINATED.PHP
Displays all data from 'players' table
This is a modified version of view.php that includes pagination
*/
// connect to the database
include('config.php');
// number of results to show per page
$per_page = 1;
// figure out the total pages in the database
$result = mysqli_query($connecDB,"SELECT * FROM users");
$total_results = mysqli_num_rows($result);
$total_pages = ceil($total_results / $per_page);
// check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
echo "<p> | <b>View Page:</b> ";
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a href='admin_user_list.php?page=$i'>$i</a> ";
}
echo "</p>";
// display data in table
echo "<table border='1' cellpadding='10'>";
// echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
// echo out the contents of each row into a table
echo "<tr>";
while($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
$fname = $row['first_name'];
echo "<td>";
echo $id;
echo $fname;
echo "</td>";
}
// echo '<td>Edit</td>';
// echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
// pagination
?>
<p>Add a new record</p>
</body>
</html>
Original answer
Try the full code below.
I added a <td> in echo "<tr>"; and </td> in echo "</tr>";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
</head>
<body>
<?php
/*
VIEW-PAGINATED.PHP
Displays all data from 'players' table
This is a modified version of view.php that includes pagination
*/
// connect to the database
include('config.php');
// number of results to show per page
$per_page = 1;
// figure out the total pages in the database
$result = mysqli_query($connecDB,"SELECT * FROM users");
$total_results = mysqli_num_rows($result);
$total_pages = ceil($total_results / $per_page);
// check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
echo "<p> | <b>View Page:</b> ";
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a href='admin_user_list.php?page=$i'>$i</a> ";
}
echo "</p>";
// display data in table
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
// echo out the contents of each row into a table
echo "<tr><td>";
while($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
$fname = $row['first_name'];
echo $id;
echo $fname;
}
// echo '<td>Edit</td>';
// echo '<td>Delete</td>';
echo "</td></tr>";
}
// close table>
echo "</table>";
// pagination
?>
<p>Add a new record</p>
</body>
</html>

Lossing session data

I want to perform some data filtering from database. After filtering, session variable is lost and I'm getting this error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in ..\test.php on line 34.
Can someone help?
FORM:
<form action= "test.php" method='get'>
<select name="Type">
<option value="winter" name="winter">winter</option>
</select>
<input type='submit' value = 'filter'>
</form>
CODE:
<?php
session_start();
$_SESSION['winter'] = $_GET['Type'];
$type = $_SESSION['winter'] ;
include "mysqlConnect.php";
//check for a page number. If not, set it to page 1
if (!(isset($_GET['pagenum']))){
$pagenum = 1;
}else{
$pagenum = $_GET['pagenum'];
}
//query for record count to setup pagination
$data = mysql_query("SELECT * FROM tblPhotos WHERE Type='$type' ");
$rows = mysql_num_rows($data);
//number of photos per page
$page_rows = 16;
//get the last page number
$last = ceil($rows/$page_rows);
//make sure the page number isn't below one, or more than last page num
if ($pagenum < 1){
$pagenum = 1;
}elseif ($pagenum > $last){
$pagenum = $last;
}
//Set the range to display in query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
//get all of the photos
$dynamicList = "";
$sql = mysql_query("SELECT * FROM tblPhotos WHERE Type='$type' $max ");
//check for photos
$photoCount = mysql_num_rows($sql); //LINE 34
echo $photoCount;
if ($photoCount > 0){
while($row = mysql_fetch_array($sql)){
$photoID = $row["PhotoID"];
$photoName = $row["photoName"];
$category = $row["category"];
$dynamicList .= '
<div class="thumb">
<img class="clip" src="galleryPhotos/' . $photoID . '.jpg" alt="' . $photoName . '" width="175" border="0" />
</div>
';
}
}else{
$dynamicList = "There are no photos at this time!";
}
mysql_close();
echo '<p style="text-align:center; font-weight:bold;">Page ' . $pagenum . ' of ' . $last . '</p>';
if ($pagenum == 1){
echo '<div class="pagination" align="center"><ul>';
}else{
echo '<div class="pagination" align="center"><ul><li>« first</li>';
$previous = $pagenum-1;
}
//check if number of pages is higher than 1
if($last != 1){
//Loop from 1 to last page to create page number links
for($i = 1; $i <= $last; $i++){
echo '<li>' . $i . '</li>';
}
}
if ($pagenum == $last){
echo '</div>';
}else{
$next = $pagenum+1;
echo '<li>last »</li></ul></div>';
}
echo $dynamicList;
?>
Your Code: mysql_query("SELECT * FROM tblPhotos WHERE Type='$type' $max ");
Read the error message, it is telling you something is wrong with the query. Having looked at the query. I am inclined to agree.

Categories