I have an app that searches the database for active jobs and it returns them in a list of results. that part work fine, but the pagination is not working. when I click the link to go to the next 10 jobs it just stays in the same page.
here is what the code looks like.
$currPage = (($s/$limit) + 1);
// Links to access additional results
if ($s>=1) {
$prevs=($s-$limit);
$upd_msg_prev = '<< Prev 10 | ';
if ($s>$numrows-10) {
$upd_msg_next = 'Next 10 >>';
}
} else {
if ($numrows>10) {
$upd_msg_prev = '<< Prev 10 | ';
}
}
$here = isset($_POST['here']);
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// Check how many $pages are needed by getting total plus one if there's a remainder
if ($numrows%$limit) {
$pages++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
$news=($s+$limit);
$upd_msg_next = 'Next 10 >>';`
}
I think the problem is $here=isset...
That function will return true or false so it doesn't make sense using it in $upd_msg... further down in your code.
Related
Right now im trying to code read other codes and make it work, because some of the projects that i have are made by other developers and i really have to learn reading other code, I already made this one work but the problem is when i try to incorporate this pagination code to a $_GET variable an error occur, when it load the first page everything thing is smooth but when i click the other pages the $_GET variable dies. i already found a code in the site but i can't really get how to incorporate it with the other code.
here is the code that "MIGHT" solve the problem solving pagination $_GET
printf('Next',$targetpage,http_build_query(array('page' => 2) + $_GET));
here is the part of the code that make the link 1 2 3 4. . . and so on
$range = 3;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1?srId=$srdd'><<</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage?srId=$srdd'><</a> ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x?srId=$srdd'>$x</a> ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage?srId=$srdd'>></a> ";
// echo forward link for lastpage
//echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages?srId=$srdd'>>></a> ";
printf("<a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages?srId=$srdd'>>></a>");
} // end if
/****** end build pagination links ******/
?>
I already figured it out guys I saw lots for this problem on the web and also some on the forum here's how to solve it
<?php
session_start();
// store session data
if (isset($_GET["srId"]))
{
$_SESSION['variable']=$_GET["srId"];
$srdd = $_SESSION['variable'];
}
else
{
$srdd = $_SESSION['variable'];
$tableName = $srdd."r";
}
?>
thanks for your response guys.
You will need to use either session variables or cookies to pass them one page to another.
The $_GET global array will be a NEW array for every new page, assuming you're sending a GET request and it can be seen in the URL.
In general, the web is stateless which means it will not remember what you do from one page to another UNLESS you use cookies, sessions or a database for persistence.
Design your links
Page 1 | 2 | 3 | 4
to have the information in the link so they are ready for the next page
Page 1 = http://url?page=1&page_limit=25
Page 2 = http://url?page=2&page_limit=25
...
Etc
If the paginated links are in this format, these variables will be in the $_GET array.
Try replacing
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1?srId=$srdd'><<</a> ";
with
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1&srId=$srdd'><<</a> ";
After the initial '?' in a url, the following *'$_GET[]'* items should be separated from the preceeding one with an '&'. Note that your 'srId=$srdd' is preceeded by a '?'.
Example:
myurl.com?id=1234&page=help
Doing it this way will help you avoid having to store the information in the $_SESSION array, and keeps the URL secure.
I have a set of pages with the following names....
update1.php
update2.php
update3.php
update4.php
update5.php
update6.php
update7.php
At the moment, I have all 7 open on Chrome and refreshing every 30 minutes using the following code.
<body onload="setInterval('window.location.reload()', 1800000);">
I want to stick all these pages together so that I need only 1 page open. Is there a way to make a page refresh every 5 mins, but load a different section each time?
I was thinking of doing something like this, but it seems a bad way of doing it...
$refresh = $_GET['refresh'];
if (!isset($refresh)) { //update1 stuff; //reload page with &refresh=2 }
elseif ($refresh == 1) { //update1 stuff; //reload page with &refresh=2 }
elseif ($refresh == 2) { //update2 stuff; //reload page with &refresh=3 }
elseif etc.....
Any ideas?
In your php code that outputs the page html code, try something like this:
<?php echo ($_GET['refresh'] % 7) + 1; ?>.php // Name of next page
?refresh=<?php echo ($_GET['refresh'] % 7) + 1; ?>"; // Name of next page + 1
// All together
<body onload="setTimeout(function() { window.location.href = 'update<?php echo ($_GET['refresh'] % 7) + 1; ?>.php?refresh=<?php echo ($_GET['refresh'] % 7) + 1; ?>';}, 1800000);">
This will open the new page with an incremented number after the specified time interval. The modulo operation helps us run through the pages in a consistent loop.
References
window.location.href
setTimeout()
You can Put All Scripts in one page and can use
<meta http-equiv="refresh" content="1800" /> in head
to refersh browser every after 30 Minutes
Based on Hunter F's answer, the solution to my problem is almost complete.
Just a couple of tweaks needed.
I modified the code a little and submitted a new question here at: php array help required - if current array item = 'last or first item' then 'do something'
ORIGINAL MESSAGE:
I want to be able to create a simple navigation bar with PREV and NEXT links that I can use to cycle though a list of pages. The navigation bar will be a php include within all of the pages to be cycled.
So I guess the starting point is to create an array of the pages that need to be cycled though with the PREV NEXT links.
Such as....
$projectlist = array(
'http://domain.com/monkey/',
'http://domain.com/tiger/',
'http://domain.com/banana/',
'http://domain.com/parrot/',
'http://domain.com/aeroplane/',
);
I want to option to re-order, add or remove the links. So having one self contained master array such as this seems like a logical choice to me as I'll only need to update this one list for any future additions.
Each directory being linked to has it's own index.php file, so I've left the index.php part off from the end of the links as it's not needed...or is it?
...I'm pretty stumped as of how to continue from here.
I guess I need to work out which page within the array I'm currently on, then generate the PREV and NEXT links based on that. So If I entered from 'http://domain.com/parrot/' I would need links to the relevant PREV and NEXT pages.
Any help or information to guide me on this next stage would be most appreciated.
$currentPath = explode('?', $_SERVER['REQUEST_URI']); //make sure we don't count any GET variables!
$currentPath = $currentPath[0]; //grab just the path
$projectlist = array(
'/monkey/',
'/tiger/',
'/banana/',
'/parrot/',
'/aeroplane/',
);
if(! in_array($currentPath, $projectlist) ) {
die('Not a valid page!'); //they didn't access a page in our master list, handle error here
}
$currentPageIndex = array_search($currentPath, $projectlist);
if($currentPageIndex == 0) { //if it's on the first page, we want them to go to the last page
$prevlink = 'Prev';
} else { //otherwise just go to the n-1th page
$prevlink = 'Prev';
}
if($currentPageIndex == sizeof($projectlist)-1 ) { //if we're on the last page, have them go to the first page for "next"
$nextlink = 'Next';
} else {
$nextlink = 'Next';
}
One thing you may want to consider, is urlencoding the href targets in the link.
I'm looking for a PREV - NEXT page link generation script that will cycle alphabetically or numerically though all the .php pages within a directory.
I have a list of projects in a directory that I access from a menu page. Once the user chooses a project from the menu I'd really like to add a PREV and NEXT button to allow the user to cycle though the projects one at a time rather than having to go back to the menu page each time. Of course I could hand code the links but thought it would be a great job for a script to handle.
I really need it to be infinite as well, so when it reaches the last file in the directory the NEXT button is still active and will take you to the first item . ( From the first item the PREV button should take you to the last item )
I've been looking around for ages but so far found nothing that quite does the job.
The content of the pages is not coming from a database, but I've used some basic php includes for navigation blocks. I don't need anything other than simple NEXT and PREV controls either as there wont be a huge number of pages.
Any suggestions most appreciated.
Cheers.
If the ids are numeric, this is very easy. Assuming the URL for displaying a project is:
http://myserver/project.php?id=3
...you just need to do something like:
// Sanitise user input
$currentId = (int) $_GET['id'];
// Calculate next/prev IDs
$nextId = $currentId + 1;
$prevId = ($currentId > 1) ? $currentId - 1 : 1; // Make sure the previous ID is not below 1
// Make links
$nextLink = "Next";
$prevLink = "Previous";
If you are doing it based on the file names themselves, it's a little more tricky - but only a little. You would need to get a list of files in the directory, and use them instead. Now we'll assume your URL's look like:
http://myserver/project.php?name=somefile
<?php
// Sanitise user input
$currentFile = basename($_GET['name']);
// These will hold the previous/next project names
$prev = $next = '';
// Loop the directory until we find the current file
$dp = opendir('.'); // Open current dir, this can be adjusted to your file structure
while (($file = readdir($dp)) !== FALSE) {
if (in_array($file, array('.','..')) continue; // Skip . and ..
if ($file == $currentFile) {
if (($file = readdir($dp)) !== FALSE) {
$next = $file;
}
break;
}
$prev = $file;
}
closedir($dp);
// Make links
if ($next !== '') {
$nextLink = 'Next';
} else {
// Handle no next project here
}
if ($prev !== '') {
$prevLink = 'Previous';
} else {
// Handle no previous project here
}
I use the code below to do a "next" and "previous" navigation. It works but even when there is 1 entry the next button shows and when there are more than 20 entries and once they have been served the code shows next and previous. How can I make it so that the next button will only show if there are more than 10 and how do I show no next and previous button if there are no more results to show:
if (!isset($_GET['pg']) or !is_numeric($_GET['pg'])) {
$startrow = 0;
} else {
$startrow = (int)mysql_real_escape_string($_GET['pg']);
}
echo '<a id=pgnvg href=
"'.$_SERVER['PHP_SELF'].'?pg='.($startrow+20).'& q='.($what).'">Next</a>';
$prev = $startrow - 20;
//only print a "Previous" link if a "Next" was clicked
if ($prev >= 0)
echo '<a id=pgnvg2 href="'.$_SERVER['PHP_SELF'].'?pg='.$prev.'">Previous</a>';
you need to add a check on how many items are actually returned e.g:
if($itemAmount > 20) {
echo '<a id=pgnvg href=
"'.$_SERVER['PHP_SELF'].'?pg='.($startrow+20).'& q='.($what).'">Next</a>';
}
otherwise the next button will always display
Assume that you have a variable $itemsCount containing the number of items into the recordset.
If you don't have one you can calculate it with a query like
count(*) FROM <wathever table or join> WHERE <list of conditions>
Further I would use a constant called ITEMS_ON_A_PAGE (Hope you will guess its content ;)
define ('ITEMS_ON_A_PAGE',20); // put it where more appropriate
...
if (!isset($_GET['pg']) or !is_numeric($_GET['pg'])) {
$startrow = 0;
} else {
// no need to issue a mysql query, you need an integer from a numeric variable
$startrow = (int)$_GET['pg'];
}
$linkTemplate = '<a id="%s" href="?%s">%s</a>';
$nextPageFirstItem = $startrow + ITEMS_ON_A_PAGE;
$previousPageFirstItem = $startrow - ITEMS_ON_A_PAGE;
if ($itemsCount> $nextPageFirstItem) {
echo printf($linkTemplate,
'pgnvg',
http_build_query(array('pg'=>$nextPageFirstItem,'q'=>$what)),
'Next'
);
}
if ($previousPageFirstItem >= 0) {
echo printf($linkTemplate,
'pgnvg2',
http_build_query(array('pg'=>$previousPageFirstItem,'q'=>$what)),
'Previous'
);
}
Here there is the reference for the http_build_query that creates the query for you
There will be a lot of room to improve this code but, as a starting point, it would suffice ;)
You might actually want to use a switch statement. Sometimes if statements stop working when you have too many of them and too many evaluate to be true. Just pay attention to when you want to break; vs not breaking (don't include break;).