Skip to content

Instantly share code, notes, and snippets.

Created September 22, 2011 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1233850 to your computer and use it in GitHub Desktop.
Save anonymous/1233850 to your computer and use it in GitHub Desktop.
<style>table,th,td{border:1pt solid #ccc;text-align:center;}</style>
Show:
<a href='?show=active'>active</a> -
<a href='?show=closed'>closed</a> -
<a href='?show=all'>all</a>
<table id="tickets">
<tr>
<th>Name</th>
<th>Email</th>
<th>Subject</th>
<th>Created on</th>
<th style="width:65px;">Status</th>
<th>Actions</th>
</tr>
<?php
//connect to db
mysql_connect("localhost", "root", "") or die("ERROR: No se pudo conectar con el servidor.");
mysql_select_db("tickets") or die("ERROR: No se pudo seleccionar la base de datos.");
//This function will be called later after display the data
//This returns links like: |First|Prev|Current/Total|Next|Last
function pagination($currentPage, $totalPages){
//to maintain other get data
$self = $_SERVER['PHP_SELF'];
//define previous and next
$prev = ($currentPage > 1) ? $currentPage - 1 : false;
$next = ($currentPage < $totalPages) ? $currentPage + 1 : false;
//here we'll save the resulting code
$pagination = '';
//first
$pagination .= ($currentPage == 1) ? "First " : "<a href='$self?page=1'>First</a> ";
//previous
$pagination .= $prev ? "<a href='$self?page=$prev'>Previous</a> " : "Previous ";
//current/total
$pagination .= "Page $currentPage of $totalPages ";
//next
$pagination .= $next ? "<a href='$self?page=$next'>Next</a> " : "Next ";
//last
$pagination .= ($currentPage == $totalPages) ? "Last " : "<a href='$self?page=$totalPages'>Last</a> ";
return $pagination;
}
//define how many tickets per page
$ticketsPerPage = 10;
//QUERY: active or not
$show = isset($_GET['show']) ? $_GET['show'] : "";
if($show == "active") $condition = "WHERE is_active='1'";
elseif($show == "closed") $condition = "WHERE is_active='2'";
else $condition = "";
//get total of tickets on database
$totalQuery = "SELECT count(*) FROM tickets $condition";
$result = mysql_query($totalQuery);
$queryData = mysql_fetch_row($result);
$totalTickets = $queryData[0];
//define total pages
$totalPages = ceil($totalTickets/$ticketsPerPage);
//get current page or define it as 1 and be sure is within range
$currentPage = isset($_GET['page']) ? $_GET['page'] : 1;
if($currentPage < 1 || $currentPage > $totalPages) $currentPage = 1;
//QUERY: limit
$limit = ' LIMIT ' .($currentPage - 1) * $ticketsPerPage .',' .$ticketsPerPage;
//FINAL QUERY
$query = "SELECT * FROM tickets $condition $limit";
$result = mysql_query($query);
//Display the results
while($info = mysql_fetch_assoc($result)){
//data
$name = $info['name'];
$email = $info['email'];
$subject = $info['subject'];
$ticketid = $info['ticket'];
$isActive = $info['is_active'];
$created = $info['created'];
//status
if($isActive == "1") $status = "active";
else if($isActive == "2") $status = "closed";
else $status = "";
echo "<tr>"
."<td>$name</td>"
."<td>$email</td>"
."<td>$subject</td>"
."<td>$created</td>"
."<td>$status</td>"
."<td></td>"
."</tr>";
}
echo "</table>";
//Finally call pagination
echo pagination($currentPage, $totalPages);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment