PHP Order by in paginated table from database - php

I got it to order by but now the paginaging doesn't work. NOTHING prints into my error_logs now.
User Red Acid helpedme out with the the missing Whitespace and use a string variable instead of PHP_SELF.
Once I press next record I get:
Not Found
The requested URL /surf/$index.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
My code:
<?php
$dbHost = "localhost";
$dbUser = "";
$dbPass = "";
$dbName = "";
$tbl_name="";
$rec_limit = 20;
$order = "ORDER BY";
$scriptname = 'index.php';
mysql_connect("$dbHost", "$dbUser", "$dbPass")or die("cannot connect");
mysql_select_db("$dbName")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name $order ".$_GET["orderby"]." ".$_GET["desc"];
$result=mysql_query($sql);
$sql = "SELECT count(steamid) FROM $tbl_name";
$result= mysql_query($sql);
if(! $result)
{
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($result, MYSQL_NUM );
$rec_count = $row[0];
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 $tbl_name ".
"$order points DESC ".
"LIMIT $offset, $rec_limit";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['points']; ?></td>
<td><?php echo $rows['winratio']; ?></td>
<td><?php echo $rows['pointsratio']; ?></td>
<td><?php echo $rows['finishedmaps']; ?></td>
<td><?php echo $rows['lastseen']; ?></td>
</tr>
</tr>
<?php
}
if( $page > 0 )
{
$last = $page - 2;
echo "Last 10 Records |";
echo "Next 10 Records";
}
else if( $page == 0 )
{
echo "Next 10 Records";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "Last 10 Records";
}
mysql_close();
?><?php
$dbHost = "localhost";
$dbUser = "c1226125_ms";
$dbPass = "wR(#ucrb5oG.";
$dbName = "c1226125_timer";
$tbl_name="ck_playerrank";
$rec_limit = 20;
$order = "ORDER BY";
$scriptname = 'index.php';
mysql_connect("$dbHost", "$dbUser", "$dbPass")or die("cannot connect");
mysql_select_db("$dbName")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name $order ".$_GET["orderby"]." ".$_GET["desc"];
$result=mysql_query($sql);
$sql = "SELECT count(steamid) FROM $tbl_name";
$result= mysql_query($sql);
if(! $result)
{
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($result, MYSQL_NUM );
$rec_count = $row[0];
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 $tbl_name ".
"$order points DESC ".
"LIMIT $offset, $rec_limit";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['points']; ?></td>
<td><?php echo $rows['winratio']; ?></td>
<td><?php echo $rows['pointsratio']; ?></td>
<td><?php echo $rows['finishedmaps']; ?></td>
<td><?php echo $rows['lastseen']; ?></td>
</tr>
</tr>
<?php
}
if( $page > 0 )
{
$last = $page - 2;
echo "Last 10 Records |";
echo "Next 10 Records";
}
else if( $page == 0 )
{
echo "Next 10 Records";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "Last 10 Records";
}
mysql_close();
?>

problem that you dont have white space between ORDER BY and DESC:
$sql="SELECT * FROM $tbl_name ORDER BY ".$_GET["orderby"].$_GET["desc"];
should be:
$sql="SELECT * FROM $tbl_name ORDER BY ".$_GET["orderby"]." ".$_GET["desc"];
use string variable instead of PHP_SELF:
$scriptname = 'myscript.php'; // OR http://domain.com/myscript.php
echo "Last 10 Records";

Related

How can I create two pagination in a single page with two different queries using PHP?

I just started to learn PHP this week, and I'd search for possible solution on this but I can make it right. While I sucessfully created a pagination that sorts queries into alphabetical order, I wasn't successful in creating a numerical pagination, in which it will limit the number of queries to be displayed. Also, I wasn't able to combined these two types of pagination with two different queries, one for A-Z sorting, and one for the number of queries to be displayed in a single page.
Here's my code
`<?php
include 'includes/connection.php';
$character = '';
$characters = '';
if (isset($_GET['character']))
{
$character = $_GET['character'];
$character = preg_replace('#[^a-z]#i', '', $character);
$sql = "SELECT * FROM drivers_info WHERE last_name LIKE '$character%' ORDER BY last_name";
}
else{
$sql = "SELECT * FROM drivers_info ORDER BY rfid ";
$pageno = 1
}
$result = mysqli_query($db, $sql);
<body>
<br /><br />
<br /> <br />
Here's the code to create an alphabetical pagination
<div class="table-responsive">
<div align="center">
<?php
$character = range('A', 'Z');
echo '<ul class="pagination">';
foreach($character as $alphabet)
{
echo '<li>'.$alphabet.'</li>';
}
echo '</ul>';
?>
</div>
<?php
Here's the code for displaying the queries alphabetically
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result)){
?>
<tr>
<td><?php echo $row["rfid"]; ?></td>
<td><?php echo $row["last_name"]; ?></td>
<td><?php echo $row["first_name"]; ?></td>
<td><?php echo $row["middle_name"]; ?></td>
<td><?php echo $row["gender"]; ?></td>
<td><?php echo $row["age"]; ?></td>
<td><?php echo $row["height"]; ?></td>
<td><?php echo $row["weight"]; ?></td>
<td><?php echo $row["address"]; ?></td>
<td><?php echo $row["contact_no"]; ?></td>
<td><?php echo $row["license_type"]; ?></td>
<td><?php echo $row["license_no"]; ?></td>
<td><?php echo $row["expiration"]; ?></td>
<td><?php echo $row["nationality"]; ?></td>
<td><?php echo $row["eyes_color"]; ?></td>
<td><?php echo $row["blood_type"]; ?></td>
</tr>
<?php
}
}
</body>
If you had any code or idea on it, would you mind sharing it with me? Thank you so much, I needed this to accomplish my school project.
`
Here is best reference code for understanding pagination logic
<?php
$host = "localhost";
$user = "root";
$pass = "New";
$db = "booksgood";
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// how many rows to show per page
$rowsPerPage = 10;
// by default we show first page
$page_num = 1;
// if $_GET['page'] defined, use it as page number, $_GET gets the page number out of the url
//set by the $page_pagination below
if(isset($_GET['page'])){$page_num = $_GET['page'];}
//the point to start for the limit query
$offset = $page_num;
// Zero is an incorrect page, so switch the zero with 1, mainly because it will cause an error with the SQL
if($page_num == 0) {$page_num = 1;}
// counting the offset
$sql = "SELECT * FROM bookdata where titles like 's%' order by titles LIMIT $offset, $rowsPerPage ";
$res = mysql_query($sql) or die(mysql_error());
// how many rows we have in database
$sql2 = "SELECT COUNT(id) AS numrows FROM bookdata where titles like 's%'";
$res2 = mysql_query($sql2) or die(mysql_error());
$row2 = mysql_fetch_array($res2);
$numrows = $row2['numrows'];
// print the random numbers
while($row = mysql_fetch_array($res))
{
//Echo out your table contents here.
echo $row[1].'<BR>';
echo $row[2].'<BR>';
echo '<BR>';
}
// how many pages we have when using paging?
$numofpages = ceil($numrows/$rowsPerPage);
// print the link to access each page
$self = "/paging3.php?";
if ($numofpages > '1' ) {
$range =15; //set this to what ever range you want to show in the pagination link
$range_min = ($range % 2 == 0) ? ($range / 2) - 1 : ($range - 1) / 2;
$range_max = ($range % 2 == 0) ? $range_min + 1 : $range_min;
$page_min = $page_num- $range_min;
$page_max = $page_num+ $range_max;
$page_min = ($page_min < 1) ? 1 : $page_min;
$page_max = ($page_max < ($page_min + $range - 1)) ? $page_min + $range - 1 : $page_max;
if ($page_max > $numofpages) {
$page_min = ($page_min > 1) ? $numofpages - $range + 1 : 1;
$page_max = $numofpages;
}
$page_min = ($page_min < 1) ? 1 : $page_min;
//$page_content .= '<p class="menuPage">';
if ( ($page_num > ($range - $range_min)) && ($numofpages > $range) ) {
$page_pagination .= '<a class="num" title="First" href="'.$self.'page=1"><</a> ';
}
if ($page_num != 1) {
$page_pagination .= '<a class="num" href="'.$self.'page='.($page_num-1). '">Previous</a> ';
}
for ($i = $page_min;$i <= $page_max;$i++) {
if ($i == $page_num)
$page_pagination .= '<span class="num"><strong>' . $i . '</strong></span> ';
else
$page_pagination.= '<a class="num" href="'.$self.'page='.$i. '">'.$i.'</a> ';
}
if ($page_num < $numofpages) {
$page_pagination.= ' <a class="num" href="'.$self.'page='.($page_num + 1) . '">Next</a>';
}
if (($page_num< ($numofpages - $range_max)) && ($numofpages > $range)) {
$page_pagination .= ' <a class="num" title="Last" href="'.$self.'page='.$numofpages. '">></a> ';
}
//$page['PAGINATION'] ='<p id="pagination">'.$page_pagination.'</p>';
}//end if more than 1 page
echo $page_pagination.'<BR><BR>';
echo 'Number of results - '.$numrows ;
echo ' and Number of pages - '.$numofpages.'<BR><BR>';
// Free resultset
mysql_free_result($res);
// and close the database connection
mysql_close($con);
?>
here is reference code link

How to display only 5 records per page from my mysql database table through pagination?

I want to display five record per page through pagination (mysql,php,html,css) until all the records are displayed, navigation to pages must be like, Page: 1 2 3 4 5 6 7 7 8... Last.
HERE IS MY CODE TO VIEW ALL THE RECORDS FROM emp_master table.
I am new to PHP so please write an easily understandable code for pagination. I have seen few examples but they are not working.
<?php
$con=mysqli_connect("localhost","user","password","dataplus");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM emp_master");
echo "<table border='1'>";
$i = 0;
while($row = $result->fetch_assoc())
{
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
I want to display five record per page through pagination untill all the records are displayed, navigation to pages must be like, Page: 1 2 3 4 5 6 7 7 8... Last.
This code below is not working:
$dbhost="localhost";
$dbuser="10053";
$dbpass="n6867242";
$database="0368";
$rec_limit = 10;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('1005368');
/* Get total number of records */
$sql = "SELECT count(emp_id) FROM emp_master ";
$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];
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 emp_id, emp_name, e_mail ".
"FROM emp_master ".
"LIMIT $offset, $rec_limit";
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "EMP ID :{$row['emp_id']} <br> ".
"EMP NAME : {$row['emp_name']} <br> ".
"EMP MAIL : {$row['e_mail']} <br> ".
"--------------------------------<br>";
}
if( $page > 0 ) {
$last = $page - 2;
echo "Last 10 Records |";
echo "Next 10 Records";
}else if( $page == 0 ) {
echo "Next 10 Records";
}else if( $left_rec < $rec_limit ) {
$last = $page - 2;
echo "Last 10 Records";
}
mysql_close($conn);
If you want just PHP MySQL code, I usually use something like the following.
$page=max(intval($_GET['page']),1); // assuming there is a parameter 'page'
$itemsperpage = 5;
$total=100; // total results if you know it already otherwise use another query
$totalpages = max(ceil($total/$itemsperpage),1);
$query = "SELECT * FROM emp_master LIMIT ".(($page-1)*$itemsperpage).",".$itemsperpage; // this will return 5 items based on the page
You can use a library called dataTables that will auto paginate the data and you can easily customize it to suit your needs with the look and how many records to show per page. It also has a search field which enables users to search your data without any extra code.
All you have to do is include a couple of cdn's and the following code and everything works fine
$(document).ready(function(){
$('#myTable').DataTable();
});
Some examples here
you just should remove spaces in href and also make $page = $page + 1 when page is 0
if( $page > 0 ) {
$last = $page - 2;
echo "Last 10 Records |";
echo "Next 10 Records";
}else if( $page == 0 ) {
$page = $page + 1;
echo "Next 10 Records";
}else if( $left_rec < $rec_limit ) {
$last = $page - 2;
echo "Last 10 Records";
}
Link is here for demo, click here to experience the result
Here is the code working for me, just change your db name, username and password
and get pagination done. You can change $rec_limit value to your desired no. of records per page.
<?php
$host="localhost";
$username="68";
$password="67242";
$database="68";
$rec_limit = 5;
$conn = mysql_connect($localhost,$username,$password);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('10');
/* Get total number of records */
$sql = "SELECT count(emp_id) FROM emp_master ";
$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];
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 eid,ename, email, quali, gender, contactno, birthdate, joiningdate,CURDATE(), TIMESTAMPDIFF( YEAR, birthdate, CURDATE( ) ) AS age ".
"FROM emp_master ".
"LIMIT $offset, $rec_limit";
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
echo "<table border='1'>";
$i = 0;
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
if( $page > 0 ) {
$last = $page - 2;
echo "Last 5 Records | ";
echo "Next 5 Records";
}else if( $page == 0 ) {
$page = $page + 0;
echo "Next 5 Records";
}else if( $left_rec < $rec_limit ) {
$last = $page - 2;
echo "Last 5 Records";
}
mysql_close($conn);
php?>
IF your are using mysqli the code is below
$conn=mysqli_connect("localhost","root","","ui");
$start=0;
$limit=5;
$t=mysqli_query($conn,"select * from form_table");
$total=mysqli_num_rows($t);
if(isset($_GET['id']))
{
$id=$_GET['id'] ;
$start=($id-1)*$limit;
}
else
{
$id=1;
}
$page=ceil($total/$limit);
$query=mysqli_query($conn,"select * from form_table limit $start, $limit");
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script s src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"> </script>
</head>
<body>
<div class="container">
<h2>Table</h2>
<table class="table table-bordered">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Gender</th>
<th>Hobbies</th>
<th>Course</th>
</tr>
</thead>
<tbody>
<?php
while($ft=mysqli_fetch_array($query))
{?>
<tr>
<td><?= $ft['0']?></td>
<td><?= $ft['1']?></td>
<td><?= $ft['2']?></td>
<td><?= $ft['3']?></td>
<td><?= $ft['4']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<ul class="pagination">
<?php if($id > 1) {?> <li>Previous</li><?php }?>
<?php
for($i=1;$i <= $page;$i++){
?>
<li><?php echo $i;?></li>
<?php
}
?>
<?php if($id!=$page)
{?>
i Give you an example of my project of pagination. Just you have to put your values in the code
$sql="SELECT * FROM tblname LIMIT $next,5";
$results = mysqli_query($conn,$sql);
}
else if (isset($_POST['prev']))
{
$prev1=$_POST['prev'];
$next=$_POST['prev'];
$prev=$prev1;
$sql="SELECT * FROM tablename LIMIT $prev,5";
$prev=$prev1;
$results = mysqli_query($conn,"SELECT * FROM tablename LIMIT $prev,10");
if($prev==0)
{
$prev = 0;
}
else
{
$prev=$next-10;
}
}
else
{
$next=0;
$prev=0;
$results = mysqli_query($conn,"SELECT * FROM tablename LIMIT $next,10");
}

PHP Mysql Paginating results

Hey I am trying to have paginated results for a book store but I can not figure out why my links to other pages will not work. When I run this the original page loads the results but when I go to the next page it says items not found.
<?php
$host = '???'; //IP Address on domain name of the host for the database
$user ='???'; //the name of the user
$pass="???";//Our Password
//make the connection to the database
$con=mysql_connect($host,$user,$pass) or
die("Error connecting to Database");
$dbname = "???"; //we had a database name ???
mysql_select_db($dbname);
?>
<?php
echo "<br><h3><u>Available Books for Sell</u></h3><br>"; // promps user to order a book
$per_page=5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page=1;
}
// Page will start from 0 and Multiple by Per Page
$start_from = ($page-1) * $per_page;
$query="select * from Books limit $start_from,$per_page"; // create query using the $_GET 'id' sent
$result=mysql_query($query); //results from executing the mysql query
if(!$result) // if not results
{
echo" get Items error"; // print error
exit;
}
while($row=mysql_fetch_array($result)) // while there are results
{
echo
"<br>----------------------------------------------------------------------
<br>Title: ".$row['Title'].
"<br>Author: ".$row['Author']. //print row price, name, author
"<br>ISBN: ".$row['ISBN'].
"<br>Condition: ".$row['BookCondition'].
"<br>Price: ".$row['Price'].
"<br>Sellers Username: ".$row['Uname'];
echo"<br>";
echo"----------------------------------------------------------------------";
}
//Now select all from table
$query = "select * from Books";
$result = mysql_query($query);
// Count the total records
$total_records = mysql_num_rows($result);
//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);
//Going to first page
echo "<center><a href='getItems.php?page=1'>".'First Page'."</a> ";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='getItems.php?page=".$i."'>".$i."</a> ";
};
// Going to last page
echo "<a href='getItems.php?page=$total_pages'>".'Last Page'."</a></center> ";
?>
this may help you
<?php
$dbhost = 'localhost';
$dbuser = '---';
$dbpass = '---';
$rec_limit = 1;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('---');
/* Get total number of records */
$sql = "SELECT count(user_id) FROM table_name";
$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];
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 company_name, e_mail, international_code, mob_number, nationality, current_loc, pref_location,
key_skills, address, user_dob, training, lang1, lang2, lang3, lang4 ".
"FROM se_job_contracts ".
"LIMIT $offset, $rec_limit";
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "<b>Company Name</b> :{$row['company_name']} <br> ".
"<b>Email-ID</b> : {$row['e_mail']} <br> ".
"<b>International code </b> : {$row['international_code']} "." <b>Mobile No </b> : {$row['mob_number']} <br> ".
"<b>Nationality </b> : {$row['nationality']} <br> ".
"<b>Current Location </b> : {$row['current_loc']} <br> ".
"<b>Preferred Location </b> : {$row['pref_location']} <br> ".
"<b>Key Skills </b> : {$row['key_skills']} <br> ".
"<b>Address </b> : {$row['address']} <br> ".
"<b>User DOB </b> : {$row['user_dob']} <br> ".
"<b>Training </b> : {$row['training']} <br> ".
"<b>Language known 1 </b> : {$row['lang1']} <br> ".
"<b>Language known 2 </b> : {$row['lang2']} <br> ".
"<b>Language known 3 </b> : {$row['lang3']} <br> ".
"<b>Language known 4 </b> : {$row['lang4']} <br> ".
"--------------------------------<br>";
}
if( $page > 0 )
{
$last = $page - 2;
echo "Previous |";
echo "Next >>";
}
else if( $page == 0 )
{
echo "Next >>";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "Previous ";
}
mysql_close($conn);
?>

PHP sequential numbering

I'm having trouble with my page here:
LINK
My "rank" part of the table keeps resetting back to zero when you click on page 2 and beyond. It doesn't start at 21, it just resets back to zero.
How do I fix that?
<?php
include ("database.php");
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 20;
$sql = ("SELECT * FROM voting ORDER BY votepoints DESC LIMIT $start_from, 20") or die ("Error 1.");
$rs_result = mysql_query ($sql) or die (include ("error.html"));
$rank = 1;
?>
<table>
<tr><td>Rank</td><td></td><td>Username</td><td></td><td></td><td></td><td>Vote points</td></tr>
<?php
while ($row = mysql_fetch_assoc($rs_result)) {
?>
<tr>
<td><? echo $rank++; ?></td>
<td></td>
<td><? echo $row["username"]; ?></td>
<td></td>
<td></td>
<td></td>
<td><? echo $row["votepoints"]; ?></td>
</tr>
<?php
};
?>
</table>
<?php
echo '<br>';
echo 'Total Votes: ';
include ("sum.php");
include("database.php");
$sql = "SELECT COUNT(votepoints) FROM voting";
$rs_result = mysql_query($sql);
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 20);
echo '<span class="text-bleumarin">';
if($page>1)
$pagelink ='prev ';
echo $pagelink;
for ($i=1; $i<=$total_pages; $i++) {
if ($i != $page)
echo "<a href='votes.php?page=".$i."'>".$i."</a> ";
if ($i==$page)
echo '' . $i . ' ';
};
if($page<$total_pages)
$pagelink ='next ';
echo $pagelink;
echo '</span>';
?>
EDIT: this is the line you need to change
*EDIT2: subtracted 1 from page as you set 1 to default.*
<td><? echo (($page-1)*20)+$rank++; ?></td>
This will do:
(0*20) + 1 = 1
(0*20) + 2 = 2
...
(0*20) + 20 = 20
(1*20) + 1 = 21
(1*20) + 2 = 22
...
(1*20) + 20 = 40
(2*20) + 1 = 41
(2*20) + 2 = 42
...
(2*20) + 20 = 60
...
(21*20) + 1 = 421
Note: No code provided when this solution was posted
Assuming you are using a for loop of some sort to keep count:
if (!isset($_GET['page']))
{
$page=0;
}
else
{
$page=$_GET['page'];
}
for($i=1; $i<21; $i++)
{
echo '<tr>';
echo '<td>'.($page*20)+$i.'</td>;' //THIS IS THE RANK LINE
//Other fields
echo '</tr>';
}

i got error while coding of pagination in php5?

i am writing code of pagination & i got an error.
this is my code :
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("Admin") or die(mysql_error());
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page=1;
}
$start_from = ($page-1) * 2;
$sql = "SELECT * FROM events ORDER BY event ASC LIMIT $start_from, 2";
$result = mysql_query ($sql) or die(mysql_error());
$num=mysql_numrows($result);
$x=0;
?>
<table>
<tr><td>Event</td><td>Types</td></tr>
<?php
while ($x<$num) {
$row1 = mysql_result($result,$x,'event');
$row2 = mysql_result($result,$x,'types');
?>
<tr>
<td><? echo $row1; ?></td>
<td><? echo $row2; ?></td>
</tr>
<?php
$x++;
}
?>
</table>
<?php
$sql = "SELECT COUNT(event) FROM events";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_row($result);
$total_records = $row[0];
$total_pages = ceil($total_records / 2);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='pagination.php?page=".$i."'>".$i."</a> ";
}
?>
now i got an error.
object not found
Note that instead of terrible while ($x<$num) you should use way more neat while($row=mysql_fetch_array($res))
so, make it
<?
$per_page = 2;
$page = 1;
if (isset($_GET['page'])) $page = $_GET['page'];
$start_from = ($page-1) * $per_page;
$sql = "SELECT * FROM events ORDER BY event ASC LIMIT $start_from, $per_page";
$res = mysql_query ($sql) or trigger_error(mysql_error().": ".$sql);
?>
<table>
<tr><td>Event</td><td>Types</td></tr>
<?php
while($row=mysql_fetch_array($res)) {
?>
<tr>
<td><? echo $row['event'] ?></td>
<td><? echo $row['types'] ?></td>
</tr>
<?php
}
?>
</table>
<?php
$sql = "SELECT COUNT(event) FROM events";
$res = mysql_query ($sql) or trigger_error(mysql_error().": ".$sql);
$row = mysql_fetch_row($res);
$total_records = $row[0];
$total_pages = ceil($total_records / $per_page);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='pagination.php?page=".$i."'>".$i."</a> ";
}
?>
mysql_query returns false - i.e. your query failed and you are passing a wrong argument to mysql_num_rows. I think you have a typo in the table name in the SQL query, it should be events instead. Generally, you should check if the query succeeded:
$result = mysql_query ($sql) or trigger_error(mysql_error().": ".$sql);

Categories