| | |
memorising requested variables after page reset
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 42
Reputation:
Solved Threads: 0
Ok... I have another problem. I have tryed to split the resultts of my query in to multiple pages with a fixed number of results per page... I used this tutorial for it:
http://www.php-mysql-tutorial.com/php-mysql-paging.php
Originally, my page worked as follows:
There was a search frame where the user would select different things through dropdown lists or checkboxes... hisselections were sent via a post method as parameters to the "display" page.
On the display page those parameters were obtained through the "$_request" or "$_post" methodes and asigned to local variables wich were then used as conditions in the query.
Now, AFTER I implemented the tutorial everything works perfectly... except one thing.
Well, let me start from the beginning:
In the new versione the parameters are still beying sent from the browse frame to the display frame via request or post and then used locally in the query string. the tutorial also implemented a limit parameter to the query determining the first page and the number of results per page.
The results are split acordingly and the corect number of pages is displayed. It also echoes next, previous and page number links for accessing the other results of the query.
well... these links look something like this:
this basicly tells the page to reload itself and send the new $page variable to the reloadedpage.
When the page reloads there's an if statement at the very top that verifies if the page has been reloaded and has resived a page number:
so after the page reloads it will use the new offset variable in the "limit" parameter of the query.
THE PROBLEM IS that when the page reloads all the requested variables from the browse frame are lost and the query will load everything in the database ( just as if there are no conditions set ). Even if I forciblly RE-execute the REQUEST lines to get the variables from the browse frame, the variables just return empty... even though the conditions are still set on the browse frame...
SO my question is... is there no way to send the requestev variables from the browse frame to the reloaded page like we sent the page number?
SOmething like this:
and then on the page reload check it would be like this?
Or if there's any other way to transmit multyple variables to a new page... Or maybe just send the WHOLE query string with all the parameters already included as it was executed when the search button was clicked... like this:
and at the page reload check it owuld be like this:
Please help...
http://www.php-mysql-tutorial.com/php-mysql-paging.php
Originally, my page worked as follows:
There was a search frame where the user would select different things through dropdown lists or checkboxes... hisselections were sent via a post method as parameters to the "display" page.
On the display page those parameters were obtained through the "$_request" or "$_post" methodes and asigned to local variables wich were then used as conditions in the query.
Now, AFTER I implemented the tutorial everything works perfectly... except one thing.
Well, let me start from the beginning:
In the new versione the parameters are still beying sent from the browse frame to the display frame via request or post and then used locally in the query string. the tutorial also implemented a limit parameter to the query determining the first page and the number of results per page.
The results are split acordingly and the corect number of pages is displayed. It also echoes next, previous and page number links for accessing the other results of the query.
well... these links look something like this:
PHP Syntax (Toggle Plain Text)
$page = $pageNum + 1; $next = " <a href=\"$self?page=$page\">[Next]</a> ";
this basicly tells the page to reload itself and send the new $page variable to the reloadedpage.
When the page reloads there's an if statement at the very top that verifies if the page has been reloaded and has resived a page number:
PHP Syntax (Toggle Plain Text)
if(isset($_GET['page'])) { $pageNum = $_GET['page']; } $offset = ($pageNum - 1) * $rowsPerPage;
so after the page reloads it will use the new offset variable in the "limit" parameter of the query.
THE PROBLEM IS that when the page reloads all the requested variables from the browse frame are lost and the query will load everything in the database ( just as if there are no conditions set ). Even if I forciblly RE-execute the REQUEST lines to get the variables from the browse frame, the variables just return empty... even though the conditions are still set on the browse frame...
SO my question is... is there no way to send the requestev variables from the browse frame to the reloaded page like we sent the page number?
SOmething like this:
PHP Syntax (Toggle Plain Text)
$next = " <a href=\"$self?page=$page?cond1=$cond1?cond2=$cond2...?condx=$condx\">[Next]</a> ";
and then on the page reload check it would be like this?
PHP Syntax (Toggle Plain Text)
if(isset($_GET['page'])) { $pageNum = $_GET['page']; $cond1=$_GET['cond1']; $cond2=$_GET['cond2']; ... $condx=$_GET['condx']; }
Or if there's any other way to transmit multyple variables to a new page... Or maybe just send the WHOLE query string with all the parameters already included as it was executed when the search button was clicked... like this:
PHP Syntax (Toggle Plain Text)
$next = " <a href=\"$self?page=$page?sqlstring=$sqlstring\">[Next]</a> ";
and at the page reload check it owuld be like this:
PHP Syntax (Toggle Plain Text)
if(isset($_GET['page'])) { $pageNum = $_GET['page']; $sqlstring=$_GET['sqlstring']; }
Please help...
Last edited by marcmm; Nov 19th, 2008 at 7:25 am.
Marcmm,
Normally the sql statement should be re-run every time with the new parameters. This is why it is good to use $_GET, instead of $_POST, because $_GET does not explicitly require a form and fields to be submitted.
What you actually need to do is always check the $_GET variables as if it is the first time you run the code.
If you want you may also check this thread
http://www.daniweb.com/forums/thread156953.html
Normally the sql statement should be re-run every time with the new parameters. This is why it is good to use $_GET, instead of $_POST, because $_GET does not explicitly require a form and fields to be submitted.
What you actually need to do is always check the $_GET variables as if it is the first time you run the code.
If you want you may also check this thread
http://www.daniweb.com/forums/thread156953.html
" Of all the things I've lost,
I miss my mind the most...."
Mark Twain
I miss my mind the most...."
Mark Twain
•
•
Join Date: Oct 2008
Posts: 42
Reputation:
Solved Threads: 0
•
•
•
•
Marcmm,
Normally the sql statement should be re-run every time with the new parameters. This is why it is good to use $_GET, instead of $_POST, because $_GET does not explicitly require a form and fields to be submitted.
What you actually need to do is always check the $_GET variables as if it is the first time you run the code.
If you want you may also check this thread
http://www.daniweb.com/forums/thread156953.html
PHP Syntax (Toggle Plain Text)
ECHO "<FORM NAME=hoteluri METHOD=post ACTION=display.php TARGET=result>"; ... code for several dropdown lists and checkboxes (and maybe even a textbox in the future); ... ECHO "</FRAME>";
should I use get method when submitting these results and loading the display frame?
Or were you refearing to the way I'm requesting these results from within the display frame:
PHP Syntax (Toggle Plain Text)
$combo_1 = $_REQUEST['combo_1 from browse frame']; $combo_2 = $_REQUEST['combo_2 from browse frame'];
the above is requesting the variable transmited by the dropdown lists ( and to my understanding $_request is like "post" and "get" combined anyways )
Or
PHP Syntax (Toggle Plain Text)
IF ( ISSET($_POST[checkboxname]) ) { $sql_query .= "AND ( condition )"; }
to check if the checkboxes on the browse frame have been checked.
And yes, the sql statement IS rerun everytime you click a next or previus page... but I loose all my parameters when I do so ( bolth the variables from the dropdown lists and the checkbox checks... for some reason, hen I reload the display frame, the page no longer seems to execute these lines of code... either it no longer recognises the browse frame... or it just skips all the checks... I jsut know that when the page is reloaded all the conditions are gone and the query loads everything from the database )... I need to be able to memorise those parameters in order to rebuild the exact same query when I click next or previous...
Yes, by usign get i refer to defining your search form as mehtod=get.
This is how normally get works. You can see the get request as all the symbols in the browser address which appear like this pagename.php?variable=smth&another=whatever
Using the get method can be simulated using links, which are not actual get request, but are equally valid for the server. In short <a href='pagename.php?variable=smth&another=whatever'>My link</a> is interpreted by the server absolutely the same way as e.g. when you press the submit button in your form where method=get.
I hope the above makes sense for you and you easily can figure out how to create your page number links. But, to make sure it is clear, here is an example for you.
1. Imagine you have 5 pages mentioned at the bottom of the page like numbers
1|2|3|4|5
each number should be wrapped by a link tag <a></a>
so the href attribute of your <a> for number one should look something like
In order to achieve that, you need to check the GET method on each load of your page as we have discussed earlier using if (isset($_GET...))
This time however, instead of only creating your sql statement using .='and ....', your if(isset... cycle should look something like this.
So, at the end you will have to echo this code for each page number
Hope it is clear now.
This is how normally get works. You can see the get request as all the symbols in the browser address which appear like this pagename.php?variable=smth&another=whatever
Using the get method can be simulated using links, which are not actual get request, but are equally valid for the server. In short <a href='pagename.php?variable=smth&another=whatever'>My link</a> is interpreted by the server absolutely the same way as e.g. when you press the submit button in your form where method=get.
I hope the above makes sense for you and you easily can figure out how to create your page number links. But, to make sure it is clear, here is an example for you.
1. Imagine you have 5 pages mentioned at the bottom of the page like numbers
1|2|3|4|5
each number should be wrapped by a link tag <a></a>
so the href attribute of your <a> for number one should look something like
PHP Syntax (Toggle Plain Text)
<a href='target_page.php?page=1¶m1=value1¶m2=&value2>1</a>
In order to achieve that, you need to check the GET method on each load of your page as we have discussed earlier using if (isset($_GET...))
This time however, instead of only creating your sql statement using .='and ....', your if(isset... cycle should look something like this.
PHP Syntax (Toggle Plain Text)
if(isset($_GET['param1'])) { $param1 = $_GET['param1']; $SQL .= 'AND whatever='.$param1; $pagelink .= '¶m1='$param1; }
PHP Syntax (Toggle Plain Text)
for($i=0;$i<$page_numbers;$i++) { echo '<a href="yourpage.php?page='.$i.$pagelink.'">'.$i.'</a>'; }
Hope it is clear now.
" Of all the things I've lost,
I miss my mind the most...."
Mark Twain
I miss my mind the most...."
Mark Twain
Rhyan answered this the same time as I did. His post pretty much says the same thing.
you need to send the searched string in the url with each pagination link (what you are doing is called pagination, just thought I would let you know). Do NOT send your query in the url. That is a HUGE security risk.
You can also use sessions, but in this case I think you'd be better off send the string in the url.
also, make sure you are using $_GET and not $_REQUEST as it is not very secure.
you need to send the searched string in the url with each pagination link (what you are doing is called pagination, just thought I would let you know). Do NOT send your query in the url. That is a HUGE security risk.
You can also use sessions, but in this case I think you'd be better off send the string in the url.
also, make sure you are using $_GET and not $_REQUEST as it is not very secure.
Last edited by kkeith29; Nov 19th, 2008 at 9:44 am.
•
•
Join Date: Oct 2008
Posts: 42
Reputation:
Solved Threads: 0
Ok... so I redefined my search button form and now it reads like this:
ok... now on to the if ifset statement...
right now it looks like this:
If I understand corectly... I have to have all the code to rebuild the query inside this if statement... ( or a, I even more confuse and this is another if ifset statement different from the one that apears at the very top of the page )
Or do I have one of these if isset statements for each variable I transmit via the next/previous/number links?
Or is this just one single if isset statement wich coveres all the necesary variables needed to build the query... if so how do you handle the fact that the number of parameters needed varies ( acording to what the user selects from the browse frame )
it's a bit confusing... sorry...
I'm guessing that my param 1 you are refearing to one of the variables needed to create the query... like for example one of the variables depicting the selection in a dropdown list on the browse frame.
Well if so... that means I have to copy paste the entire query building process inside this if statement... unfortunatelly that will be a lot of code since the code to build it contains multiple if statements reflecting the combinations betwen the different posible combinations of the drop down lists ( checking if they have anything but the default option selected ).
I also am having troulbe understanding this line:
what dose that do?
at present my display area for the first/last and page number links looks like this:
( I calculate the numrows variable by executing the query separatelly without the limit parameter to figure out how many lines it would have in total, then I use it to determine how many pages would contain the query results.
I'm affraid this get VS post debate is overpowering me it seems...
PHP Syntax (Toggle Plain Text)
ECHO "<FORM NAME=hoteluri METHOD=get ACTION=display.php TARGET=result>";
ok... now on to the if ifset statement...
right now it looks like this:
PHP Syntax (Toggle Plain Text)
if(isset($_GET['page'])) { $pageNum = $_GET['page']; } $offset = ($pageNum - 1) * $rowsPerPage;
If I understand corectly... I have to have all the code to rebuild the query inside this if statement... ( or a, I even more confuse and this is another if ifset statement different from the one that apears at the very top of the page )
Or do I have one of these if isset statements for each variable I transmit via the next/previous/number links?
Or is this just one single if isset statement wich coveres all the necesary variables needed to build the query... if so how do you handle the fact that the number of parameters needed varies ( acording to what the user selects from the browse frame )
it's a bit confusing... sorry...
PHP Syntax (Toggle Plain Text)
if(isset($_GET['param1'])) { $param1 = $_GET['param1']; $SQL .= 'AND whatever='.$param1; $pagelink .= '¶m1='$param1; }
I'm guessing that my param 1 you are refearing to one of the variables needed to create the query... like for example one of the variables depicting the selection in a dropdown list on the browse frame.
Well if so... that means I have to copy paste the entire query building process inside this if statement... unfortunatelly that will be a lot of code since the code to build it contains multiple if statements reflecting the combinations betwen the different posible combinations of the drop down lists ( checking if they have anything but the default option selected ).
I also am having troulbe understanding this line:
PHP Syntax (Toggle Plain Text)
$pagelink .= '¶m1='$param1;
what dose that do?
at present my display area for the first/last and page number links looks like this:
( I calculate the numrows variable by executing the query separatelly without the limit parameter to figure out how many lines it would have in total, then I use it to determine how many pages would contain the query results.
PHP Syntax (Toggle Plain Text)
$maxPage = ceil($numrows/$rowsPerPage); $self = $_SERVER['PHP_SELF']; $nav = ''; for($page = 1; $page <= $maxPage; $page++) { if ($page == $pageNum) { $nav .= " $page "; // no need to create a link to current page } else { $nav .= " <a style=' text-decoration: none;' href=\"$self?page=$page\">$page</a> "; } } if ($pageNum > 1) { $page = $pageNum - 1; $prev = " <a style=' text-decoration: none;' href=\"$self?page=$page\">[Prev]</a> "; $first = " <a style=' text-decoration: none;' href=\"$self?page=1\">[First Page]</a> "; } else { $prev = ' '; // we're on page one, don't print previous link $first = ' '; // nor the first page link } if ($pageNum < $maxPage) { $page = $pageNum + 1; $next = " <a href=\"$self?page=$page\">[Next]</a> "; $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> "; } else { $next = ' '; // we're on the last page, don't print next link $last = ' '; // nor the last page link }
I'm affraid this get VS post debate is overpowering me it seems...
•
•
Join Date: Oct 2008
Posts: 42
Reputation:
Solved Threads: 0
Ok, I figured out the logic behind your earlyer post... but one question still remains...
it's easy for the dropdown list parameters since they wiull always exist... having either 0 or a value different then 0 to transmit... so those parameters would be present for every link every time reguardless of the circumstances...
but what happenes with the checkboxes? if the user dosen't select a checkbox... that parameter shouldn't exist... what do I do then?
and about the parameter list to transmit... is it viable to just keep adding parameters to it via .= so that they would be like this:
is that a viable construction? just adding ?name=$name over and over ( with different parameters of course ) will be recognised as multyple parameters and then I check each of them in the if issetstatement with an independent if isset statement for each of them?
it's easy for the dropdown list parameters since they wiull always exist... having either 0 or a value different then 0 to transmit... so those parameters would be present for every link every time reguardless of the circumstances...
but what happenes with the checkboxes? if the user dosen't select a checkbox... that parameter shouldn't exist... what do I do then?
and about the parameter list to transmit... is it viable to just keep adding parameters to it via .= so that they would be like this:
PHP Syntax (Toggle Plain Text)
<a href='target_page.php?page=1¶m1=value1¶m2=&value2>1</a>
is that a viable construction? just adding ?name=$name over and over ( with different parameters of course ) will be recognised as multyple parameters and then I check each of them in the if issetstatement with an independent if isset statement for each of them?
Last edited by marcmm; Nov 19th, 2008 at 12:29 pm.
Well,
we have to start it all over again obvioursly
The things go like this.
In order to retrieve results from a search query, you have to do the following
1. Check what get or post variables have been submitted. In your case - only GET variables.
2. If a get value is set and is valid, you include it in your SELECT statement using .= and....
3. Send a request to the mysql and retrieve all records corresponding to defined select statement.
4. Display your results.
Now, because you have too many results to display you want to extend your capabilities by making multiple pages using pagination function
We should go like this
1. Check what get or post variables have been submitted. In your case - only GET variables.
2. If a get value is set and is valid, you include it in your SELECT statement using .= and....
a) Check If isset current page. If current page is not set default it to 0, otherwise current page value is equal to submitted get value. In both cases(either set or not set in the get method) include the LIMIT clause to the end of your SELECT statement.
b) During checking of your GET variables, apart from the select statement, create new string that will be used by the page numbering scheme only. Name it something like - $page_number_link_string. This string will carry the get values you want to store when you click on next page number.
3. Send a request to your sql server in order to retrieve the results. Note that the LIMIT option in your sql will not return the complete number of records that match the SELECT statement, but only a number of records <= limited record number. SO, in order to make the page numbering correctly, you have to run a hidden SELECT statement WITHOUT LIMIT parameter and store the value of total returned records in a variable - e.g. $total_records_in_db.
4. Display the limited number of records retrtieved.
5. Create the pagination below your displayed records in the following way.
a) Calculate page numbers related to total record number
a1. If total records / records to be displayed = any integer, total page numbers = total records / records to be displayed
a2. if total records / records to be displayed = float, total page numbers = (total records / records to be displayed)+1 - this will give you the extra page for those records that are less than the maximum records to be presented on a page.
b) display the page numbers you have calculated in the following way.
b1. for($i=0; $i< page numbers; $i++) echo your page number as a link, as i have explained in my previous post.
And that's it.
I hope you get the logic. I cannot do more for you but create the whole page for you, but you'll never learn like that....
we have to start it all over again obvioursly
The things go like this.
In order to retrieve results from a search query, you have to do the following
1. Check what get or post variables have been submitted. In your case - only GET variables.
2. If a get value is set and is valid, you include it in your SELECT statement using .= and....
3. Send a request to the mysql and retrieve all records corresponding to defined select statement.
4. Display your results.
Now, because you have too many results to display you want to extend your capabilities by making multiple pages using pagination function
We should go like this
1. Check what get or post variables have been submitted. In your case - only GET variables.
2. If a get value is set and is valid, you include it in your SELECT statement using .= and....
a) Check If isset current page. If current page is not set default it to 0, otherwise current page value is equal to submitted get value. In both cases(either set or not set in the get method) include the LIMIT clause to the end of your SELECT statement.
b) During checking of your GET variables, apart from the select statement, create new string that will be used by the page numbering scheme only. Name it something like - $page_number_link_string. This string will carry the get values you want to store when you click on next page number.
3. Send a request to your sql server in order to retrieve the results. Note that the LIMIT option in your sql will not return the complete number of records that match the SELECT statement, but only a number of records <= limited record number. SO, in order to make the page numbering correctly, you have to run a hidden SELECT statement WITHOUT LIMIT parameter and store the value of total returned records in a variable - e.g. $total_records_in_db.
4. Display the limited number of records retrtieved.
5. Create the pagination below your displayed records in the following way.
a) Calculate page numbers related to total record number
a1. If total records / records to be displayed = any integer, total page numbers = total records / records to be displayed
a2. if total records / records to be displayed = float, total page numbers = (total records / records to be displayed)+1 - this will give you the extra page for those records that are less than the maximum records to be presented on a page.
b) display the page numbers you have calculated in the following way.
b1. for($i=0; $i< page numbers; $i++) echo your page number as a link, as i have explained in my previous post.
And that's it.
I hope you get the logic. I cannot do more for you but create the whole page for you, but you'll never learn like that....
" Of all the things I've lost,
I miss my mind the most...."
Mark Twain
I miss my mind the most...."
Mark Twain
•
•
•
•
Ok, I figured out the logic behind your earlyer post... but one question still remains...
it's easy for the dropdown list parameters since they wiull always exist... having either 0 or a value different then 0 to transmit... so those parameters would be present for every link every time reguardless of the circumstances...
but what happenes with the checkboxes? if the user dosen't select a checkbox... that parameter shouldn't exist... what do I do then?
and about the parameter list to transmit... is it viable to just keep adding parameters to it via .= so that they would be like this:
PHP Syntax (Toggle Plain Text)
<a href='target_page.php?page=1¶m1=value1¶m2=&value2>1</a>
is that a viable construction? just adding ?name=$name over and over ( with different parameters of course ) will be recognised as multyple parameters and then I check each of them in the if issetstatement with an independent if isset statement for each of them?
What concerns the checkboxes - I did not get your question. But i think that you want to ask me if the user changes some or all of the parameters on the search form, what will happen then with the links and with the results. If this is your question then, note the following.
I intentionally mentioned that the a href=?param... is a simulated get request. This link represents a dummy get request, but it is simulated. You will get the very same results by just changing the numbers in your browser's address field and by hitting enter
However, if you fill the form and hit the submit button, then new values will be provided via the get method. In this way your page numbers will automatically reflect the new changed parameters and your link will be different.
I hope I said this clear, because even I got confused
" Of all the things I've lost,
I miss my mind the most...."
Mark Twain
I miss my mind the most...."
Mark Twain
•
•
Join Date: Oct 2008
Posts: 42
Reputation:
Solved Threads: 0
Ok... after reading the above I realised how unnecesarelly I complicated myself with the code... SO I deleted most of it and started from scratch this is how the beginning of the document looks like right now ( this is before starting to implement the split paging )
this format ctually simplifyed my coding on the display page considerably... so thanks for the hint... basicly now... each time the page loads it will suposedly check to see weather any of those parameters have been sent to the page... by default they are sent by the browse frame... and next they will be sent by the page links... am I corect?
PHP Syntax (Toggle Plain Text)
$sql_line="SELECT DISTINCT tables.names FROM tables WHERE ((standard cond1) AND (standard cond2)"; $sql_end=") ORDER BY something ASC"; IF ( ISSET($_GET[combo_1]) ) { $combo_1 = $_GET['combo_1']; IF ( $combo_1 <> 0 ) // check to see if user has selected something other then the default { $sql_line .= "AND (cond1)"; } } IF ( ISSET($_GET[combo_2]) ) { $combo_2 = $_GET['combo_2']; IF ( $combo_2 <> 0 ) // check to see if user has selected something other then the default { $sql_line .= "AND (cond2)"; } } IF ( ISSET($_GET[checkbox1]) ) { $sql_line .= "AND ( cond3 )"; } IF ( ISSET($_GET[checkbox2]) ) { $sql_line .= "AND ( cond4 )"; } IF ( ISSET($_GET[checkbox3]) ) { $sql_line .= "AND ( cond5 )"; } $sql_line .= $sql_end; // I atatch the end of the sql line
this format ctually simplifyed my coding on the display page considerably... so thanks for the hint... basicly now... each time the page loads it will suposedly check to see weather any of those parameters have been sent to the page... by default they are sent by the browse frame... and next they will be sent by the page links... am I corect?
![]() |
Other Threads in the PHP Forum
- Previous Thread: Need Advice on Concepts.
- Next Thread: language code not showing in source view. Really weird!
Views: 1677 | Replies: 19
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date directory display download dynamic echo email error execution file files folder form forms function functions google href htaccess html htmlspecialchars image include insert integration ip java javascript joomla jquery limit link links login loop mail menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote replace script search select server session sessions sms soap source space speed sql structure syntax system table tutorial update updates upload url validation validator variable video web xml youtube






