I have been adding pagination to a live search. currently the call go through jquery that call search.php and display the data in a id in the homepage. The pagination is showing up but it only stays on one page. I cannot get it to display data on page 2 or 3 etc. Below if I manually change the variable $start by putting a number it would change to the page. Do you know how to correct it?
between the sql to get the count and the while loop I have the following:
$per_page = 1;
$start = isset($_POST['start']) ? $_POST['start']: '';
$max_pages = ceil($count[0]/$per_page);
if (!$start)
$start = 0;
After closing the while loop I have the pagination data:
echo "<center>";
$prev = $start - $per_page;
$next = $start + $per_page;
if (!($start <= 0))
echo "<a href='search.php?search=$key&submit=search&start=$prev'> Prev </a>";
$i=1;
for ($x=0; $x<$count; $x=$x +$per_page) {
if ($start != $x)
echo "<a href='search.php?search=$key&submit=search&start=$x'> $i </a>";
else
echo "<a href='search.php?search=$key&submit=search&start=$x'><b> $i </b></a>";
$i++;
}
if (!($start >= $count - $per_page ))
echo "<a href='search.php?search=$key&submit=search&start=$next'> Next </a>";
echo "</center>";
I had a live search index page that call a script(jquery) that call a php. My pagination code was not going to the next page. What I did was put a hidden input to get the value from $_POST['start']; on the index page. I using already using a script (JQUERY) to put the value in the text area from the url for the text input and retrieve it so I did the same for the page increment. I get the value from the url put it into the hidden input and retrieve it with my script (JQUERY). You see me using $_GET and $_POST. I had a difficult time figuring it out. Hope this help.
Related
I am trying to make anchor links for different sections in the page.
#section1
#section2
But, the url isn't being updated each time I click the href link
if(!isset($_SESSION['counter'])) {
$_SESSION['counter'] = 1;
}
<?php echo ''.'Next Section'.'';?>
When I echo the counter I can see it being updated upon refresh but I want to be able update the anchor section without refreshing.
Thank you
You need to actually increment the value in the session and not just what is currently stored in the session.
// If there is a value in the session, then increment it.
if(isset($_SESSION['counter'])) {
$_SESSION['counter'] = $_SESSION['counter'] + 1;
} else {
$_SESSION['counter'] = 1;
}
// Now, use the value which you have set.
<?php echo ''.'Next Section'.'';?>
I've got the data from nusoap webservice and put it in the table. Here is the code function:
`$filter='';
$order='';
$limit='10';
$offset='';
$result=$proxy->GetRecordset($token,$table,$filter,$order,$limit,$offset);
`
After parsing the json, here is the result:
table with total data
Now the problem is I don't know how to split the data into pagination. The total data is 251 data (country ID with name). I change offset to 10 manually from the code and it give the result from 11-20.
I stuck with this code (don't know what to do):
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
$total_page=ceil($total_data/$limit);
$self=$_SERVER['PHP_SELF'];
echo "<ul class='pagination'>";
for ($i=1;$i<=$total_page;$i++){
echo "<li>$i</li>";
}
echo "</ul>";
Here is the result:
Table with link pagination
But when I click page number 2,3,...etc it still the same data. How I should do when I click the page number e.g 2 it will refresh the page and change the offset to 10 and so on. Please help. Thanks.
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.
Hello i got variable $offset. When user is viewing page - $offset should be equal to 0;
When he is clicking on link
Next
It should update the $offset by adding to it next's value.
So i wrote like this
$offset = $offset + $_GET['next'];
When i'm clicking on link first time it works, but the future clicks doesnt, because he dont remember $offset value. How should i write to do it right?
I would deplore using a session to store this. It is a waste of server resources.
$nextLot=3;
$offset=0;
if(!empty($_GET['offset']))
{
$offset=$_GET['offset'];
}
$offset+=$_GET['next'];
Next
This method saves some resources and simply checks the URL for all the info that is needed.
You can pass the $offset variable via $_SESSION.
So your code will look something like this:
$_SESSION['offset'] = $_SESSION['offset'] + $_GET['next'];
You can do this by using SESSION
<?php if(isset($_SESSION['next']) : ?>
Next
<?php else : ?>
Next
<?php endif; ?>
and for offset
$_SESSION['offset'] = isset($_SESSION['offset']) + $_GET['next'];
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;).