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;
?>
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 have to make a small adjustment on a website, the problem comes on the page.
For example I have a page with 11 button, these 1 I do not have to show them at once, but I have for example I have to show the first 5 and then the page has to be moving but I never have to stop showing 5 results on the page.
I leave reference images:
This is the way the page does NOT have to look:
This is the way the page should look:
for example if I get to 7 that 3 is no longer shown and shows me 8
This is my code
<nav>
<ul class="pagination">
<?php if ($pagina == 1) : ?>
<li class="page-item disabled">
<a class="page-link" href="">
<span class="ti-arrow-left"></span>
</a>
</li>
<?php else : ?>
<li class="page-item">
<a class="page-link" href="?pagina=<?php echo $pagina - 1 ?>">
<span class="ti-arrow-left"></span>
</a>
</li>
<?php endif; ?>
<!---------------------------------------------------------------------->
<?php
for ($i = 1; $i <= $numeroDePaginas; $i++) {
if ($pagina == $i) {
echo "<li class='page-item active'>
<a class='page-link' href='?pagina=$i'>$i</a>
</li>";
} else {
if (($i + 2) < $pagina || ($i - 2) > $pagina) {
echo "<li class='page-item'>
<a class='page-link' href='?pagina=$i'>$i</a>
</li>";
}
}
}
?>
<!---------------------------------------------------------------------->
<?php if ($pagina == $numeroDePaginas) : ?>
<li class='page-item disabled'>
<a class='page-link' href=''>
<span class='ti-arrow-right'></span>
</a>
</li>
<?php else : ?>
<li class='page-item'>
<a class='page-link' href="?pagina=<?php echo $pagina + 1 ?>">
<span class='ti-arrow-right'></span>
</a>
</li>
<?php endif; ?>
</ul>
</nav>
Here you go. I have added next_pages variable and previous_pages variables, which will make sure to get the pagina - 2 and pagina + 2 if exists. Let me know if you need help understanding it.
<nav>
<?php
$next_pages = ($pagina + 2) <= $numeroDePaginas ? $pagina + 2 : $numeroDePaginas;
$previous_pages = ($pagina - 2) >= 1 ? $pagina - 2 : 1;
?>
<ul class="pagination">
<?php if ($pagina == 1) : ?>
<li class="page-item disabled">
<a class="page-link" href="">
<span class="ti-arrow-left"></span>
</a>
</li>
<?php else : ?>
<li class="page-item">
<a class="page-link" href="?pagina=<?php echo $pagina - 1 ?>">
<span class="ti-arrow-left"></span>
</a>
</li>
<?php endif; ?>
<!---------------------------------------------------------------------->
<?php
for ($i = $previous_pages; $i <= $next_pages; $i++) {
if ($pagina == $i) {
echo "<li class='page-item active'>
<a class='page-link' href='?pagina=$i'>$i</a>
</li>";
} else {
echo "<li class='page-item'>
<a class='page-link' href='?pagina=$i'>$i</a>
</li>";
}
}
?>
<!---------------------------------------------------------------------->
<?php if ($pagina == $numeroDePaginas) : ?>
<li class='page-item disabled'>
<a class='page-link' href=''>
<span class='ti-arrow-right'></span>
</a>
</li>
<?php else : ?>
<li class='page-item'>
<a class='page-link' href="?pagina=<?php echo $pagina + 1 ?>">
<span class='ti-arrow-right'></span>
</a>
</li>
<?php endif; ?>
</ul>
</nav>
Edit with more explaination:
I have added a variable called next_pages, this variable checks if the current page (ex: 9) + 2 is smaller than or equal to "numeroDePaginas" variable, I have done this to make sure that we never show any pages exceeding the number of pages we have, in case 9+2 is bigger than "numeroDePaginas" we set the variable equal to "numeroDePaginas"
For the previous_pages variable, we make sure that the current page(ex: 3) - 2 is never less than 1, (so it doesn't show 0 or negative pages for example), in case
3-2 is smaller than 1, we set the variable to 1.
Now, I have edited the for loop, to make sure that it starts from the previous_pages variable, and ends with next_pages variable.
Here are a test case:
$pagina = 7;
$numeroDePaginas = 9;
//$next_pages will be = 9 (i.e 7+2)
//$previous_pages will be = 5 (i.e 7-2)
for loop would print pages from 5 to 9.
I use Kirby CMS as backend.
I want following structure for my html output:
<ul>
<li class="link-1">Link</li>
<li class="link-2">Link</li>
<li class="link-3">Link</li>
<li class="link-4">Link</li>
</ul>
I have following code:
<?php foreach($pages->visible() AS $p): ?>
<?php $nbr = $pages->countVisible()?>
<li class="link-<?php for ($i = 1; $i <= $nbr; $i++){echo $i;} ?>">
<a<?php echo ($p->isOpen()) ? ' class="active"' : '' ?> href="<?php echo $p->url() ?>"><?php echo html($p->title()) ?></a></li>
<?php endforeach ?>
But instead I only get the css class
link-1234
in each of the links, so it is making the for loop, but I need only one number per foreach loop.
This code made it work:
<li class="link-<?php static $x=1; echo $x; $x++; ?>">
<li class="link-<?php for ($i = 1; $i <= $nbr; $i++){echo $i;} ?>">
only loops inside that element
<?php for ($i = 1; $i <= $nbr; $i++){
echo "<li class=\"link-$i\">";
echo 'the rest of the line';
} ?>
should loop the whole block
I Have a ordered list and i want to generate a incemental number for the data-slide-to starting from 0
That is the php code
get("display_indicators", 1)): ?>
$item){
$activeclass = "";
if($key == 0){
$activeclass = "active";
}
?>
id;?>" data-slide-to="" class="">
You could use a variable to contain a counter...
<?php $counter = 0; ?>
<li data-target="#carousel<?php echo $module->id;?>" data-slide-to="<?php
echo $counter;
$counter++;
?>" class="<?php echo $activeclass; ?>"></li>
You could even use the following instead of echo-ing and incrementing on two lines...
echo $counter++;
This echoes the current count and increments afterwards, but some people prefer to avoid this.
Try this, hope it'll help you
<?php $uniqueNo=0; ?>
<li data-target="#carousel<?php echo $module->id;?>" data-slide-to="
<?php echo $uniqueNo+=1; ?>" class="<?php echo $activeclass; ?>"></li>
Use an incremential counter
<?php $counter = 0; ?>
<ul>
enter code here
<li data-target="#carousel<?php echo $module->id;?>" data-slide-to="<?=$counter++?>"
class="<?php echo $activeclass; ?>"></li>
this however could not be "unique" application wide
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>