Pagination next previous multiplied - php

Please forgive me if i make some mistake in english its not my native language.
To the point. This is my pagination code:
if (!isset($_GET["page"])) $_GET["page"] = 1;
if (sizeof($dirs) + sizeof($files) > $thumbs_pr_page)
{
$page_navigation .= "$label_page ";
for ($i=1; $i <= ceil((sizeof($files) + sizeof($dirs)) / $thumbs_pr_page); $i++)
{
if ($_GET["page"] == $i)
$page_navigation .= "$i";
else
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($i) . "'>" . $i . "</a>";
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($i-1) . "'> Previous </a>";
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($i+1) . "'> Next </a>";
if ($i != ceil((sizeof($files) + sizeof($dirs)) / $thumbs_pr_page)) $page_navigation .= " | ";
}
}
Any ideas where is the problem ? The next/ previous button is mutliplied. But it should be only on the left side should be previous and on the right side next. I am only learning php now and its hard for me. Here you can see it how it looks: http://tinypic.com/view.php?pic=2djl3m&s=5 and it should be like this: http://tinypic.com/view.php?pic=2eeli54&s=5

You need to move the next and previous button out of your for-loop. Previous at front, next after it. Next and Previous dont need to depend on $i - they should depend on the currently loaded page, only.
if (!isset($_GET["page"])) $_GET["page"] = 1;
if (sizeof($dirs) + sizeof($files) > $thumbs_pr_page)
{
$page_navigation .= "$label_page ";
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($_GET["page"]-1) . "'> Previous </a>";
for ($i=1; $i <= ceil((sizeof($files) + sizeof($dirs)) / $thumbs_pr_page); $i++)
{
if ($_GET["page"] == $i)
$page_navigation .= "$i";
else
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($i) . "'>" . $i . "</a>";
if ($i != ceil((sizeof($files) + sizeof($dirs)) / $thumbs_pr_page)) $page_navigation .= " | ";
}
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($_GET["page"]+1) . "'> Next </a>";
}
But you should also "catch" 0 and n+1 pages (then no Previous / Next Button is required)

Problem is in your for loop. You are checking if $_GET['page'] == $i and if not you are concatenating links to $page_navigation
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($i) . "'>" . $i . "</a>";
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($i-1) . "'> Previous </a>";
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($i+1) . "'> Next </a>";
This will generate Next / Previous link for each iteration where $_GET['page'] == $i is not true (which is always except once). Since you need them only once (relative to current $_GET['page']) you need to move them outside the loop.

First of all, please try and put brackets in the after the else, because, the last two statements in the else will not be a part of the else clause, which will cause the Previous and Next to be a part of the for(;;;) instead of the else, and that is causing it to maybe repeat the Previous and Next.
if ($_GET["page"] == $i)
$page_navigation .= "$i";
else{
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($i) . "'>" . $i . "</a>";
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($i-1) . "'> Previous </a>";
$page_navigation .= "<a href='?dir=" . $_GET["dir"] . "&page=" . ($i+1) . "'> Next </a>";
}

Related

Problem with showing pagination page numbers

I have a problem coding the numbers of pages of a pagination.
I will be as simple as possible in detailing what I need to do.
There are N numbers of pages (keep track as $listPages);
I need to show a maximum of 5 pages numbers generated as $listCount "1(current),2,3,4,5 + Next + Last" and keep the pattern with increasing pages (example for page 33 "First + Prev + 31,32,33 (current),34,35 + Next + Last");
$list keep track of the current page;
I written this code for the moment but it doesn't work correctly giving as output (example on page 7 "1,2,3,4,5,6,7(current),8,9 + Next + Last) it works going up, but it keep previous numbers. I hope some could help coding it, thanks!
function ShowPageNumbers($pageUrl, $listPages, $list) {
global $listPagesCount;
foreach (range(1, $listPages) as $listCount) {
//Check if current page
if($listCount == ($list - 1)) {
echo "<li class='current' style='margin:3px;'>";
echo "<span class='pages-nav-item'>" . $listCount . "</span>";
echo "</li>";
$listPagesCount++;
} else {
if (($list - 1) == 1) {
//null
} else {
//echo "<li class='the-prev-page' style='margin:3px;'>";
//echo "<a class='pages-nav-item' href=" . $pageUrl . ($listCount + 4) . " title=" . ($listCount + 4) . ">«</a>";
//echo "</li>";
}
echo "<li style='margin:3px;'>";
echo "<a class='pages-nav-item' href=" . $pageUrl . $listCount . " title=" . $listCount . ">" . $listCount . "</a>";
echo "</li>";
$listPagesCount++;
if($listPagesCount >= 3 && $listPagesCount == $list + 1) {
echo "<li class='the-next-page' style='margin:3px;'>";
echo "<a class='pages-nav-item' href=" . $pageUrl . $listCount . " title=" . $listCount . ">»</a>";
echo "</li>";
echo "<li class='extend' style='margin:3px;'>";
echo "<span class='pages-nav-item'>...</span>";
echo "</li>";
echo "<li class='last-page first-last-pages' style='margin:3px;'>";
echo "<a class='pages-nav-item' href=" . $pageUrl . $listPages . " title='Ultimo'>Ultimo</a>";
echo "</li>";
break;
}
}
}
}

I want to limit my search results in my live search, I am learning as I go

I am just trying to do a small project for my (very simple) website, my goal is to create an easily searchable list of links with this code but the list I have is very long (500+ entries in an xml) and I would like to limit the results to 5-10
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("links.xml");
$x=$xmlDoc->getElementsByTagName('link');
$q=$_GET["q"];
if (strlen($q)>0) {
$hint="";
for($i=0; $i<($x->length); $i++) {
$y=$x->item($i)->getElementsByTagName('title');
$z=$x->item($i)->getElementsByTagName('url');
if ($y->item(0)->nodeType==1) {
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint="<a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
} else {
$hint=$hint . "<br /><a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
}
}
}
}
if ($hint=="") {
$response="";
} else {
$response=$hint;
}
echo $response;
?>
'''
use 'while' and if it reached max results, it stops giving results.
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("links.xml");
$maxresult=10;
$x=$xmlDoc->getElementsByTagName('link');
$q=$_GET["q"];
if (strlen($q)>0) {
$hint="";
for($i=0; $i<($x->length); $i++) {
while($i < $maxresult) {
$y=$x->item($i)->getElementsByTagName('title');
$z=$x->item($i)->getElementsByTagName('url');
if ($y->item(0)->nodeType==1) {
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint="<a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
} else {
$hint=$hint . "<br /><a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
}
}
}
}
}
if ($hint=="") {
$response="";
} else {
$response=$hint;
}
echo $response;
?>

how i can return a value after end recursive function in php

I need after the end of a loop in a recursive function to return the $build variable
This is my code:
$traverse = function ($tree,$build = '') use (&$traverse) {
foreach ($tree as $key=>$menu) {
if (count($menu->children) > 0) {
$build .= "<li ><a href='" . $menu->url . "'>" . $menu->text . "</a><ul>";
$traverse( $menu->children,$build);
$build .= "</ul></li>";
} else {
$build .= "<li ><a href='" . $menu->url . "'>" . $menu->text . "</a></li>";
}
}
};
$traverse($tree );
Regarding my comment u should have:
$traverse = function ($tree) use (&$traverse) {
$build = '';
if (count($menu->children) > 0) {
$build .= "<li ><a href='" . $menu->url . "'>" . $menu->text . "</a><ul>";
$build .= $traverse($menu->children);
$build .= "</ul></li>";
} else {
$build .= "<li ><a href='" . $menu->url . "'>" . $menu->text . "</a></li>";
}
return $build;
};
As u can see u also don't need to pass and use $build as argument to the function.
Also u should check the html code for being valid at the end. Because of it won`t be.

a simple modification of a for loop

I have a simple php script that counts the number of pages in a manga script :
$omv_pager = "";
$omv_pager .= "<div class=\"well\">\n";
$omv_pager .= "<span>Manga <select class=\"form-control\" style='margin-bottom:10px;' name=\"manga\" onchange=\"change_manga(this.value)\">";
$omv_pager .= "<option class=\"form-control\" value=\"0\">Selecione Título do Manga...</option>";
for ($i = 0; $i < count($mangas); $i++) {
$m = $mangas[$i];
$omv_pager .= "<option value=\"" . omv_encode($m) . "\"" . (($m == $manga) ? " selected=\"selected\"" : "") . ">" . $m . "</option>";
}
$omv_pager .= "</select></span>\n";
if ($manga) {
if ($chapter) {
$omv_pager .= "<span>Chapter <select name=\"chapter\" onchange=\"change_chapter('$manga_escaped', this.value)\">";
for ($i = 0; $i < count($chapters); $i++) {
$cnumber = $chapters[$i]["number"];
$omv_pager .= "<option value=\"" . omv_encode($cnumber) . "\"" . (($cnumber == $chapter_number) ? " selected=\"selected\"" : "") . ">" . $cnumber . (isset($chapters[$i]["title"]) ? (" - " . $chapters[$i]["title"]) : "") . "</option>";
}
$omv_pager .= "</select></span>\n";
if ($page) {
$prevhtml = "";
if ($page <= 1) {
$prevhtml = "<img src=\"http://www.leitor.tk/themes/default/no-previous.png\" alt=\"\" />";
} else {
$prevhtml = "<img src=\"http://www.leitor.tk/themes/default/previous.png\" alt=\"Previous Page\" title=\"Previous Page\" />";
}
$nexthtml = "";
if ($page >= count($pages)) {
$nexthtml = "<img src=\"http://www.leitor.tk/themes/default/no-next.png\" alt=\"\" />";
} else {
$nexthtml = "<img src=\"http://www.leitor.tk/themes/default/next.png\" alt=\"Next Page\" title=\"Next Page\" />";
}
$omv_pager .= "<span>$prevhtml Page <select name=\"page\" onchange=\"change_page('$manga_escaped', '$chapter_number_escaped', this.value)\">";
for ($p = 1; $p <= count($pages); $p++) {
$omv_pager .= "<option value=\"" . $p . "\"" . (($p == $page) ? " selected=\"selected\"" : "") . ">" . $p . "</option>";
}
$omv_pager .= "</select> of " . count($pages) . " $nexthtml</span>\n";
}
}
}
$omv_pager .= "</div>\n";
echo $omv_pager;
What I want to do is, in the last page change the name of the to "end"
e.g : 1-2-3-4-5-6-7-8-9-10-11-12-end
And thanks to all of you guys.
Try with:
">" . ($p == count($pages) ? "end" : $p) . "</option>"
instead of
">" . $p . "</option>"
For optimization reasons you might want to buffer count($pages) into a variable before the for.

PHP: Undefined offset in for loop

While making a photo gallery I encountered a problem. With every photo I try to show how many comments it has, however if a photo has 0 comments it will give me an 'undefined offset' error. I have no idea what I am doing wrong because it does show that there are 0 comments.
This is the code of what is relevant to the problem:
(The problem occurres in the line: if($reacties[$i]==0){)
if((isset($_GET['vanafFoto'])) AND (intval($_GET['vanafFoto']>=0)) AND (intval($_GET['vanafFoto'] < $countFotos))){
$begin = intval($_GET['vanafFoto']);
if(($begin + $aantalFotos) <= $countFotos){
$eind = ($begin + $aantalFotos);
} // end if
else {
$eind = $countFotos;
} // end else
} // end if
else {
$begin = 0;
$eind = $aantalFotos;
} // end else
$countFotos = count($fotoArray);
// path naar echte foto
} // end else
echo "<table border='0' cellpadding='0' cellspacing='2'><tr><td ><b>" . $pathspatie . "</b> <small>(" . $count . ")</small>
<br><br><center><small>Pictures " . ($begin + 1) . " - " . $eind . "</small></center></td></tr></table>";
if(($begin - $aantalFotos) >= 0){
$navigation = "<a href='" . $_SERVER['PHP_SELF'] . "?page=album&boek=" . $originalPath . "&vanafFoto=" . ($begin - $aantalFotos) . "'><</a> " . $navigation;
} // end if
if(($begin + $aantalFotos) < $count){
$navigation .= " <a href='" . $_SERVER['PHP_SELF'] . "?page=album&boek=" . $originalPath . "&vanafFoto=" . ($begin + $aantalFotos) . "'>></a>";
} // end if
echo $navigation . "<br><br>";
echo "</td></tr><tr>";
$fotonr = 1;
for($i=$begin; $i < $eind; $i++){
$thumb = str_replace($path2, $thumbPath, $fotoArray[$i]);
echo "<td align='center'><a href='" . $_SERVER['PHP_SELF'] . "?page=album&boek=" . $originalPath . "&fotoID=" . $i . "'><img border='0' src='" . $thumb . "' height='100'><br>";
echo "<small>reacties (";
if($reacties[$i]==0){ // error occurres here.
echo "0";
} // end if
else {
echo $reacties[$i];
} // end else
echo ")</small>";
echo "</a></td>";
$fotonr++;
if($fotonr == ($clm + 1)){
echo "</tr>\n<tr>";
$fotonr = 1;
} // end if
} // end for
If anyone can see what the problem is it would be great!
I did not understand you exact goal but maybe it is better to write one more check:
if(!isset($reacties[$i]) || $reacties[$i]==0){
echo "0";
}

Categories