TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-07-2008, 04:01 PM   #1 (permalink)
The Wanderer
 
Join Date: Mar 2008
Location: United Kingdom
Posts: 22
Thanks: 1
boycoda is on a distinguished road
Default Seems advanced to me :)

Hey all,

Ok.. this is my first ever system i'm building using classes and functions. So far i'd say im doing very bloody good with it.

Anyhow, I have a question for you more advanced programmers here.

I have a class called, dbclass lets say. And in that class, I have a function to check if the username exists, a function to grab the persons name, and some other functions.

Then further down, I have a new class, called admin. Obviously extending dbclass.

Now, I have a function on here, called welcome. Pretty simple right? Well, my problem is that i've set $email as a session. And the getname function will grab all the users information where the email is the same as the session email.

So i'm able to get the name of the user no problem.

And all this jargon i'm going on about does infact work. But!, in my welcome function, if I do this...

echo "<h3>Welcome back, <span>" . $this->getUsersName($email) . "</span></h3>";

The users name will go above the welcome back text.. like this,

adam
Welcome back,

Do you need to see some code to see what i mean or is this a straight forward fix?

Thanks in advance.
Send a message via MSN to boycoda Send a message via Skype™ to boycoda
boycoda is offline  
Reply With Quote
Old 08-07-2008, 08:26 PM   #2 (permalink)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Cana'derr
Posts: 653
Thanks: 24
delayedinsanity is on a distinguished road
Default

It's a straight forward fix. You're using an echo statement inside of getUsersName(), correct? Instead of echoing the data, put it in a variable, and return it at the end of the function. Then it will work properly, and will allow you to do any of the following:

PHP Code:
$variable $this->getUsersName(); // return to another var
echo $this->getUsersName(); // echo it directly
echo 'some string'.$this->getUsersName().'end of string'// echo it within a string 
-m
delayedinsanity is offline  
Reply With Quote
Old 08-07-2008, 09:14 PM   #3 (permalink)
The Wanderer
 
Join Date: Mar 2008
Location: United Kingdom
Posts: 22
Thanks: 1
boycoda is on a distinguished road
Default

Thanks for that, I will try this out tomorrow.
Send a message via MSN to boycoda Send a message via Skype™ to boycoda
boycoda is offline  
Reply With Quote
Old 08-08-2008, 09:44 AM   #4 (permalink)
The Wanderer
 
Join Date: Mar 2008
Location: United Kingdom
Posts: 22
Thanks: 1
boycoda is on a distinguished road
Default

hmm, I still have the same problem.

This is my getUsersName function..

PHP Code:
    function getUsersName() {
    
        
$email $_SESSION['myemail'];
        
$query mysql_query("SELECT * FROM `tbl_users` WHERE `email` = '$email' LIMIT 1") or trigger_error(mysql_error());
        
        
$row mysql_fetch_array($query);
        
        echo 
$row['name'];
        return;
    
    } 
And in my other function I have put this line,

PHP Code:
<h3>Welcome back, <span>'.$this->getUsersName().'</span></h3>; 
It still appears above the welcome back, text
Send a message via MSN to boycoda Send a message via Skype™ to boycoda
boycoda is offline  
Reply With Quote
Old 08-08-2008, 10:34 AM   #5 (permalink)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Cana'derr
Posts: 653
Thanks: 24
delayedinsanity is on a distinguished road
Default

It's the echo. When you try and use string concatenation on a function that echo's a value, instead of returning it, the value will appear before the string in which it's been included.

Try this:

PHP Code:
    function getUsersName()
    { 
     
        
// In addition, since you're only using the name column from your
        // query, it's more efficient to just select that column instead of *
        
$q sprintf("SELECT name FROM `tbl_users` WHERE `email` = '%s' LIMIT 1"mysql_real_escape_string($_SESSION['myemail']));
        
$result mysql_query($q) or trigger_error(mysql_error()); 
         
        
$row mysql_fetch_assoc($result);
         
        return 
$row['name']; 
     
    } 
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
boycoda (08-08-2008)
Old 08-08-2008, 10:41 AM   #6 (permalink)
The Wanderer
 
Join Date: Mar 2008
Location: United Kingdom
Posts: 22
Thanks: 1
boycoda is on a distinguished road
Default

Dude, your a lifesaver! :D Thanks alot for the help, I understand it now.
Send a message via MSN to boycoda Send a message via Skype™ to boycoda
boycoda is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:44 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0