Pass $_GET variable to the next page - php

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.

Related

Autoincrement url for anchor link

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'.'';?>

How do I create pagination with live search using php and jquery?

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'>&nbspPrev&nbsp</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'>&nbsp$i&nbsp</a>";
else
echo "<a href='search.php?search=$key&submit=search&start=$x'><b>&nbsp$i&nbsp</b></a>";
$i++;
}
if (!($start >= $count - $per_page ))
echo "<a href='search.php?search=$key&submit=search&start=$next'>&nbspNext&nbsp</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.

Need help conditionally including php in page

I've been trying to conditionally include a banner which incorporates a form into a php site. Previously the banner was part of the header but I need to exclude it from certain pages in order for other elements to be included.
I'm using $_SERVER['SCRIPT_NAME'] to define a variable on the page and then check for this. I'm a little confused though as to where the variable should be placed. Will it work if placed in the header or does it need to be included in every separate page?
This is what I've got:
<?php $currentPage = $_SERVER['SCRIPT_NAME']; ?>
<?php if ($currentPage != "/ecoProcess.php") {
include ("includes/banner.php");
} else {
echo "ECO";
}
?>
For further info, I've updated the code because of my schoolboy error with -= instead of != but neither of the conditions works now (no banner, no "ECO"). The pages are being called using this method:
<?php if($_GET['action']=='boiler_installation_replacement') {?> class="active"<? php } ?>>Boiler Installation and Replacement
I've been advised to try var_dump which reveals this:
' string(14) "/ths/index.php" ' everytime. I guess this is due to the way the pages are called but I don't know how to get around this.
-= means subtract right value from left side. Eg.
$a = 5;
$a -= 3;
echo $a; // will print 2 because 5 - 3 = 2 :)
Change it to comparison == or better ===. You can also use !=/ !== for opposite cases.
Change your condition like this.
<?php
$currentPage = $_SERVER['SCRIPT_NAME'];
if ($currentPage == "/ecoProcess.php") {
include ("includes/banner.php");
} else {
echo "ECO";
}
?>
Try this:
just change your if statement because it is not logically correct.
Use this
if ($currentPage == "/ecoProcess.php")
instead of
if ($currentPage -= "/ecoProcess.php")
Thanks

Custom PHP Pagination

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;).

php pagination using class

hello everyone plz help me out with this script.
I am calling my pages in this way in main page
if(isset($_GET['page'])=='view_user' && $_GET['page']=='view_user')include("user/crud_user.php");
i have user class in which i am doing pagination
$self = $_SERVER['PHP_SELF'];
if($this->openPage<=0) {
$next = 2;
}
else {
$next = $this->openPage+1;
}
$prev = $this->openPage-1;
$last = $this->pages;
if($this->openPage > 1) {
echo "<a href=$self?page=1>First</a>&nbsp ";
echo "<a href=$self?page=$prev>Prev</a>&nbsp ";
}
The url of the page which is using this class is lie this
admin.php?page=view_user
Now i can get pagination no according to the records but when i click on no it rather take me to admin.php?page=3 because of self . I want to stick to that page for showing the result . I tried in this way also $self="admin.php?page=view_user";but no luck . please help me out
To use one variable for serving two different purposes is quite unusual idea.
Let me suggest you to use 2 different variables, one to designate included page and another to point to the actual page in pagination.
Hope this helps.
do
if(isset($_GET['page']) && $_GET['page'] == 'view_user')
{
include("user/crud_user.php");
exit;
}
because of many reasons.
for pagination try the following: http://www.catchmyfame.com/2007/07/28/finally-the-simple-pagination-class/

Categories