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.
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..
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. Here's the 'names' table again. Table: names id (same as UserId session variable) first_name last_name
dude is that an exam? 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.
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! 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.
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.
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?
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
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?
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
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.