1
moisty70
How to declare a global variable?
  • 2007/10/1 10:46

  • moisty70

  • Just popping in

  • Posts: 21

  • Since: 2007/8/24


Hello, I am developing a module on which a navigation bar is used.
Categories List -> Category A -> Product B

I would like to set the selected category as a global variable to avoid access the database on every user click.

I have all the module presentation on index.php with different 'op'.

I declare $GLOBALS['selectedCat']=$cat when a Category is selected on Categories list page.
Then a products page is shown.

Then when I select a product $GLOBALS['selectedCat'] is null.

How can I declare a global variable on this context?

2
kaotik
Re: How to declare a global variable?
  • 2007/10/1 17:49

  • kaotik

  • Just can't stay away

  • Posts: 861

  • Since: 2004/2/19


actually, all you need is this in each new page and/or function:
global $cat;

3
moisty70
Re: How to declare a global variable?
  • 2007/10/2 8:21

  • moisty70

  • Just popping in

  • Posts: 21

  • Since: 2007/8/24


Thanks.

So the problem is other, may be a reference problem or something like that.

I cannot get to it...

I use
global $cat;
$categoria = $cat_handler->get($catid);
$cat = $categoria;

And use global $cat when I try to use it later...

4
nachenko
Re: How to declare a global variable?
  • 2007/10/2 9:19

  • nachenko

  • Quite a regular

  • Posts: 356

  • Since: 2005/1/18


From the code you've just put, you seem to use global before declaring the variable. If it's so, you're in a mistake.

global $cat is not used to declare a global variable, but inside functions and stuff like that to "tell the system" that when we "say" $cat we are talking about a global variable declared OUTSIDE functions.

I've had some problems like this before, and I found the solution searching for "variable scope" in the PHP manual.

5
Dave_L
Re: How to declare a global variable?
  • 2007/10/2 9:30

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


Quote:

moisty70 wrote:

I would like to set the selected category as a global variable to avoid access the database on every user click.


So you want to access the variable on different HTTP requests, not merely later on the same HTTP request?

Using a "global" variable won't accomplish that. You'll need to save the variable in one of several ways: in a session, URL parameter, form field or cookie.

6
moisty70
Re: How to declare a global variable?
  • 2007/10/2 10:35

  • moisty70

  • Just popping in

  • Posts: 21

  • Since: 2007/8/24


I thought Globals are stored on session.
Ok, I'll do it with $_SESSION.

Where are XOOPS variables stored?
For example, xoopsConfig?

Thanks.

7
Catzwolf
Re: How to declare a global variable?
  • 2007/10/2 11:38

  • Catzwolf

  • Home away from home

  • Posts: 1392

  • Since: 2007/9/30


While doing this might seem like a good idea does come with some complications, storing Vars within a $_SESSION could create more problems than it would solve, plus the security issues on a sharded host.

Also, the other problem you may come across with this is an out of date data, this could happen if someone else changes or deletes the data within your categories.

You could call it 'static'ly or if your version of MySQL allows, you could cache the SQL query. Look at the MySQL manual for this.

Catz

Login

Who's Online

189 user(s) are online (62 user(s) are browsing Support Forums)


Members: 0


Guests: 189


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Oct 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits