Reputation: 1
I just started to learn PHP this week, and I'd search for possible solution on this but I can make it right. While I sucessfully created a pagination that sorts queries into alphabetical order, I wasn't successful in creating a numerical pagination, in which it will limit the number of queries to be displayed. Also, I wasn't able to combined these two types of pagination with two different queries, one for A-Z sorting, and one for the number of queries to be displayed in a single page. Here's my code
`<?php
include 'includes/connection.php';
$character = '';
$characters = '';
if (isset($_GET['character']))
{
$character = $_GET['character'];
$character = preg_replace('#[^a-z]#i', '', $character);
$sql = "SELECT * FROM drivers_info WHERE last_name LIKE '$character%' ORDER BY last_name";
}
else{
$sql = "SELECT * FROM drivers_info ORDER BY rfid ";
$pageno = 1
}
$result = mysqli_query($db, $sql);
<body>
<br /><br />
<br /> <br />
Here's the code to create an alphabetical pagination
<div class="table-responsive">
<div align="center">
<?php
$character = range('A', 'Z');
echo '<ul class="pagination">';
foreach($character as $alphabet)
{
echo '<li><a href = "users.php?character='.$alphabet.'">'.$alphabet.'</a></li>';
}
echo '</ul>';
?>
</div>
<?php
Here's the code for displaying the queries alphabetically
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result)){
?>
<tr>
<td><?php echo $row["rfid"]; ?></td>
<td><?php echo $row["last_name"]; ?></td>
<td><?php echo $row["first_name"]; ?></td>
<td><?php echo $row["middle_name"]; ?></td>
<td><?php echo $row["gender"]; ?></td>
<td><?php echo $row["age"]; ?></td>
<td><?php echo $row["height"]; ?></td>
<td><?php echo $row["weight"]; ?></td>
<td><?php echo $row["address"]; ?></td>
<td><?php echo $row["contact_no"]; ?></td>
<td><?php echo $row["license_type"]; ?></td>
<td><?php echo $row["license_no"]; ?></td>
<td><?php echo $row["expiration"]; ?></td>
<td><?php echo $row["nationality"]; ?></td>
<td><?php echo $row["eyes_color"]; ?></td>
<td><?php echo $row["blood_type"]; ?></td>
</tr>
<?php
}
}
</body>
If you had any code or idea on it, would you mind sharing it with me? Thank you so much, I needed this to accomplish my school project.
`
Upvotes: 0
Views: 235
Reputation:
Here is best reference code for understanding pagination logic
<?php
$host = "localhost";
$user = "root";
$pass = "New";
$db = "booksgood";
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
// how many rows to show per page
$rowsPerPage = 10;
// by default we show first page
$page_num = 1;
// if $_GET['page'] defined, use it as page number, $_GET gets the page number out of the url
//set by the $page_pagination below
if(isset($_GET['page'])){$page_num = $_GET['page'];}
//the point to start for the limit query
$offset = $page_num;
// Zero is an incorrect page, so switch the zero with 1, mainly because it will cause an error with the SQL
if($page_num == 0) {$page_num = 1;}
// counting the offset
$sql = "SELECT * FROM bookdata where titles like 's%' order by titles LIMIT $offset, $rowsPerPage ";
$res = mysql_query($sql) or die(mysql_error());
// how many rows we have in database
$sql2 = "SELECT COUNT(id) AS numrows FROM bookdata where titles like 's%'";
$res2 = mysql_query($sql2) or die(mysql_error());
$row2 = mysql_fetch_array($res2);
$numrows = $row2['numrows'];
// print the random numbers
while($row = mysql_fetch_array($res))
{
//Echo out your table contents here.
echo $row[1].'<BR>';
echo $row[2].'<BR>';
echo '<BR>';
}
// how many pages we have when using paging?
$numofpages = ceil($numrows/$rowsPerPage);
// print the link to access each page
$self = "/paging3.php?";
if ($numofpages > '1' ) {
$range =15; //set this to what ever range you want to show in the pagination link
$range_min = ($range % 2 == 0) ? ($range / 2) - 1 : ($range - 1) / 2;
$range_max = ($range % 2 == 0) ? $range_min + 1 : $range_min;
$page_min = $page_num- $range_min;
$page_max = $page_num+ $range_max;
$page_min = ($page_min < 1) ? 1 : $page_min;
$page_max = ($page_max < ($page_min + $range - 1)) ? $page_min + $range - 1 : $page_max;
if ($page_max > $numofpages) {
$page_min = ($page_min > 1) ? $numofpages - $range + 1 : 1;
$page_max = $numofpages;
}
$page_min = ($page_min < 1) ? 1 : $page_min;
//$page_content .= '<p class="menuPage">';
if ( ($page_num > ($range - $range_min)) && ($numofpages > $range) ) {
$page_pagination .= '<a class="num" title="First" href="'.$self.'page=1"><</a> ';
}
if ($page_num != 1) {
$page_pagination .= '<a class="num" href="'.$self.'page='.($page_num-1). '">Previous</a> ';
}
for ($i = $page_min;$i <= $page_max;$i++) {
if ($i == $page_num)
$page_pagination .= '<span class="num"><strong>' . $i . '</strong></span> ';
else
$page_pagination.= '<a class="num" href="'.$self.'page='.$i. '">'.$i.'</a> ';
}
if ($page_num < $numofpages) {
$page_pagination.= ' <a class="num" href="'.$self.'page='.($page_num + 1) . '">Next</a>';
}
if (($page_num< ($numofpages - $range_max)) && ($numofpages > $range)) {
$page_pagination .= ' <a class="num" title="Last" href="'.$self.'page='.$numofpages. '">></a> ';
}
//$page['PAGINATION'] ='<p id="pagination">'.$page_pagination.'</p>';
}//end if more than 1 page
echo $page_pagination.'<BR><BR>';
echo 'Number of results - '.$numrows ;
echo ' and Number of pages - '.$numofpages.'<BR><BR>';
// Free resultset
mysql_free_result($res);
// and close the database connection
mysql_close($con);
?>
here is reference code link
Upvotes: 2