Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

How to have global variable in PHP script?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

How to have global variable in PHP script?

Postby raulgh5 » Sun Aug 24, 2003 8:07 pm

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.
raulgh5
New php-forum User
New php-forum User
 
Posts: 3
Joined: Sun Aug 24, 2003 8:04 pm

Postby Joel » Sun Aug 24, 2003 8:32 pm

What is the error you are getting?

You could be passing the variables to the page using GET or POST. Like how you have you're form, when the user clicks refresh, when they have 5 in the input box. You could be using a GET method of form sending, sending it to the page which picks it up.

so you will have a URL like page.php?limit=5

Then on the page with the SQL query you can have some code like this.

Code: Select all
if (isset($_GET['limit'])) {
   $limit = $_GET['limit'];
   settype($limit, 'int'); //Leaves only integer values
}
else {
   $limit = 300; //Just a large value
}

$sql = "SELECT * FROM table ORDER BY field1 LIMIT 0, ".$limit;
Joel
New php-forum User
New php-forum User
 
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand

Global variable

Postby ruturajv » Sun Aug 24, 2003 9:09 pm

I don't know why you require a global variable, but if the variable is for a particular user, you can use sessions and then set a particualar varible which you want it globally!
:idea:
User avatar
ruturajv
php-forum Super User
php-forum Super User
 
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India

Re: Global variable

Postby raulgh5 » Sun Aug 24, 2003 10:46 pm

Hi all:

You may not get my question. The problem is not to get the number from the "txtNumber" textbox but to get the SQLstmt which is lastly executed. That is why I want to use a global variable. Because the var SQLStmt automatically becomes null after it is passed to a function. However, for my program, I want to get it after it was executed.

I don't want to use session, because the session concept doesn't fits my application scenario.

I changed the code as follows:
[
Global g_sql //this is the global var

if ($Msg == "NewUsr"){
$SQLStmt = "Select datetime, nick, mobile from users order by datetime desc";
$g_sql = $SQLStmt;
displayResults($SQLStmt);}

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

//Here is where the user refresh the search result:
if($txtNumber != ""){
$SQLStmt = $G_SQLStmt;
DisplayResults($SQLStmt);}
]

But it doesn't work, I found the g_sql is blank after either of the first two select statment is executed.

Any idea to solve the problem?
raulgh5
New php-forum User
New php-forum User
 
Posts: 3
Joined: Sun Aug 24, 2003 8:04 pm

Postby sigix » Mon Aug 25, 2003 1:15 am

.....or sessions not solution :arrow:
sigix
php-forum Active User
php-forum Active User
 
Posts: 364
Joined: Mon Jul 14, 2003 9:39 pm
Location: /Earth/Xion

Postby Redcircle » Mon Aug 25, 2003 5:28 am

User avatar
Redcircle
Moderator
Moderator
 
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA

Re: Global variable

Postby swirlee » Mon Aug 25, 2003 12:32 pm

raulgh5 wrote:Global g_sql //this is the global var


First of all, unless g_sql is a constan (as created by define()), then it needs a $ before it just like every other variable. Second, if you really want a global variable (as defined in the documentation, as I have a sneaking suspicion that you have the wrong idea of what a global variable is), the you have to declare the variable global from within the function that's referencing it:

Code: Select all
<?
$a = 1; // the variable you want to be global -- right now it isn't

function add_one() {
   global $a; // now the variable can be accessed from within this function
   $a++;
}

add_one(); // add one to $a
echo $a; // this will output '2';
?>


The other way to do this is using pass-by-reference:

Code: Select all
<?
$a = 1; // look familiar?

function add_one(&$var) { // see the &? that means pass it by reference
   $var++;
}

add_one($a); // add one to $a;
echo $a; // this will output '2'
?>


Now do me a favor and read both of those links very thoroughly. Don't just copy my code and expect it to work. Of course, the example above is a bad one, because it'd be better to just to do this:
Code: Select all
<?
   $a = 1;

   function add_one($var) {
      return $var++;
   }

   $a = add_one($a); // add one to $a, assign the result to $a
   echo $a; // this will output '2'
?>
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby Joel » Mon Aug 25, 2003 1:27 pm

Why is the $SQL_stmt becoming null after the function? Is there something in the function that is doing this? If you only need the SQL statement variable available on one page then you shouldn't need to do anything special to reach it anywhere AFTER it has been assigned.
Joel
New php-forum User
New php-forum User
 
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.