Guest User

Untitled

a guest
Jan 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. <style>table,th,td{border:1pt solid #ccc;text-align:center;}</style>
  2.  
  3. Show:
  4. <a href='?show=active'>active</a> -
  5. <a href='?show=closed'>closed</a> -
  6. <a href='?show=all'>all</a>
  7.  
  8. <table id="tickets">
  9. <tr>
  10. <th>Name</th>
  11. <th>Email</th>
  12. <th>Subject</th>
  13. <th>Created on</th>
  14. <th style="width:65px;">Status</th>
  15. <th>Actions</th>
  16. </tr>
  17.  
  18. <?php
  19. //connect to db
  20. mysql_connect("localhost", "root", "") or die("ERROR: No se pudo conectar con el servidor.");
  21. mysql_select_db("tickets") or die("ERROR: No se pudo seleccionar la base de datos.");
  22.  
  23. //This function will be called later after display the data
  24. //This returns links like: |First|Prev|Current/Total|Next|Last
  25. function pagination($currentPage, $totalPages){
  26. //to maintain other get data
  27. $self = $_SERVER['PHP_SELF'];
  28. //define previous and next
  29. $prev = ($currentPage > 1) ? $currentPage - 1 : false;
  30. $next = ($currentPage < $totalPages) ? $currentPage + 1 : false;
  31. //here we'll save the resulting code
  32. $pagination = '';
  33. //first
  34. $pagination .= ($currentPage == 1) ? "First " : "<a href='$self?page=1'>First</a> ";
  35. //previous
  36. $pagination .= $prev ? "<a href='$self?page=$prev'>Previous</a> " : "Previous ";
  37. //current/total
  38. $pagination .= "Page $currentPage of $totalPages ";
  39. //next
  40. $pagination .= $next ? "<a href='$self?page=$next'>Next</a> " : "Next ";
  41. //last
  42. $pagination .= ($currentPage == $totalPages) ? "Last " : "<a href='$self?page=$totalPages'>Last</a> ";
  43.  
  44. return $pagination;
  45. }
  46.  
  47. //define how many tickets per page
  48. $ticketsPerPage = 10;
  49.  
  50. //QUERY: active or not
  51. $show = isset($_GET['show']) ? $_GET['show'] : "";
  52. if($show == "active") $condition = "WHERE is_active='1'";
  53. elseif($show == "closed") $condition = "WHERE is_active='2'";
  54. else $condition = "";
  55.  
  56. //get total of tickets on database
  57. $totalQuery = "SELECT count(*) FROM tickets $condition";
  58. $result = mysql_query($totalQuery);
  59. $queryData = mysql_fetch_row($result);
  60. $totalTickets = $queryData[0];
  61.  
  62. //define total pages
  63. $totalPages = ceil($totalTickets/$ticketsPerPage);
  64.  
  65. //get current page or define it as 1 and be sure is within range
  66. $currentPage = isset($_GET['page']) ? $_GET['page'] : 1;
  67. if($currentPage < 1 || $currentPage > $totalPages) $currentPage = 1;
  68.  
  69. //QUERY: limit
  70. $limit = ' LIMIT ' .($currentPage - 1) * $ticketsPerPage .',' .$ticketsPerPage;
  71.  
  72. //FINAL QUERY
  73. $query = "SELECT * FROM tickets $condition $limit";
  74. $result = mysql_query($query);
  75.  
  76. //Display the results
  77. while($info = mysql_fetch_assoc($result)){
  78. //data
  79. $name = $info['name'];
  80. $email = $info['email'];
  81. $subject = $info['subject'];
  82. $ticketid = $info['ticket'];
  83. $isActive = $info['is_active'];
  84. $created = $info['created'];
  85. //status
  86. if($isActive == "1") $status = "active";
  87. else if($isActive == "2") $status = "closed";
  88. else $status = "";
  89. echo "<tr>"
  90. ."<td>$name</td>"
  91. ."<td>$email</td>"
  92. ."<td>$subject</td>"
  93. ."<td>$created</td>"
  94. ."<td>$status</td>"
  95. ."<td></td>"
  96. ."</tr>";
  97. }
  98. echo "</table>";
  99.  
  100. //Finally call pagination
  101. echo pagination($currentPage, $totalPages);
  102.  
  103. ?>
Add Comment
Please, Sign In to add comment