There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
php Variables Are Losing Their Value


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
HowdeeDoodee's Avatar
Senior Member with 217 posts.
 
Join Date: Aug 2004
Experience: Intermediate
06-Nov-2004, 07:26 PM #1
php Variables Are Losing Their Value
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's Avatar
Senior Member with 638 posts.
 
Join Date: Nov 2000
Location: MI
07-Nov-2004, 12:13 PM #2
Quote:
Originally Posted by HowdeeDoodee
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's Avatar
Senior Member with 590 posts.
 
Join Date: Jan 2004
07-Nov-2004, 02:35 PM #3
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's Avatar
Senior Member with 217 posts.
 
Join Date: Aug 2004
Experience: Intermediate
07-Nov-2004, 03:20 PM #4
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...d=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.

PHP Code:
$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 border=0 align=center><tr>");
    if(
$currentpage >= $max_pages_to_print) {
    print(
"<td><a href=\"$PHP_SELF?pagenr=1\">First</a>...</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><a href=\"$PHP_SELF?pagenr=$i\">$i</a> &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><a href=\"$PHP_SELF?pagenr=$i\">$i</a> &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>...<a href=\"$PHP_SELF?pagenr=$num_pages\">Last</a></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 align=center>
    <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><font color=#FFFFFF><b>Topic</b></font></td><td align=right><font color=#FFFFFF><b>Subtopic</b></font></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's Avatar
Senior Member with 638 posts.
 
Join Date: Nov 2000
Location: MI
08-Nov-2004, 08:16 AM #5
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?
__________________
www.ricksweb.info
www.2webheads.com
Quote:
Techies just think a little differently...at least that's what they keep telling me.
HowdeeDoodee's Avatar
Senior Member with 217 posts.
 
Join Date: Aug 2004
Experience: Intermediate
08-Nov-2004, 10:59 AM #6
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's Avatar
Senior Member with 638 posts.
 
Join Date: Nov 2000
Location: MI
08-Nov-2004, 11:09 AM #7
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><a href="' . $_SERVER['PHP_SELF'] . '?pagenr=' . $i . '&SeeAlso=' . $_GET['SeeAlso'] . '">' . $i . '</a> &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?
__________________
www.ricksweb.info
www.2webheads.com
Quote:
Techies just think a little differently...at least that's what they keep telling me.
HowdeeDoodee's Avatar
Senior Member with 217 posts.
 
Join Date: Aug 2004
Experience: Intermediate
08-Nov-2004, 01:18 PM #8
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's Avatar
Senior Member with 217 posts.
 
Join Date: Aug 2004
Experience: Intermediate
08-Nov-2004, 02:19 PM #9
EXCELLENT!!!!!

So far...

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


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:36 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.