Backend Development

Pagination

Submitted by linkzy, , Thread ID: 22382

Thread Closed
14-07-2016, 08:13 PM
This post was last modified: 14-07-2016, 08:14 PM by linkzy
#1
How do i paginate this thing.
PHP Code:
<html>
<
body style="background: #d2d2d2;">
<
link rel="stylesheet" type="text/css" href="style.css">


<
center><h1>User List</h1></center>
<
table>
<
tr>
<
th>Name</th>
<
th>Cash</th>
<
th>Score</th>

<?
php
include('config.php');

$query mysql_query('select Name, Cash, Score from users');

while(
$row mysql_fetch_array($query))
{
?>
<tr>
        <td class="left"><?php echo $row['Name']; ?></td>
        <td class="left"><?php echo $row['Cash']; ?></td>
        <td class="left"><?php echo $row['Score']; ?></td>
</tr>
<?php
}
?>

</table>
</body>
</html> 

If there is any mistake in the code, Please tell me how to correct it.
Thank You! Smile

Dont kill me plz plebzz
| A | v4hl| Addicted | Senpai | Sui | Sensei | H | fdigl |


RE: Pagination

#2
Just curious but whats this for?
[Image: pcX8d00.gif]
Thanks for you're contribution! : PenguinGuy
[Image: pcX8d00.gif]

RE: Pagination

OP
This post was last modified: 15-07-2016, 02:22 PM by linkzy
#3
15-07-2016, 04:18 AM
GamesPuff Wrote:
Just curious but whats this for?

Gaming stuff :3....

Help me Please. Someone.
| A | v4hl| Addicted | Senpai | Sui | Sensei | H | fdigl |


RE: Pagination

#4
You need to count how many users you have, then make a limit +offset on the mysql select query
[Image: ATWe5um.gif]

RE: Pagination

OP
#5
15-07-2016, 03:09 PM
Fragan Wrote:
You need to count how many users you have, then make a limit +offset on the mysql select query

An example will be lovely and appreciated Smile
| A | v4hl| Addicted | Senpai | Sui | Sensei | H | fdigl |


RE: Pagination

This post was last modified: 16-07-2016, 12:46 AM by Kill
#6
14-07-2016, 08:13 PM
linkzy Wrote:
How do i paginate this thing.
-

Why so many name changes Believer ;3

Code:
<html>
<body style="background: #d2d2d2;">
<head>
   <link rel="stylesheet" type="text/css" href="style.css">
   <!-- Latest compiled and minified CSS -->
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
   <!-- Latest compiled and minified JavaScript -->
   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</head>

<center><h1>User List</h1></center>
<table>
<tr>
<th>Name</th>
<th>Cash</th>
<th>Score</th>

<?php
$conn = mysql_connect('localhost','root','');
$db = mysql_select_db('users', $conn) or die(mysql_error());



function pagination(){
global $conn;
$sql = "SELECT COUNT(*) FROM users";
$count = mysql_query($sql, $conn);
$result = mysql_fetch_row($count);
$totalrows = $result[0];

// number of rows to show per page
$itemsperpage = 1;
// find out total pages
$totalpages = ceil($totalrows / $itemsperpage);

// get the current page or set a default
if (isset($_GET['p']) && is_numeric($_GET['p']) && is_numeric($_GET['p'])) {
  // cast var as int
  $p = $_GET['p'];
} else {
  // default page num
  $p = 1;
} // end if

// if current page is greater than total pages...
if ($p > $totalpages) {
  // set current page to last page
  $p = $totalpages;
} // end if
// if current page is less than first page...
if ($p < 1) {
  // set current page to first page
  $p = 1;
} // end if

// the offset of the list, based on current page
$offset = ($p - 1) * $itemsperpage;
$range = 3;
// if not on page 1, don't show back links
if ($p > 1) {
  // show << link to go back to page 1
  echo "<li><a href='{$_SERVER['PHP_SELF']}?p=1'><<</a></li>";
  // get previous page num
  $prevpage = $p - 1;
  // show < link to go back to 1 page
  echo "<li><a href='{$_SERVER['PHP_SELF']}?p=$prevpage'><</a></li>";
} // end if

// loop to show links to range of pages around current page
for ($x = ($p - $range); $x < (($p + $range) + 1); $x++) {
  // if it's a valid page number...
  if (($x > 0) && ($x <= $totalpages)) {
   // if we're on current page...
   if ($x == $p) {
     // 'highlight' it but don't make a link
     echo "<li class='active'><a href='#'>$x</a></li>";
     // if not current page...
   } else {
     // make it a link
     echo "<li><a href='{$_SERVER['PHP_SELF']}?p=$x'>$x</a></li>";
   } // end else
  } // end if
} // end for

// if not on last page, show forward and last page links
if ($p != $totalpages) {
  // get next page
  $nextpage = $p + 1;
  // echo forward link for next page
  echo "<li><a href='{$_SERVER['PHP_SELF']}?p=$nextpage'>></a></li>";
  // echo forward link for lastpage
  echo "<li><a href='{$_SERVER['PHP_SELF']}?p=$totalpages'>>></a></li>";
} // end if
/****** end build pagination links ******/


$query = mysql_query("select username, avatar from mybb_users LIMIT $offset, $itemsperpage", $conn);
while($row = mysql_fetch_array($query))
{
?>
<tr>
   <td class="left"><?php echo $row['Name']; ?></td>
       <td class="left"><?php echo $row['Cash']; ?></td>
           <td class="left"><?php echo $row['Score']; ?></td>
</tr>
<?php
}
}
?>
<ul class="pagination">
<?= pagination() ?>
</ul>
</table>
</body>
</html>


Source: http://www.phpfreaks.com/tutorial/basic-pagination

Use MySQLI please.

RE: Pagination

#7
Aaah i see well goodluck
[Image: pcX8d00.gif]
Thanks for you're contribution! : PenguinGuy
[Image: pcX8d00.gif]

RE: Pagination

#8
There might be errors since I did not test it, but this should give you an idea.

Code:
<?php

// 30 Users per page
define( 'PER_PAGE', 30 );

$page  = intval( $_GET['page'] );

/**
* When $page = 1: $start = ( 1 - 1 ) * 30 = 0
* When $page = 2: $start = ( 2 - 1 ) * 30 = 30
* When $page = 3: $start = ( 3 - 1 ) * 30 = 60
* ...
*/
$start = ( $page - 1 ) * PER_PAGE;

$numUsers = mysql_fetch_field( mysql_query( 'SELECT COUNT(*) AS count FROM users' ), 'count' );
$totalPages = ( PER_PAGE > $numUsers ) ? 1 : ceil( (float)$numUsers / PER_PAGE );

$pagination = '';
for( $i = 0; $i < $totalPages; $i++ )
{
$pagination .= '<a href="index.php?page='.($i+1).'">Page '.($i+1).'</a>';
}

?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body style="background: #d2d2d2;">
<center><h1>User List</h1></center>
<table>
<tr>
<th>Name</th>
<th>Cash</th>
<th>Score</th>

<?php
require_once './config.php';

$query = mysql_query( sprintf( 'SELECT `name`, `cash`, `score` FROM `users` LIMIT %d, %d', $start, PER_PAGE ) );

while( $row = mysql_fetch_array($query) )
{
?>
<tr>
    <td class="left"><?php echo $row['Name']; ?></td>
    <td class="left"><?php echo $row['Cash']; ?></td>
    <td class="left"><?php echo $row['Score']; ?></td>
</tr>
<?php
}
?>

</table>
</body>
</html>
Do not let your difficulties fill you with anxiety, after all it is only in the darkest nights that stars shine more brightly. - Ali(a.s)

Developer( PHP, Python, C++, HTML+CSS, JS I am available for Hire. Message Me for details.

RE: Pagination

#9
16-07-2016, 02:01 AM
Sozin Wrote:
There might be errors since I did not test it, but this should give you an idea.

Code:
<?php

// 30 Users per page
define( 'PER_PAGE', 30 );

$page = intval( $_GET['page'] );

/**
* When $page = 1: $start = ( 1 - 1 ) * 30 = 0
* When $page = 2: $start = ( 2 - 1 ) * 30 = 30
* When $page = 3: $start = ( 3 - 1 ) * 30 = 60
* ...
*/
$start = ( $page - 1 ) * PER_PAGE;

$numUsers = mysql_fetch_field( mysql_query( 'SELECT COUNT(*) AS count FROM users' ), 'count' );
$totalPages = ( PER_PAGE > $numUsers ) ? 1 : ceil( (float)$numUsers / PER_PAGE );

$pagination = '';
for( $i = 0; $i < $totalPages; $i++ )
{
$pagination .= '<a href="index.php?page='.($i+1).'">Page '.($i+1).'</a>';
}

?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body style="background: #d2d2d2;">
<center><h1>User List</h1></center>
<table>
<tr>
<th>Name</th>
<th>Cash</th>
<th>Score</th>

<?php
require_once './config.php';

$query = mysql_query( sprintf( 'SELECT `name`, `cash`, `score` FROM `users` LIMIT %d, %d', $start, PER_PAGE ) );

while( $row = mysql_fetch_array($query) )
{
?>
<tr>
  <td class="left"><?php echo $row['Name']; ?></td>
  <td class="left"><?php echo $row['Cash']; ?></td>
  <td class="left"><?php echo $row['Score']; ?></td>
</tr>
<?php
}
?>

</table>
</body>
</html>

why you using sql instead of sqli
[Image: InZ3hGx.png]

RE: Pagination

Warned
#10
16-07-2016, 02:05 AM
Faded Wrote:
16-07-2016, 02:01 AM
Sozin Wrote:
There might be errors since I did not test it, but this should give you an idea.

Code:
<?php

// 30 Users per page
define( 'PER_PAGE', 30 );

$page = intval( $_GET['page'] );

/**
* When $page = 1: $start = ( 1 - 1 ) * 30 = 0
* When $page = 2: $start = ( 2 - 1 ) * 30 = 30
* When $page = 3: $start = ( 3 - 1 ) * 30 = 60
* ...
*/
$start = ( $page - 1 ) * PER_PAGE;

$numUsers = mysql_fetch_field( mysql_query( 'SELECT COUNT(*) AS count FROM users' ), 'count' );
$totalPages = ( PER_PAGE > $numUsers ) ? 1 : ceil( (float)$numUsers / PER_PAGE );

$pagination = '';
for( $i = 0; $i < $totalPages; $i++ )
{
$pagination .= '<a href="index.php?page='.($i+1).'">Page '.($i+1).'</a>';
}

?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body style="background: #d2d2d2;">
<center><h1>User List</h1></center>
<table>
<tr>
<th>Name</th>
<th>Cash</th>
<th>Score</th>

<?php
require_once './config.php';

$query = mysql_query( sprintf( 'SELECT `name`, `cash`, `score` FROM `users` LIMIT %d, %d', $start, PER_PAGE ) );

while( $row = mysql_fetch_array($query) )
{
?>
<tr>
  <td class="left"><?php echo $row['Name']; ?></td>
  <td class="left"><?php echo $row['Cash']; ?></td>
  <td class="left"><?php echo $row['Score']; ?></td>
</tr>
<?php
}
?>

</table>
</body>
</html>

why you using sql instead of sqli

+1



plz dont fucking warn me.
xxx

Users browsing this thread: 5 Guest(s)