/heraldry/op/test2.php

https://github.com/micheleberg/Calontir-OP · PHP · 90 lines · 58 code · 20 blank · 12 comment · 5 complexity · c031f0efce496dbd20c6e42d9fe19a23 MD5 · raw file

  1. <?PHP
  2. $user_name = "kocdbopinfo";
  3. $password = "CalontirOP0311";
  4. $database = "kocdbopinfo";
  5. $server = "kocdbopinfo.db.7394721.hostedresource.com";
  6. $db_handle = mysql_connect($server, $user_name, $password);
  7. $db_found = mysql_select_db($database, $db_handle);
  8. if ($db_found) {
  9. // how many rows to show per page
  10. $rowsPerPage = 20;
  11. // by default we show first page
  12. $pageNum = 1;
  13. // if $_GET['page'] defined, use it as page number
  14. if(isset($_GET['page']))
  15. {
  16. $pageNum = $_GET['page'];
  17. }
  18. // counting the offset
  19. $offset = ($pageNum - 1) * $rowsPerPage;
  20. $query = " SELECT Name FROM Populace" .
  21. " LIMIT $offset, $rowsPerPage";
  22. $result = mysql_query($query) or die('Error, query failed');
  23. // print the random numbers
  24. while($row = mysql_fetch_array($result))
  25. {
  26. echo $row['Name'] . '<br>';
  27. }
  28. // how many rows we have in database
  29. $query = "SELECT COUNT(Name) AS numrows FROM Populace";
  30. $result = mysql_query($query) or die('Error, query failed');
  31. $row = mysql_fetch_array($result, MYSQL_ASSOC);
  32. $numrows = $row['numrows'];
  33. // how many pages we have when using paging?
  34. $maxPage = ceil($numrows/$rowsPerPage);
  35. // print the link to access each page
  36. $self = $_SERVER['PHP_SELF'];
  37. // creating previous and next link
  38. // plus the link to go straight to
  39. // the first and last page
  40. if ($pageNum > 1)
  41. {
  42. $page = $pageNum - 1;
  43. $prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
  44. $first = " <a href=\"$self?page=1\">[First Page]</a> ";
  45. }
  46. else
  47. {
  48. $prev = '&nbsp;'; // we're on page one, don't print previous link
  49. $first = '&nbsp;'; // nor the first page link
  50. }
  51. if ($pageNum < $maxPage)
  52. {
  53. $page = $pageNum + 1;
  54. $next = " <a href=\"$self?page=$page\">[Next]</a> ";
  55. $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
  56. }
  57. else
  58. {
  59. $next = '&nbsp;'; // we're on the last page, don't print next link
  60. $last = '&nbsp;'; // nor the last page link
  61. }
  62. // print the navigation link
  63. echo $first . $prev .
  64. " Showing page $pageNum of $maxPage pages " . $next . $last;
  65. mysql_close($db_handle);
  66. }
  67. else {
  68. print "Database NOT Found ";
  69. }
  70. ?>