how to make paging with function ?

how to make paging with function ?

how to make paging with function ?

View Answers

June 6, 2011 at 11:04 AM

//this page is display.php

<?php

$con=mysql_connect("localhost","root","");
if(!$con)
{
die("you can connect please check". mysql_error());
}
mysql_select_db(mydata,$con);
include "pagingcheck.php";
$rowsPerPage = 2;
$pageNum = 1;
$offset = ($pageNum - 1) * $rowsPerPage;

$query = mysql_query("SELECT * FROM login LIMIT $offset, $rowsPerPage");

echo "<table border='1'>
<tr>
<th>id</th>
<th>first name</th>
<th>last name</th>
<th>user</th>
<th>password</th>
<th>gender</th>
<th>e-mailid</th>
<th>mobile no</th>
<th>image</th>
<th>edit</th>
<th>delete</th>
</tr>";


while($row = mysql_fetch_array($query))
  {
  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['fname'] . "</td>";
  echo "<td>" . $row['lname'] . "</td>";
  echo "<td>" . $row['user'] . "</td>";
  echo "<td>" . $row['pass'] . "</td>";
  echo "<td>" . $row['gender'] . "</td>";
  echo "<td>" . $row['mail'] . "</td>";
  echo "<td>" . $row['mno'] . "</td>";
  echo "<td>" . "<img src='".$row['file']."' height='25px' width='25'/>" . "</td>";
  //echo "<td>" . "<a href='edit.php'> edit </a>". "</td>";
    ?>
  <td><a href="edit.php?id=<? echo $row['id']; ?>">edit</a></td>  
 <td><a href="javascript:confirmation(<? echo $row['id']; ?>)">delete</a> </td> 

  <?php

  echo "</tr>";
  }
echo "</table>";


$query = mysql_query("SELECT COUNT(id) AS numrows FROM login ");
//this page in only function call and next page in function body
paging($offset,$rowsPerPage,$query);

?>

//this page is pagingcheck.php

<!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>
</body>
</html>
<?php


function paging($offset,$rowsPerPage,$query)
{
    if(isset($_GET['page']))
    {
        $pageNum = $_GET['page'];
    }
    $row = mysql_fetch_array($query);
    $numrows = $row['numrows'];
    $maxPage = ceil($numrows/$rowsPerPage);

        $self = $_SERVER['PHP_SELF'];
        $nav = '';
        for($page = 1; $page <= $maxPage; $page++)
        {
            if ($page == $pageNum)
            {
                $nav .= " $page "; // no need to create a link to current page

            }
            else
            {
                $nav .= " <a href=\"$self?page=$page\">$page</a> ";
            }
        }
        if ($pageNum > 1)
        {
            $page = $pageNum - 1;
            $prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
            $first = " <a href=\"$self?page=1\">[First Page]</a> ";
        }
        else
        {
            $prev = ' '; // we're on page one, don't print previous link
            $first = ' '; // nor the first page link
        }
        if ($pageNum < $maxPage)
        {
            $page = $pageNum + 1;
            $next = " <a href=\"$self?page=$page\">[Next]</a> ";
            $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
        }
        else
        {
            $next = ' '; // we're on the last page, don't print next link
            $last = ' '; // nor the last page link
        }

echo $nav . "<br />";
echo $prev . "  " . $next . "<br />";
echo $first . "  " . $last;

}
?>









Related Tutorials/Questions & Answers:

Ads