Between Pagination - php

Help me, i was writing my code, all done succesfully but my problem is when the "..." (separator) for my curerent page and last page showed after the last page, not between my current page and my last page
how can that possible ?
this is my script for showing the page :
<?php
$paging2 = mysqli_query($koneksi,"select * from laporan where seksi='{$seksi}'");
$jmldata = mysqli_num_rows($paging2);
$jmlhalaman = ceil($jmldata/$batas);
echo "<ul class='pagination'>";
if ($halaman !== 1){
echo "<li><a href='index.php?load=table_laporan&halaman=1'>First</a></li>";
}
if ($halaman > 1){
echo "<li><a href='index.php?load=table_laporan&halaman=".($halaman-1)."'>Previous</a></li>";
}
for($i =1; $i <= $jmlhalaman; $i++){
if ((($i >= $halaman - 3) && ($i <= $halaman + 3)) || ($i == 1) || ($i ==$jmlhalaman))
{
if ($i == $halaman){
$class="active";
} else{
$class="disable";
} //echo "<b>$i</b>";
echo "<li class='$class'>".$i."<span class='sr-only'>(current)</span></li>";
if($i==$jmlhalaman && $halaman <= $jmlhalaman-5) echo "<li><a>...</a></li>";
if($i==1 && $halaman >= 6) echo "<li><a>...</a></li>";
}
}
if ($halaman >= 1){
echo "<li><a href=index.php?load=table_laporan&halaman=". ($halaman+1)."'>Next</a></li>";
} if ($halaman >= 1){
echo "<li><a href='index.php?load=table_laporan&halaman=". ($jmlhalaman)."'>Last</a></li>";
}
echo "</ul>";
echo "<p>Total Data : <b>$jmldata</b> Data</p>";
?>

Related

php navigation pages issue

I have a strange problem with my navigation next/previous code. Everything works fine but random after I click on the next page, the whole navigation is gone. And when I change the pagina variable from page 4 to 5 for example, does everything work again, but pagina 4 keeps removing the navigation. I don't know why it occurs?
On the top of my page do I have:
if (isset($_GET['pagina']) && $_GET['pagina']!="") {
$page_no = $_GET['pagina'];
}
else {
$page_no = 1;
}
$total_records_per_page = 100;
$offset = ($page_no-1) * $total_records_per_page;
$previous_page = $page_no - 1;
$next_page = $page_no + 1;
$adjacents = "2";
And on the bottom of my page
$taal = htmlspecialchars(addslashes($_GET['taal']));
if($page_no > 1){
echo <a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$previous_page."'>Vorige</a>";
}
if ($total_no_of_pages <= 10){
for ($counter = 1; $counter <= $total_no_of_pages; $counter++){
if ($counter == $page_no) {
echo "<a class='active'>".$counter."</a>";
}
else
{
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
}
elseif ($total_no_of_pages > 10){
if($page_no <= 4) {
for ($counter = 1; $counter < 8; $counter++){
if ($counter == $page_no) {
echo "<span class='active'><a>".$counter."</a></span>";
}
else {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
echo "<a>...</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$second_last."'>".$second_last."</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$total_no_of_pages."'>".$total_no_of_pages."</a>";
}
elseif($page_no > 4 && $page_no < $total_no_of_pages - 4) {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=1'>1</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=2'>2</a>";
//echo "<li><a>...</a></li>";
for (
$counter = $page_no - $adjacents;
$counter <= $page_no + $adjacents;
$counter++
) {
if ($counter == $page_no) {
echo "<span class='active'><a>".$counter."</a></span>";
}else{
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
echo "<a>...</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$second_last."'>".$second_last."</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$total_no_of_pages."'>".$total_no_of_pages."</a>";
}
else {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=1'>1</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=2'>2</a>";
echo "<a>...</a>";
for (
$counter = $total_no_of_pages - 6;
$counter <= $total_no_of_pages;
$counter++
)
{
if ($counter == $page_no) {
echo "<span class='active'><a>".$counter."</a></span>";
}
else{
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
}
}
if($page_no < $total_no_of_pages) {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$next_page."'>Volgende</a>";
}
$taal is language,
$page_no is which page where I'm on
$zoekveld is search field to search on
$pagina is pagenumber in url
I think it has something to do with my GET search field (Zoekveld) but why occurs it after a couple of pages?
Thanks in advance

How to create pagination that shows previous and next 3 pages

I want to show a pagination which only shows the recent and next 3 pages, but I can't get it working.
Here's my current code:
$stmt = MySQL::connection3()->prepare("SELECT COUNT(ID) AS TOTAL FROM products");
$stmt->execute();
$row = $stmt->fetch();
$total_pages = ceil(intval($row["TOTAL"]) / $results_per_page);
foreach (range(1, $total_pages) as $i) {
if ($i == $page) {
?>
<li class="page-item active">
<?php echo $i ?>
</li>
<?php
} else {
?>
<li class="page-item">
<?php echo $i ?>
</li>
<?php
}
}
Thanks!
I create this example you can run and adapt with your code:
<style>.active{color:green!important}</style>
<?php
echo get_pagination_links('10','100');
function get_pagination_links($current_page, $total_pages)
{
$links = "";
if ($total_pages >= 1 && $current_page <= $total_pages) {
$i = max(2, $current_page - 3);
for (; $i < min($current_page + 4, $total_pages); $i++) {
if($i==$current_page){
$links .= "<li class='page-item active'><a href='?page=$i;' class='page-link'>$i</a></li>";
}else{
$links .= "<li class='page-item'><a href='?page=$i;' class='page-link'>$i</a></li>";
}
}
return $links;
}
}
?>
Output:
7-8-9-10-11-12-13
obviously I didn't connect a database and therefore I used a static result

pagination in php with mysql

I wonder how to easily make pagination in PHP with MySQL.. May I ask you a favor of you?
This my code have not error... take this code with your project implement..
<ul class="pagination justify-content-end">
<?php
$sql = "SELECT * FROM json_data";
$rs_result1 = mysqli_query($connect, $sql);
$row = mysqli_num_rows($rs_result1);
//print_r($rs_result1); die();
$limit = 10;
$total_records = $row;
//$total_pages = ceil($total_records / $limit)-220;
// calculate total pages
$total_pages = ceil($row / $limit);
$prev = $page-1;
$next = $page+1;
$pagination_buttons =5;
$last_page = $total_pages;
$half = floor($pagination_buttons/2);
//echo '<ul class="pagination">';
if($page >= 5){
echo '<li>First</li>';
}
if($page < $pagination_buttons AND ($last_page == $pagination_buttons OR $last_page > $pagination_buttons)){
for($i=1; $i<=$pagination_buttons; $i++){
if($i == $page){
echo '<li class="active">'.$i.'</li>';
}
else{
echo '<li>'.$i.'</li>';
}
}
if($last_page > $pagination_buttons){
echo '<li>Next</li>';
}
}
else if($page >= $pagination_buttons AND $last_page > $pagination_buttons){
if(($page+$half) >= $last_page){
echo '<li>Previous</li>';
for ($i=($last_page-$pagination_buttons)+1; $i<=$last_page; $i++) {
if($i == $page){
echo '<li class="active">'.$i.'</li>';
}
else{
echo '<li>'.$i.'</li>';
}
}
}
else if(($page+$half) < $last_page){
echo '<li>Previous</li>';
for ($i=($page-$half); $i<=($page+$half); $i++) {
if($i == $page){
echo '<li class="active">'.$i.'</li>';
}
else{
echo '<li>'.$i.'</li>';
}
}
echo '<li>Next</li>';
}
}
if($page != $total_pages && $total_pages >= 6){
echo '<li>Last</li>';
} ?>
i solve it my code in pagination in php with mysql.. and hope may help this code your pagination project...
I coded pagination code for my site in php and mysql.. I wonder how to easily make pagination in PHP with MySQL.. May I ask you a favor of you? This my code have not error... take this code with your project implement..

How to remove image output using a counter in the range

Now the output of the images is carried out using a counter in 3 intervals:
0-73
73-88
88-141
Question: How to remove image output using a counter in the range 0-73?
$name = glob('./album/*.{php}', GLOB_BRACE);
$counter = 0; //заводим счетчик
for($i=0; $i<=(sizeof($name)-1); $i++) {
if( substr($name[$i][2],0,1) != "_") {
echo "<li><img src='".$dir."img/_share/".$shortname."_1.jpg' height='162px'></li>";
$counter++;
if ($counter == 73) {
echo "<li><img src='".$dir."img/_share/".$shortname."_1.jpg' height='162px'></li>";
} elseif ($counter == 88) {
require_once "ddd.php";
} elseif ($counter == 141) {
break;
}
}
}
You need to change == comparison operator to > otherwise it will only execute when $counter is 73. Make sure to remove the extra code that displays the image throughout the loop.
$name = glob('./album/*.{php}', GLOB_BRACE);
$counter = 0; //заводим счетчик
for($i=0; $i<=(sizeof($name)-1); $i++) {
if( substr($name[$i][2],0,1) != "_") {
//echo "<li><img src='".$dir."img/_share/".$shortname."_1.jpg' height='162px'></li>";
$counter++;
if ($counter > 73 && $counter <= 88) {
echo "<li><img src='".$dir."img/_share/".$shortname."_1.jpg' height='162px'></li>";
} elseif ($counter > 88 && $counter <= 141) {
require_once "ddd.php";
} elseif ($counter > 141) {
break;
}
}
}

php pagination, something wrong

Some code from me.
$files = glob("pardod/*.html");
$record_count = 5;
$total_pages = ceil(count($files)/$record_count);
$page = $_GET['page'];
$offset = ($page-1)*$record_count;
$files_filter = array_slice($files, $offset,$record_count);
for ($i = 0; $i<$filecount; $i++){
if ($page){
$start = ($page - 1) * $record_count;
}else{
$start = 0;
}
}
if($total_pages > 1){
if($page != 1){
echo 'Atpakal';
}
if($page != $total_pages){
echo 'Uz priekšu';
}
}
The php pagination dont work, i am just learning how to make, where is a problem?
The *.html files didn't shows :(
Try this code for pagination
<?php
$con=mysql_connect("localhost","root","");
$page=$_REQUEST['page'];
if ($page < 1)
{
$page = 1;
}
$resultsPerPage =15;
$startResults = ($page - 1) * $resultsPerPage;
$numberOfRows = mysql_num_rows(mysql_query('SELECT * FROM tablename'));
$totalPages = ceil($numberOfRows / $resultsPerPage);
echo"<center><table border='1' bordercolor='blue' height='90%' width='90%'> <tr><th bgcolor='silver'>Name</th><th bgcolor='silver'>Password</th><th bgcolor='silver'>Question</th><th bgcolor='silver'> Answer</th><th bgcolor='silver'>Image</th> </tr>";
$i=1;
$result= mysql_query("SELECT * FROM password LIMIT $startResults, $resultsPerPage");
while($row=mysql_fetch_array($result))
{
}
echo"</tr></table></center>";
echo '<center>First&nbsp';
if($page > 1)
echo 'Back&nbsp';
for($i = 1; $i <= $totalPages; $i++)
{
if($i == $page)
echo '<strong>'.$i.'</strong>&nbsp';
else
echo ''.$i.'&nbsp';
}
if ($page < $totalPages)
echo 'Next ';
echo 'Last</center>';
?>
Try This Code
$limit = ( isset($_GET['limit'])) ? $_GET['limit'] : 5;
if (strtolower($limit) == 'all') {
$limit = 'all';
} else {
$limit = filter_var($limit, FILTER_SANITIZE_NUMBER_INT);
if (trim($limit) == '') {
$limit = 5;
}
}
$page = ( isset($_GET['page'])) ? $_GET['page'] : 1;
$page = filter_var($page, FILTER_SANITIZE_NUMBER_INT);
$links = ( isset($_GET['links'])) ? $_GET['links'] : 1;
$links = filter_var($links, FILTER_SANITIZE_NUMBER_INT);
Here is the link where step by step described all the process in details.
Simple Pagination in PHP By Learning Ocean Team
if you are using bootstrap, you can use the following
function pagination($page, $count) {
global $options;
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$actual_link = trim(str_replace("page=".$page, "", $actual_link), "&");
$lenght = ceil($count/$options->get("result_per_page"));
if ($page <= 3) {
if ($lenght < 5) {
$first = 0;
$last = $lenght-1;
} else {
$first = 0;
$last = 4;
}
} else if ($page < ($lenght-2)) {
$first = $page-2;
$last = $page+2;
} else if ($page <= $lenght) {
$first = $page-5;
$last = $lenght-1;
}
if ($page > 0) {
$prev = $page-1;
$next = $page+1;
} else {
$prev = 0;
$next = $page+1;
}
echo '<span>Viewing page '.($page+1).' of '.$lenght.'</span><br>';
echo '<nav aria-label="Page navigation example">';
echo '<ul class="pagination justify-content-center">';
if ($prev > 0) {
echo '<li class="page-item"><a class="page-link" href="'.$actual_link.'&page=0">««</a></li>';
echo '<li class="page-item"><a class="page-link" href="'.$actual_link.'&page='.$prev.'">«</a></li>';
}
for ($i = $first; $i<=$last; $i++) {
if ($i == $page) {
$active = ' active';
} else {
$active = '';
}
echo '<li class="page-item'.$active.'"><a class="page-link" href="'.$actual_link.'&page='.$i.'">'.($i+1).'</a></li>';
}
if ($next < $last) {
echo '<li class="page-item"><a class="page-link" href="'.$actual_link.'&page='.$next.'">»</a></li>';
echo '<li class="page-item"><a class="page-link" href="'.$actual_link.'&page='.($lenght-1).'">»»</a></li>';
}
echo '</ul>';
echo '</nav>';
}
to call the function
$pageNumber = 1;
$totalRowCount = 5000;
pagination($pageNumber, $totalRowCount);

Categories