Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
using a single variable from a separate file without using import
Old 06-10-2010, 11:23 PM using a single variable from a separate file without using import
Skilled Talker

Posts: 83
Trades: 0
I have created a file called onlineusers.php that figures out how many users there currently are online. At the end of the script is a variable that captures the final number. I want to be able to use ONLY this variable in a separate file called members.php

How do I go about doing something like this?
Smudly is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-11-2010, 04:51 AM Re: using a single variable from a separate file without using import
Mad182's Avatar
Skilled Talker

Posts: 54
Name: Madars
Location: Latvia
Trades: 0
Code:
include('onlineusers.php');
echo $variable;
__________________
There's no place like 127.0.0.1

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Mad182 is offline
Reply With Quote
View Public Profile Visit Mad182's homepage!
 
Old 06-11-2010, 05:16 AM Re: using a single variable from a separate file without using import
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Or better yet, transform this script in a function, and make it available site wide by having this file included in each page.

Then getting that number would simply be
PHP Code:
$users=countUsers(); 
in any page to fetch the current user count.

How to transform the script in a function you might ask?
Take this code for example:
PHP Code:
<?php
$users
=0;
$query="select count(*) as cnt from users where activ=1";
$r=mysql_query($query);
while(
$o=mysql_fetch_object($r)){
  
$users=$o->cnt;
}
To transform this in a function, simply add the definition at the beginning, and return the value you fetched:
PHP Code:
<?php
function userCount(){
  
$users=0;
  
$query="select count(*) as cnt from users where activ=1";
  
$r=mysql_query($query);
  while(
$o=mysql_fetch_object($r)){
    
$users=$o->cnt;
  }
  return 
$users;
}
And then a simple
PHP Code:
<?php
print userCount();
will fetch the value and display it.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-11-2010, 11:11 AM Re: using a single variable from a separate file without using import
Skilled Talker

Posts: 83
Trades: 0
Thanks for the tips. Got it working
Smudly is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to using a single variable from a separate file without using import
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.30381 seconds with 12 queries