# php Variables Are Losing Their Value



## HowdeeDoodee (Aug 26, 2004)

I have php variables with values obtained from post and from url's. These variables are losing their value after what I am assuming is one passthrough of the script from top to bottom. The variables are stored in the script but after one pass of the program the variables lose their value. What is this about and how do I stop this from happening? I am a novice. Please educate.


----------



## deuce868 (Nov 2, 2000)

HowdeeDoodee said:


> I have php variables with values obtained from post and from url's. These variables are losing their value after what I am assuming is one passthrough of the script from top to bottom. The variables are stored in the script but after one pass of the program the variables lose their value. What is this about and how do I stop this from happening? I am a novice. Please educate.


Do you have any code you can share? I'm not following 100%. Your $_POST and $_GET variables are losing values through the processing page?


----------



## tdi_veedub (Jan 29, 2004)

If I follow correctly, you are losing your information once the script you are running ends.

Unless you store the information somewhere (like a database) you will lose all information stored in variables once the end of file is reached ...


----------



## HowdeeDoodee (Aug 26, 2004)

Thank you for the replies. The data that is being lost is the data the user would submit to retrieve records from the database.

The source of the code can be found at the following link. The developer's email in the code is no longer valid.

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=787&lngWId=8

Global variables are set to "on" at the server.
I tried using serialize and unserialize with no effect.

See working sample page at the link below of php code that works properly when a variable is assigned a value within the php script and then passed on to the query string. Only 14 items should show up as they do in this example as containing the letter and number combination in Subtopics equal to 4a. The query string is set to only retrieve records from the MySql database where Subtopics contains 4a without the quotes. If you click on page 2 you only see 4 records. This page works properly.

http://www.findthepower.com/M2.php

At the link below you will see how the php code does not work properly when a variable is a assigned a value by having the value passed to the query string from a url. The same issue occurs whether one uses input boxes or drop down boxes either in a separate form calling the php page or whether the input boxes or drop down boxes are on the same php page as the code. The same issue occurs whether you use POST or whether you pick up the search variable from a url.

See example page at the link below of a php results page where the code DOES NOT work properly. Click on page 2 and see more items show up than should show up. Only 14 items should show up as containing the letter and number combination in Subtopics equal to 4a. On page one there are 10 items showing. However, instead of 4 items showing on page 2 you see more than four. When you click on page two then you see page three appear at the top of page two which should not be happening. The query string is set to only retrieve records from the MySql database where Subtopics contains 4a without the quotes. An echo statement shows that the values for the query are held for the page one cycle but the query values are deleted for the page 2 and higher pages. Thus, with the query values deleted for the page 2 and higher pages, the query pulls up all records from the database instead of just the records desired in the SeeAlso request from the url.

http://www.findthepower.com/M1.php?SeeAlso=4a

Here is the code I am using for the above (MI.php) that is not working properly.


```
$input="$SeeAlso";
$inputnew = $input;

$query = "SELECT * FROM `View2` WHERE `Subtopic` LIKE '%$inputnew%'";

    $result = mysql_query($query) or die("ERROR");
    $num_record = mysql_num_rows($result);
    if($num_record > $display) { // Only show 1,2,3,etc. when there are more records found that fit on 1 page
    // when the page is loaded first...
    if(empty($pagenr)) { 
    $pagenr = 1;
    }
    // some variables
    $display = 10; // number of records to display per page
    $max_pages_to_print = 7; // number of pages to show, if you change this you also have to change the variable 'middlenumber', for example:increase this one with two, increase middlenumber with one
    $startrecord = $pagenr * $display; // first record to show from the queryresult
    $num_pages = intval($num_record / $display) + 1; // total number of pages
    $loopcounter = 0; // counter for whileloop
    $currentpage = $pagenr; // Page where we are at the moment
    $middlenumber = 3; // Number will be decreased from variable currentpage in order to get the currentpage always in the middle
    $colourcounter = 0; // Variable to change the background-color of the [TD]
    $i = 1; // variable that will print 1,2,3,etc..
    $x = 0; // variable i use to put always the current, marked page in the middle
    // actual stuff starts here
    print("[TABLE][TR]");
    if($currentpage >= $max_pages_to_print) {
    print("[TD][URL]First[/URL]...[/TD]");
    }
    //BEGIN LOOP
    while($loopcounter < $max_pages_to_print) { 
    if($currentpage >= $max_pages_to_print) { // If user clicks om page higher than $max_pages_to_print
    // Mark current page
    if($currentpage == $i) {
    print("[TD]$i &nbsp[/TD]"); // print pagenumbers
    $i += 1; //increase pagenr
    $loopcounter += 1; // increase loopcounter
    }
    // End marking
    if($i > $num_pages) { // if last page has been printed, exit loop
    break;
    }
    if($x == 0) {
    $i = $currentpage - $middlenumber; // current page will always be printed in the middle
    }
    print("[TD][URL]$i[/URL] &nbsp[/TD]"); // print pagenumbers
    $x = $x + 1;
    $i += 1; //increase pagenr
    $loopcounter += 1; // increase loopcounter
    }
    else { // Else user clicks on a pagenumber lower $max_pages_to_print
    // Mark current page
    if($currentpage == $i) {
    print("[TD]$i &nbsp[/TD]"); // print pagenumbers
    $i += 1; //increase pagenr
    $loopcounter += 1; // increase loopcounter
    }
    // End marking
    if($i > $num_pages) { // if less than $max_mages_to_print, exit loop
    break;
    }
    print("[TD][URL]$i[/URL] &nbsp[/TD]"); // print pagenumbers
    $i += 1; //increase pagenr
    $loopcounter += 1; // increase loopcounter
    } // End if
    } 
    // END LOOP
    if(($num_pages > $max_pages_to_print AND // notice the user that there are more pages
    	$i <= $num_pages)) {
    print("[TD]...[URL]Last[/URL][/TD]");
    }
    print("[/TR][/TABLE]");
    $startrecord = $startrecord - $display; // Set startrecord to the right position
    // Some calculation for the lastrecord
    if($currentpage == $num_pages) { // Last page...
    $lastrecord = $num_record; // so $lastrecord = $num_record
    }
    else {
    $lastrecord = ($currentpage * $display);
    }
    } // End of the first if-statement
    // Some info
    print("[TABLE]
    [TR][TD]You are now on page $currentpage[/TD][/TR]
    [TR][TD]There are $num_pages pages in total[/TD][/TR]
    [TR][TD]$num_record records are spread over $num_pages pages[/TD][/TR]
    [TR][TD]Current display : $startrecord - $lastrecord[/TD][/TR]
    [/TABLE]");
    // End info
    // actual query, watch the end($startrecord, $display)

$query2 = "SELECT * FROM `View2` WHERE `Subtopic` LIKE '%$inputnew%' LIMIT $startrecord, $display";

$result2 = mysql_query($query2) or die("ERROR");

    // print results on screen

print("<table border=0 align=center width=300 bgcolor=#0E711D cellpadding=\"2\" cellspacing=\"1\">[TR][TD][B]Topic[/B][/TD][TD][B]Subtopic[/B][/TD][/TR]");
    $Field1="Topic";
	$Field2="Subtopic";
    //Use only $Field references in the statements below 
	while(list($Field1, $Field2) = mysql_fetch_row($result2)) {
    if ($colorcounter == 0) {
    $colorbg = "#ECE28B";
    }
    else {
    $colorbg = "#F4EFC1";
    $colorcounter = $colorcounter - 2;
    }
    print("[TR]<td bgcolor=$colorbg>$Field1[/TD]<td align=right bgcolor=$colorbg>$Field2[/TD][/TR]");
    $colorcounter = $colorcounter + 1;
    }
    print("[/TABLE]");
echo "See Also At Bottom of Code";
echo $SeeAlso

?>
```
Thank you again for the replies.


----------



## deuce868 (Nov 2, 2000)

The thing is that you aren't limiting your query anywhere. If you only want to see topics with 4a in them you need to specify that somewhere and it needs to be accessible to every single page. In the code you have shown here you never get that. Your query is set to :
$input="$SeeAlso";
$inputnew = $input;

Now somewhere the variable $SeeAlso needs to be set to 4a, but it's not. 
Change the link for the pages to:
http://www.findthepower.com/M1.php?pagenr=1&SeeAlso=4a

And you will only get the 4a ones. Which means you need to pass the 4a in the url for each page you are going to link to. So the link to page 2 would be:
http://www.findthepower.com/M1.php?pagenr=2&SeeAlso=4a

Does this make sense?


----------



## HowdeeDoodee (Aug 26, 2004)

Thank you deuce898 for the reply.

Well, it does may sense to a point. Your suggestion certainly works. My problem is how do I set up the number of pages in advance without doing a search in advance. In the case of users filling in a text box, there are hundreds of thousands of combinations of inputs, maybe more. 

In the example page that is not working, the user would typically click on a link with the url including ?SeeAlso=4a

Since I am a novice, I was thinking the search parameter of 4a is supposed to be passed from the url to the php page because the php page is called in the url.

The code developed by the originator (not me) does do a page count which appears on page one. How do I use that page count to set up the parameters you suggest? 

As I am asking this question I am thinking my answer probably lies in the $PHP_SELF? statement in the code.

I could not find any simple code like this on the web which is why I started using it.

Any suggestions would be appreciated. 

Thank you again for the reply.


----------



## deuce868 (Nov 2, 2000)

Ok, to do what you are wanting to do you need another step in the process. On page one you need to add a seach box in a form. The form would call this page you are using. The textbox name should match the same variable you are using to filter the results here. So in your case your search box should be called name="SeeAlso"

The other thing to check is that the form needs to have the method set to be GET so that the filter string is placed in the url to be picked out by your results page.

On the results page it should filter based on the url string now that it is there from the search box. The trick is to get the page 2 & 3 & etc...etc to work right. To fix those you need to add the variable $SeeAlso to the url you click on to go to page 2 & 3 etc.

So the code would look like this:
print '[TD] . '?pagenr=' . $i . '&SeeAlso=' . $_GET['SeeAlso'] . ']' . $i . ' &nbsp[/TD]' ; // print pagenumbers

Something like that. I hate register globals so I changed the code to get the variables form the proper places.

In this way each link to the subsequent pages will also carry the filter so that page 2 will filter based on the seach terms from the start.

How about that? Getting closer?


----------



## HowdeeDoodee (Aug 26, 2004)

Thank you for the reply. I will play around with this and get back to you. It does look good. 

Not in the code above is a branching sequence I built to take input from two different search pages. One search page has three drop down boxes and the other search page has three input boxes. I was using the _POST method to transfer data to the called php page. I think we have id'ed the problem, that being the PHP self code.

If I cannot take input from separate forms and branch the input from these pages, I may be able to have two php pages. One php page could have the text boxes and the other php page could have the drop downs.

It may be a day before I get back on this but I will definitely give this a shot. 

Thank you again.


----------



## HowdeeDoodee (Aug 26, 2004)

*EXCELLENT!!!!!*

So far...

Thank you so very much. Now I shall tinker around some more.


----------

