View Single Post
Old 03-02-2008   #2 (permalink)
Jolt
Registered User
 
Join Date: Aug 2004
Posts: 119

Easiest way is to create an array for the user.

Create a file that can be included in every page of your site. Or include the below in something like your header include.

The basic principal is you create a cached array to store any info you want to display on the page without needing to query the DB for each instance.

Note, I have not gone through the membership tutorial here so obviously DB field names and values will need to be changed to match your DB.

I choose to set a cookie for the user with their username when they are logged in. I can then call the cookie username value to use in the query. The membership tut here uses a session cookie I believe. Same thing, different storage capabilities.

PHP Code:
if($_SESSION['user'] != '') { //checks for the session cookie first. If it's there proceed.
    
$user $_SESSION['user'];
    
$usr mysql_query("SELECT * FROM users WHERE name = '$user'") or die(mysql_error()); 
    while (
$user mysql_fetch_array($usr)) {
        
$u = array('id' => $user['id'], 'name' => $user['name'], 'email' => $user['mail'], 'fname' => $user['Firstname'], 'lname' => $user['Lastname'], 'address' => $user['addy'], 'city' => $user['city'], 'state' => $user['state'], 'zip' => $user['zip'], 'phone' => $user['phone'], 'pass' => $user['Pword']);
    }
    
mysql_free_result($usr);

With that array set you can then call any user information on the page. Such as $u['name'] will equal the username of the person browsing, $u['city'] will echo their city, etc. Change the array values to match your database.
Jolt is offline   Reply With Quote