Replace display of user's login name with user's full name

Discussion in 'PHP' started by healthy, Jan 8, 2010.

  1. #1
    I have a CakePHP application that requires a simple change that is beyond my limited php/MySQL skills. I'd like to replace the display of the user's login name with the user's full name.

    Here's the current code:
    <?php if($session->read('UserId')) {?>
    <?php echo $session->read('Username'); ?>
    <?php  $chidUsername = $session->read('ChildActorUsername');
    if(!empty($chidUsername)) 
    { echo " >> ".$chidUsername."";}?>
    Code (markup):
    The MySQL tables and fields:

    Table: actors
    id
    username

    Table: names
    first_name
    last_name

    I hope that's sufficient information. Thanks for any help.
     
    healthy, Jan 8, 2010 IP
  2. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    from what I understand the changes will not be done from the code you posted here..
    you need to search and see where is the session variable which gets the username value and then change it with firstname - lastname values..
     
    xenon2010, Jan 8, 2010 IP
  3. healthy

    healthy Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks Xenon. The first and last name variables are not saved in the session. I was hoping that based on the UserId session variable, the values could be looked up in the 'names' table.

    Now that I look back at my original post, the 'actors' table is irrelevant and I forgot to put in the related field in the 'names' table. It's good that I don't do this stuff for a living. :p

    Here's the 'names' table again.
    Table: names
    id (same as UserId session variable)
    first_name
    last_name
     
    healthy, Jan 8, 2010 IP
  4. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    dude is that an exam? :D
    give us more info.
    okay at least how the session "username" gets its value. its surly from the db 'I suppose'.
    but I'm pretty sure you need to query your database first like:
    SELECT first_name, last_name FROM names WHERE id = '$session->read('UserId')'
    then you get the values of first_name and last_name then put them inside 2 variables.
    then take the two vars and put them inside $session->read('Username') or somthin like that.
    now the $session->read('Username') will be containing the firstname and lastname of that user.
     
    xenon2010, Jan 8, 2010 IP
  5. healthy

    healthy Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    As you may have gathered, I don't know enough to give an exam. I barely know enough for this post. I wouldn't want your nightmares! :p

    I think that's my missing link, but what's the code to get the values of first_name and last_name? I'm thinking that I can replace the first echo statement from the original code with something like this:
    <?php echo $first_name." ".$last_name;?>
    Code (markup):
    I'm pretty sure that the second echo statement is for subusers of the account. I'll tackle that after I get the first part settled. Thanks for your help.
     
    healthy, Jan 8, 2010 IP
  6. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    hmm I'm not sure but here how it goes.
    although the codes I will post is not necessarily will be workin with PHPCake you need to modify and do necessary changes to them.
    1st you query your DB:
    $query = $sql->query("SELECT first_name, last_name FROM names WHERE id = '".$session->read('UserId')."'");
    $results = $sql->fetch($query);

    then put firstname and lastname into PHP vars like:
    $firstname = $results['first_name'];
    $lastname = $results['last_name'];

    now you got the firstname and lastname all you need is to push them inside $session->read('Username') session.
    since im not familiar with PHPCake sessions/mysqlDB classes. the above codes might not work with you. its just the concept of how u can do it.
     
    xenon2010, Jan 8, 2010 IP
  7. healthy

    healthy Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Okay, so how slow is this reply?

    It turns out that one of my issues was there was an additional MySQL table in the middle, but I'm going to hire an experienced Cake PHP developer to do this. Worst case, how long do you think it should take?
     
    Last edited: Jan 14, 2010
    healthy, Jan 14, 2010 IP
  8. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi,

    If you're working from inside one of the controllers and it has a database model already working, you can use the CakePHP's in-built methods of getting data from the MySQL database. eg. find, findall, findby

    However, from looking at that table design there is nothing linking the "actors" to the "names" table, no related fields eg. (ID's)

    I'll leave you with a link: http://book.cakephp.org/view/451/findBy

    If the models are set-up properly and the database tables have related fields, you should be able to use that to get the first_name and last_name for a certain user ID/username.

    One good thing about Cake, is that you don't need to know SQL to fetch information from the database; there are already in-built functions to do that for you.

    PS. Anyone who knows CakePHP it should take anywhere from 5mins - 20mins depending if the controllers/models and database design needs altering.

    Regards,
    Steve
     
    Steve136, Jan 14, 2010 IP
  9. healthy

    healthy Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks Steve. That's the info I was looking for.
     
    healthy, Jan 14, 2010 IP
  10. healthy

    healthy Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hmmm, I just thought some more about what you said. The relationship is two step with names.ID linked to profiles.ID linked to actors.ID. There's a mountain of reasons not to mess with that. Should it still be easily doable?
     
    healthy, Jan 14, 2010 IP
  11. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Can you show us the full structure of the names and actors tables? If there is a relationship between those two the database wouldn't even need editing, it would just be a matter of fetching the information with cake.

    Just a stab in the dark here, but does this return anything? (or just give any errors?)

    $testData = $this->Names->findByID($session->read('UserId'));
    var_dump($testData); //Print out the array.
    
    PHP:
    Regards,
    Steve
     
    Last edited: Jan 14, 2010
    Steve136, Jan 14, 2010 IP
  12. healthy

    healthy Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    The full structure of the relevant tables would be a bit much. This is from my issue description:

    Code to be changed in app/views/elements/template/header.ctp:
    <?php $chidUsername = $session->read('ChildActorUsername'); if(!empty($chidUsername)) { echo " >> ".$chidUsername."";}?>

    Fields in table - actors:
    id
    username

    Fields in table - profiles:
    id
    actor_id (related to actors.id)

    Fields in table - names:
    profile_id (related to profiles.id)
    first_name
    last_name

    I really appreciate your help.
     
    healthy, Jan 14, 2010 IP