Log in

View Full Version : Code for Paging like Page 1 of 5



rhodarose
05-03-2011, 09:18 AM
I need help in coding for the First, Previous, Paging like Page 1 0f 5, Next, Last. Theirs somebody has an idea on that?

Any help is highly appreciated...

Thank you

fastsol1
05-03-2011, 11:33 PM
http://www.youtube.com/phpacademy#p/c/FA954987101252EF/68/wC0uc_TkdR0

rhodarose
05-04-2011, 03:38 AM
This is my new code with pagination:



<?php

// Connects to your Database

mysql_connect("localhost", "root", "") or die(mysql_error());

mysql_select_db("db_machine1") or die(mysql_error());


//This checks to see if there is a page number. If not, it will set it to page 1

if (!(isset($pagenum)))

{

$pagenum = 1;

}



//Here we count the number of results

//Edit $data to be your query

$data = mysql_query("SELECT * FROM tbl_machine1") or die(mysql_error());

$rows = mysql_num_rows($data);



//This is the number of results displayed per page

$page_rows = 4;

//$page_rows = 2;

//This tells us the page number of our last page

$last = ceil($rows/$page_rows);



//this makes sure the page number isn't below one, or more than our maximum pages

if ($pagenum < 1)

{

$pagenum = 1;

}

elseif ($pagenum > $last)

{

$pagenum = $last;

}



//This sets the range to display in our query

$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

//This is your query again, the same one... the only difference is we add $max into it

$data_p = mysql_query("SELECT * FROM tbl_machine1 $max") or die(mysql_error());


//This is where you display your query results
echo "<table border='1'><tr>";
for($i = 0; $i < mysql_num_fields($data_p); $i++){
echo "<th>".mysql_field_name($data_p, $i)."</th>";
//echo "<th><a>.mysql_field_name($result, $i).""</a></th>";
}
echo "<th>Options</th>";
echo "</tr>";
//while($row = mysql_fetch_array($result))
while($info = mysql_fetch_array( $data_p ))
{
echo "<tr>";
for($i = 0; $i < mysql_num_fields($data_p); $i++){
echo "<td>". $info[$i] ."</td>";

}
echo "<td><a href = 'edit.php'>Edit</a> <a href = 'delete.php' onClick='return confirmDelete();'>Delete</a></td>";
//echo "<td><a href = 'edit.php'>Edit</a> <a href='delete.php' onclick='return confirm('Are you sure you want to delete?')'>Delete</a</td>";
echo "</tr>";
}

echo "</table>";
echo "<input type = 'button' name= 'add' value='ADD'>";
// while($info = mysql_fetch_array( $data_p ))

// {

// Print $info['Last_Name'];
// Print $info['First_Name'];
//Print $info['Birthday'];

// echo "<br>";

//}

echo "<p>";


// This shows the user what page they are on, and the total number of pages

echo " --Page $pagenum of $last-- <p>";


// 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)

{

}

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> ";

}


//just a spacer

echo " ---- ";


//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links

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> ";

}

?>

But the Next and Last hyperlink did not work.

Thank you

rhodarose
05-09-2011, 04:07 AM
I resolved my problem by this code:


<?php
session_start();
if(empty($_SESSION['logged_in'])) {
header('Location:index.php');
die();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>

</head>
<body>
<form name="machine1" action="machine1.php" method="post">

<p>
<?php

$sort = "ASC";
$data_sort = "Emp_ID";

if(isset($_GET['sorting']))
{
if($_GET['sorting'] == 'ASC'){
$sort = "DESC";
}
else{
$sort = "ASC";
}
}


if (isset($_GET['field_name'])) {
if($_GET['field_name'] == 'Emp_ID'){
$data_sort = "Emp_ID";
}
elseif($_GET['field_name'] == 'Last_Name'){
$data_sort = "Last_Name";
}
elseif($_GET['field_name'] == 'First_Name'){
$data_sort = "First_Name";
}
elseif($_GET['field_name'] == 'Birthday'){
$data_sort = "Birthday";
}
}
?>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="logout.php">Sign Out</a> </p>
<table border="1">
<tr>
<td><a href="machine1.php?sorting=<?php echo $sort; ?>&field_name=Emp_ID">Emp ID</a></td>
<td><a href="machine1.php?sorting=<?php echo $sort; ?>&field_name=Last_Name">Last Name</a></td>
<td><a href="machine1.php?sorting=<?php echo $sort; ?>&field_name=First_Name">First Name</a></td>
<td><a href="machine1.php?sorting=<?php echo $sort; ?>&field_name=Birthday">Birthday</a></td>
<td>Option</td>
</tr>

<?php
include 'connection.php';

if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$query = "SELECT count(*) FROM tbl_machine1";
$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];

$rows_per_page = 5;
$lastpage = ceil($numrows/$rows_per_page);

$pageno = (int)$pageno;
if ($pageno > $lastpage) {
$pageno = $lastpage;
}
if ($pageno < 1) {
$pageno = 1;
}

$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;



$query = "SELECT * FROM tbl_machine1 ORDER BY $data_sort $sort $limit";
$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);


while($info = mysql_fetch_array( $result ))
{
$emp_id = $info['Emp_ID'];
$lname = $info['Last_Name'];
$fname = $info['First_Name'];
$bday = $info['Birthday'];
$date = date('d-m-Y', strtotime($bday));
?>
<tr>
<td><?php echo $emp_id;?> </td>
<td><?php echo htmlentities($lname, ENT_QUOTES); ?> </td>
<td><?php echo htmlentities($fname, ENT_QUOTES);?> </td>
<td><?php echo htmlentities($date, ENT_QUOTES);?> </td>
<td><a href = 'edit.php?id=<?php echo $emp_id; ?>'>Edit</a> <a href='delete.php?id=<?php echo $emp_id; ?>' onClick="return confirm('Are you sure you want to delete?')">Delete</a></td>
</tr>
<?php
}
?>
</table>
<A HREF="javascript:void(0)" onClick="window.open('add.php','welcome','width=300,height=200')">
<input type="button" name="add" value="ADD"> </A>&nbsp;&nbsp;

<?php

if(isset($_GET['sorting']))
{
if($_GET['sorting'] == 'ASC'){
$sort = "ASC";
}
else{
$sort = "DESC";
}
}

if ($pageno == 1) {
echo " FIRST PREV ";
} else {
?>
<a href="machine1.php?pageno=1&field_name=<?php echo $data_sort; ?>&sorting=<?php echo $sort; ?>">FIRST</a>
<?php
$prevpage = $pageno-1;
?>
<a href="machine1.php?pageno=<?php echo $prevpage;?>&field_name=<?php echo $data_sort; ?>&sorting=<?php echo $sort; ?>">PREV</a>

<?php
}
echo " ( Page $pageno of $lastpage ) ";

if ($pageno == $lastpage) {
echo " NEXT LAST ";
} else {
$nextpage = $pageno+1;

?>

<a href="machine1.php?pageno=<?php echo $nextpage; ?>&field_name=<?php echo $data_sort; ?>&sorting=<?php echo $sort; ?>">NEXT</a>
<a href="machine1.php?pageno=<?php echo $lastpage; ?>&field_name=<?php echo $data_sort; ?>&sorting=<?php echo $sort; ?>">LAST</a>
<?php
}

?>
</body>
</html>


Thank you