I have a page which get's populated dynamically from a Mysql database.
To prevent to much information been displayed at once, I have added pagination to page.
Using this code.
$currentPage = $_SERVER["PHP_SELF"];
$maxRows_atoz = 10; $page= 0;
if (isset($_GET['page'])) {$page= $_GET['page'];}
$startRow_atoz = $page* $maxRows_atoz;
mysql_select_db($database_main, $main);
$query_atoz = "SELECT * FROM main WHERE title LIKE 'A%'";
$query_limit_atoz = sprintf("%s LIMIT %d, %d", $query_atoz, $startRow_atoz, $maxRows_atoz);
$atoz = mysql_query($query_limit_atoz, $main) or die(mysql_error());
$row_atoz = mysql_fetch_assoc($atoz);
if (isset($_GET['totalRows_atoz'])) {$totalRows_atoz = $_GET['totalRows_atoz'];} else {$all_atoz = mysql_query($query_atoz); $totalRows_atoz = mysql_num_rows($all_atoz);}
$totalPages_atoz = ceil($totalRows_atoz/$maxRows_atoz)-1;
$queryString_atoz = ""; if (!empty($_SERVER['QUERY_STRING'])) {$params = explode("&", $_SERVER['QUERY_STRING']); $newParams = array(); foreach ($params as $param) {
if (stristr($param, "pageNum_atoz") == false && stristr($param, "totalRows_atoz") == false) {array_push($newParams, $param);}}
if (count($newParams) != 0) {$queryString_atoz = "&" . htmlentities(implode("&", $newParams));}}
$queryString_atoz = sprintf("&totalRows_atoz=%d%s", $totalRows_atoz, $queryString_atoz);
?>
<ul>
<li class="previous"> <?php if ($page > 0) { // Show if not first page ?>
<a title="See Previous 10 Results" href="<?php printf("?page=%d%s", $currentPage, max(0, $page - 1), $queryString_atoz); ?>">
<img src="/files/previous.png" /></a>
<?php } // Show if not first page ?></li>
<li class="next"> <?php if ($page < $totalPages_atoz) { // Show if not last page ?>
<a title="See Previous 10 Results" href="<?php printf("?page=%d%s", $currentPage, min($totalPages_atoz, $page + 1), $queryString_atoz); ?>">
<img src="/files/next.png" /></a>
<?php } // Show if not last page ?></li>
</ul>
The code works in paginaing the results, but has a limitation.
It currently only display's Next and Previous buttons.
I would like to add page numbers. This way a user can skip to 4th or 5th page in the result set rather than having to press the next button 4 or 5 times.
So, can some please tell me how I would go about doing this?
Thanks
I must say, I havent gone through the entire code of yours. You must indent your code to help others. If I get your question right, the following may help you. I haven't much taken care of finer details. This is just to give you an idea. :-)
<ul>
<li class="previous">
<?php if ($page > 0) { // Show if not first page ?>
<a title="See Previous 10 Results" href="<?php printf("?page=%d%s", $currentPage, max(0, $page - 1),$queryString_atoz); ?>">
<img src="/files/previous.png" /></a>
<?php } // Show if not first page ?></li>
<!--MODIFY_BEGIN-->
<li>
<?php for ($i=0;$i<$page; $i++){
if ($currentPage==$i)
echo ($currentPage+1);
else
echo "<a title='Page $i' href='?page=$i'>Page $i</a>";
} ?>
</li>
<!--MODIFY_END-->
<li class="next"> <?php if ($page < $totalPages_atoz) { // Show if not last page ?>
<a title="See Previous 10 Results" href="<?php printf("?page=%d%s", $currentPage, min($totalPages_atoz, $page + 1), $queryString_atoz); ?>">
<img src="/files/next.png" /></a>
<?php } // Show if not last page ?></li>
</ul>
Related
I have do a pagination with php&html,the problem is my $page is always equal 1 even I click another page and the url is already become ?page=2.The value $p cannot pass as $page.How can I solve this?
<ul class="nospace clear" style="width:1310px;">
<?php
if(isset($_GET['page'])&& $_GET['page']!=""){$page = $_GET["page"];}else{$page=1;}
$current=$page;
$end=12;
if($page=1){$start=0;$previous=$page;$next=$page+1;}
else if($page<=12){$start=$page*12-12;$previous=$page-1;$next=$page+1;}
else{$start=0;$previous=$page-1;$next=12;}
$sql = "Select * from item where Gender='women' AND Category='cloth' LIMIT $start,$end";
$result = mysqli_query($connect,$sql);
while($row=mysqli_fetch_assoc($result)){?>
<img src="../images/demo/<?php echo $row["Pic"]; ?>" style="width:300px;height:280px"><br><p></p><h3><strong><?php echo $row["Name"]; ?></strong></h3><p><?php echo $row["Description"]; ?></p></li>
<?php } ?></ul><figcaption>Page <?php echo"$page ";?> end...</figcaption>
</figure>
</div>
<nav class="pagination">
<ul>
<li>« Previous</li>
<?php
for ($p=1;$p<13;$p++){
if ($page == $p) {?>
<li class="current btn1"><strong><?php echo $p ?></strong></li><?php }
else{?><li><a href="?page=<?php echo $p ?>" class='btn1'><?php echo $p ?></a></li><?php }}?>
<li> Next » </li>
</ul>
I've been researching if this is possible, but I've drawn a blank, I'm wondering if it's possible to optimize these for and if statements together? cheers.
Edit (Updated #2) : Their is an issue with the code not looping through the pages.
<?php
// calculate total number of pages
$total_pages = ceil($total_rows / $records_per_page);
// range of links to show
$range = 2;
// display links to 'range of pages' around 'current page'
$initial_num = $page - $range;
$condition_limit_num = ($page + $range) + 1;
?>
<ul class="pagination margin-zero">
<?php if ($page>1) : ?>
<li>
<a href='<?php echo $page_url; ?>page=1' title='Go to the first page.'>First Page</a>
</li>
<?php endif; ?>
<?php
for ($x = min($initial_num, 0); $x <= max($condition_limit_num-1, $total_pages); $x++) :
if ($x == $page) :
?>
<li class='active'>
<?php echo $x; ?> <span class="sr-only">(current)</span>
</li>
<?php else : ?>
<li>
<a href='<?php echo $page_url; ?>page=<?php echo $x; ?>'><?php echo $x; ?></a>
</li>
<?php
endif;
endfor;
?>
<?php
if ($page<$total_pages) : ?>
<li>
<a href='<?php echo $page_url; ?>page=<?php echo $total_pages; ?>' title='Last page is <?php echo $total_pages; ?>'>
Last Page
</a>
</li>
<?php endif; ?>
</ul>
Of course you can combine them if you think about the maximum and minimum value that $x is allowed to get assigned.
The outer for loop would require $x to be contained in the interval [0; $condition_limit_num)
If you look only at the for and the first if you could decide that the minimum has to be larger than zero or initial_num, so you could for example use the minimum of initial_num and zero. The limit would work the same way using the maximum of condition_limit_num-1 and total_pages as uppermost reachable value, i.e. the interval [min($x, 0); max($condition_limit_num-1;$total_pages)].
But if you take into account the innermost if you require $x to have a specific value ($page). That means that this if is only "true" whenever $page is contained in the intervall [min($x, 0); max($condition_limit_num-1;$total_pages)] - you can reduce that check to a single if.
Update after question update:
Since the innermost if also has an else path the loop cannot be reduced to a single if:
<?php
for ($x = max($initial_num, 0); $x <= min($condition_limit_num-1, $total_pages); $x++)) :
if($x == $page) :
?>
<li class='active'>
<?php echo $x; ?> <span class="sr-only">(current)</span>
</li>
<?php else : ?>
<li>
<a href='<?php echo $page_url; ?>page=<?php echo $x; ?>'><?php echo $x; ?></a>
</li>
<?php
endif;
endfor;
?>
The code I have works absolutely perfectly:
<?
$maxresults = 10;
$total = $row[total];
$pagecount = $total / $maxresults;
if (!isset($_GET['page'])) { $_GET['page'] = '1'; }
$startcount = (($_GET['page'] - 1) * $maxresults + 1);
$stopcount = $startcount + ($maxresults - 1);
$lastTime = null;
$i='0';
$sc = $startcount;
$stmt=$db->prepare("// SELECT STATEMENT");
$stmt->bindParam(':user', $username);
$stmt->execute();
while ( $row = $stmt->fetch() ) {
if (!($sc > $stopcount)) {
$i++;
if( $row['time'] !== $lastTime ) {
if ( $lastTime === NULL ) {
echo '// close of loop';
} else {
echo '// loop grouping as title';
}
$lastTime = $row['time'];
}
if ($i >= $sc) { ?>
// the html to loop
<?
$sc++;
}
}
}
?>
Forward and Back buttons are easy enough to create. if $_GET['page'] * 10 > $total, active the next button, if $_GET['page'] > 1 activate previous button.
What I can't figure out is the correct formula and loop so I can always display the correct number of "page numbered" links. I want to always show 5 based on the 3rd number. So 1 would should 5 links: 1 - 5, 2 would also show 1 - 5, as would 3, 4 would show 2 - 6, 5 would show 3 - 7.
Now obviously, I could just add 2 and subtract 2 from the current $_GET[page] if the get page is greater than 3, but I need to take into account testing to see if the results go that high. So only if results are greater than 10 should number 2 show, and then if on page 16 pages 17 and 18 should only show if there are 170 and 180 results to display.
Perhaps I have just been staring at the code to long, but I can't seem to get a formula or loop correct in my head to even attempt the code page.
It was a fairly specific question. Wasn't sure if anyone would help with something so specific or not. Guess I learned.
Here is the answer to my own question:
<ul>
<li class="<? if ($_GET['page'] < '2') { echo 'disabled' ; } ?>">
<a href="<?
if ($_GET['page'] == '2') {
echo 'inbox.php';
} else {
echo '?page='.($_GET[page] - 1);
}
?>">Prev</a>
</li> <!-- previous button-->
<li style="<? if ($_GET['page'] - 2 < '1') { echo 'display:none;'; } ?>">
<a href="?page=<?
echo ($_GET[page] -2); ?>">
<? echo ($_GET[page] -2); ?>
</a>
</li> <!-- the button 2 before current spot if its greater than 0 -->
<li style="<? if ($_GET['page'] - 1 < '1') { echo 'display:none;'; } ?>">
<a href="?page=<?
echo ($_GET[page] -1); ?>">
<? echo ($_GET[page] -1); ?>
</a>
</li> <!-- the button 1 before current spot if its greater than 0 -->
<li class="disabled">
<? echo $_GET[page]; ?>
</li> <!-- current button disabled -->
<li style="<? if ($total - ($_GET[page] * 10) <= '0') { echo 'display:none;'; } ?>">
<a href="?page=<?
echo ($_GET[page] + 1); ?>">
<? echo ($_GET[page] +1); ?>
</a>
</li> <!-- the button 1 after current spot if results go that high -->
<li style="<? if ($total - ($_GET[page] + 1) * 10 <= '0') { echo 'display:none;'; } ?>">
<a href="?page=<?
echo ($_GET[page] + 2); ?>">
<? echo ($_GET[page] + 2); ?>
</a>
</li> <!-- the button 2 after current spot if results go that high -->
<li class="<? if ($total - ($_GET[page] * 10) <= '0') { echo 'disabled'; } ?>">
Next
</li> <!-- the next button if results go that high -->
</ul>
#bruce after seeing your solution, I'd like to give you my own solution.
$page = $_GET['page'];
$max_pages = ceil($total / $maxresults);
$min_pages = 1;
$start = max($min_pages, $page-2);
$end = min($max_pages, $start+2);
$nav = ($page > $min_pages ? '<li><</li>' : '' );
for ($i = $start; $i <= $end; $i++)
$nav .= '<li' . ($i == $page ? 'class="active"' : '' ) . '>' . $i . '</li>';
$nav .= ($page < $max_pages ? '<li>></li>' : '' );
I'm sitting at record id 1 and hit the Previous link, it goes to record 0, then record -1, then -2, and so on. I'm trying to show just the 'Next' link if I'm on record id 1, else show both links.
<ul class="pager">
<?php if (href="?read=<?=htmlspecialchars($_GET['read'] = 1)) { ?>
<li class="previous">Next</li>
<?php } else { ?>
<li class="previous">Previous</li>
<li class="previous">Next</li>
</ul>
<?php
//get page number or set to 1 if not page is set
$read = isset($_GET['read']) ? (int)$_GET['read'] : 1;
$limit = 5;
?>
<ul class="pager">
<?php if ($read > 1): ?>
<li class="previous">Previous</li>
<?php endif ?>
<?php if ($read < $limit): ?>
<li class="previous">Next</li>
<?php endif ?>
</ul>
ps: for NEXT link there should probably class next not previous but you have previous in your code so I left it at it is
Change this line
<?php if (href="?read=<?=htmlspecialchars($_GET['read'] = 1)) { ?>
To this
<?php if (href="?read=<?=htmlspecialchars($_GET['read'] == 1)) { ?>
$_GET['read'] = 1 statement always sets $_GET['read'] variable to 1.
I am working with two pages which have included the navigation menu. The problem I am having is to show which one is active. Right now Rapport stays active all the time, even if I am in the diagram. I noticed that $page is always returning Rapport, which I dont know why.
In the diagram (index.php) I have:
include('../rapport/navigasjon.html');
$page = 'diagram';
$_SESSION['diagram']= $page;
In the rapport (index.php) I have:
include('navigasjon.html');
$page = 'rapport';
$_SESSION['rapport']= $page;
And in navigasjon.html I have:
session_start();
$page = $_SESSION['diagram'];
$page = $_SESSION['rapport'];
AND:
<li class='pil <?php if($page=='rapport') {echo 'active';} ?>'> <a href="../rapport/index.php" class='rapport' >Rapport</a></li>
<li class='pil <?php if($page=='diagram') {echo 'active';} ?>' ><a href="../diagram/index.php" class='diagram' >Diagram</a></li>
set $page variable BEFORE including your navigation :
diagram/index.php :
$page = 'diagram';
$_SESSION['diagram']= $page;
include('../rapport/navigasjon.html');
and rapport/index.php :
$page = 'rapport';
$_SESSION['rapport']= $page;
include('navigasjon.html');
In navigasjon.html remove these lines :
$page = $_SESSION['diagram'];
$page = $_SESSION['rapport'];
If you are trying to set a class to an active menu, you can try to this using directories names.
<?php
$pathInfo = pathinfo($_SERVER['PHP_SELF']);
$page = strtolower(end(explode('/', $pathinfo['dirname'])));
?>
<li class='pil <?php if($page=='rapport') {echo 'active';} ?>'> <a href="../rapport/index.php" class='rapport' >Rapport</a></li>
<li class='pil <?php if($page=='diagram') {echo 'active';} ?>' ><a href="../diagram/index.php" class='diagram' >Diagram</a></li>