Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to have a global varialbe in PHP?

Status
Not open for further replies.

raulgh

Programmer
Aug 20, 2003
20
SG
Dear all:

I am just wondering is there a way to have a global varialbe in a PHP page?

I have 2 php pages which are the two frames of a webpage. The page on the left have several buttons which will generate different msgs and pass to the page on the right.

The right page will generate different SQL stmts according to the msg it received from the left page and display the search results.

There is also a text box and a button called "Refresh" on the right page. So that when users type a number, say "5", in the text box and click the "Refresh" button, the previously displayed results will be reduced to the number of rows according to the number in the text box.

Here is some snippets of my code:

[
//This is to display search results

if ($Msg == "NewUsr"){//Msg is received from left page
$SQLStmt = "Select datetime, nick, mobile from users order by datetime desc";
DisplayResults($SQLStmt);}

if ($Msg == "AllUsr") {
$SQLStmt = "Select nick, mobile, datetime from users order by nick asc";
DisplayResults($SQLStmt); }

//This means to refresh the results, the text box for usr
//to input number is named as "txtNumber".
//The code is:
if($txtNumber != ""){
$SQLStmt.= " limit 0, ".$txtNumber;
DisplayResults($SQLStmt); }
]

I intend to append clause "limit 0, '$txtNumber' " to the end of the SQL Stmt which is lastly executed. However, that SQL Stmt seems don't remain after executing. So I really want a some kind of global variable which can hold the value of that SQLStmt.

I tried defining the variable SQLStmt at the beginning of PHP script. I also tried defining a variable named 'G_SQLStmt' at the beginning and passed the value of SQLStmt to it. Here is how I do it:
[
if ($Msg == "RecentMsg"){
$SQLStmt = "Select sender, mobile, datetime, messages from mo_messages order by datetime desc";
$G_SQLStmt = "Select sender, mobile, datetime, messages from mo_messages order by datetime desc";
DisplayResults($SQLStmt);}
]
However, neither of them work. Can someone help me on this?

BTW, the 'txtNumber' text box and 'Refresh' button are in a form called 'frmRefresh' on the right page. So the value of 'txtNumber' is passed to the right page itself and process accordingly.
 
there are two ways:
1. Use session.
2. Use hidden fields. (Recommended for this situation)

hold the original sql in a hidden field. when refresh is clicked read the value of the hidden field.

note: if u r using limit then the sql must noit have the limit part(u may want to change it)...


Known is handfull, Unknown is worldfull
 
Hi vbkris:
I used hidden value, it doesn't work either. Here is the code:
//The centerFrame is where this form is defined

<form action = &quot;CenterFrame.php?SQLStmt=$g_SQLStmt&quot; method =&quot;get&quot; name = frmRefresh>
<TABLE width =&quot;30%&quot; align =&quot;right&quot; cellspacing = &quot;10&quot;>
<TR><TD width =&quot;75%&quot;>Show: <input type = &quot;text&quot; name=&quot;txtNumber&quot; size =&quot;5&quot; ></TD>
<TD><input type = &quot;submit&quot; name=&quot;cmdRefresh&quot; value=&quot;Refresh&quot;/></TD></TR></TABLE>
<input type = &quot;hidden&quot; name = &quot;sql&quot; />
//here is hidden value
</form>
<BR><BR><HR>
<?if ($Msg == &quot;RecentMsg&quot;){
$SQLStmt = &quot;Select sender, mobile, datetime, messages from mo_messages order by datetime desc &quot;;
$sql = $SQLStmt; //assign value to hidden value
displayResults($SQLStmt);}

if($txtNumber != &quot;&quot;){ //this function will run when refresh
$sql .= &quot; limit 0, &quot;.$txtNumber; //append $sql
$SQLStmt =$sql;
DisplayResults($SQLStmt);}

However, above code doesn't work because $sql is blank
 
<input type = &quot;hidden&quot; name = &quot;sql&quot; value=&quot;<?=$SQL_HERE?>&quot; />


Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top