Sherlocked Posted November 26, 2014 Share Posted November 26, 2014 Hello, I'm not sure if I'm allowed to post the code, as I didn't write it. But, if you could offer some ideas on where I'm going wrong, I'd be very appreciative! I basically have a feature on my homepage that displays "Hello _username_!" upon login, but the username is missing. So, I'm presuming that the functionality for pulling the username from the table is somehow failing. Any ideas? Thanks Daisy Quote Link to comment https://forums.phpfreaks.com/topic/292729-help-with-pullingdisplaying-data-from-table/ Share on other sites More sharing options...
ginerjm Posted November 26, 2014 Share Posted November 26, 2014 Without seeing your code how do you even think we can help you? I can only guess that when you verify the login (with a db query?) that you are not pulling in the username as part of that query. Include it in the validation check query and then set a local var and echo that out when the time comes. Quote Link to comment https://forums.phpfreaks.com/topic/292729-help-with-pullingdisplaying-data-from-table/#findComment-1497738 Share on other sites More sharing options...
Sherlocked Posted November 26, 2014 Author Share Posted November 26, 2014 (edited) Thanks for the suggestion. I realise that I'm not giving you much to go on, I just don't want to get into trouble for posting code I didn't write. I suppose if it's just excerpts then it should be okay. This is the bit that doesn't want to work. It follows a redirect from the login page, upon successful login: <p>Hello <a href="profile.php?user=<?php echo escape($user->data()->username); ?>"><?php echo escape($user->data()->username); ?></a>!</p> It's supposedly activated by a query to confirm whether or not the user is logged in. I think this is said query: public function __construct($user = null) { $this->_db = DB::getInstance(); $this->_sessionName = Config::get('session/session_name'); $this->_cookieName = Config::get('remember/cookie_name'); // Check if a session exists and set user if so. if(Session::exists($this->_sessionName) && !$user) { $user = Session::get($this->_sessionName); if($this->find($user)) { $this->_isLoggedIn = true; } else { $this->logout(); } } else { $this->find($user); } } public function exists() { return (!empty($this->_data)) ? true : false; } public function find($user = null) { // Check if user_id specified and grab details if($user) { $field = (is_numeric($user)) ? 'id' : 'username'; $data = $this->_db->get('users', array($field, '=', $user)); if($data->count()) { $this->_data = $data->first(); return true; } } return false; } Any help? It won't pull the name for any of the functionality (I also have a profile page that is supposed to display the user's name, but currently says "Full name", and when trying to update the user's name, it should have the current name listed in the textbox, but doesn't. I *think* I've narrowed it down to the $data variable, owing to the fact that it seems to be in all of the code that isn't functioning, whereas the other variables are not. Although, quite what that means, I'm not sure. Thanks again. Edited November 26, 2014 by Sherlocked Quote Link to comment https://forums.phpfreaks.com/topic/292729-help-with-pullingdisplaying-data-from-table/#findComment-1497746 Share on other sites More sharing options...
ginerjm Posted November 26, 2014 Share Posted November 26, 2014 I would think that none of that would show if the user is not logged in. Also why an anchor tag to display a value? You want people to be able to click their name? Maybe try this: if (isset($user->data()->username) && $user->data()->username <> '') echo 'Hello ',$user->data()->username; else echo 'You are not logged in'; I have no idea what the data method is doing but I'm taking a guess here. Quote Link to comment https://forums.phpfreaks.com/topic/292729-help-with-pullingdisplaying-data-from-table/#findComment-1497770 Share on other sites More sharing options...
Sherlocked Posted November 26, 2014 Author Share Posted November 26, 2014 The anchor tag is for a clickthrough to the user's personal profile, for which the only access is set through that link at the moment. Quote Link to comment https://forums.phpfreaks.com/topic/292729-help-with-pullingdisplaying-data-from-table/#findComment-1497771 Share on other sites More sharing options...
ginerjm Posted November 26, 2014 Share Posted November 26, 2014 Looking at the partial code of your class try this: if $user is defined as public in that class, then you should be able to reference $user for the current object, whatever that is. Your initial code shows you trying to grab something using $user but $user is a property within the object so it's a bit confusing. Did you make up that code? Quote Link to comment https://forums.phpfreaks.com/topic/292729-help-with-pullingdisplaying-data-from-table/#findComment-1497774 Share on other sites More sharing options...
Sherlocked Posted November 26, 2014 Author Share Posted November 26, 2014 No, I bought it. PHP is not my specialty I'm afraid! So I'm learning on the fly, as it were. I'd happily live without the extraneous functionality, but if the only way the profile is generated is via this particular function/query/whatever, then I need to riddle it out. Quote Link to comment https://forums.phpfreaks.com/topic/292729-help-with-pullingdisplaying-data-from-table/#findComment-1497776 Share on other sites More sharing options...
ginerjm Posted November 26, 2014 Share Posted November 26, 2014 You need to find the name of the object that is created by a successful login and then reference that in you Hello thingie. Something in the form of: $(objectname)->user I think. Quote Link to comment https://forums.phpfreaks.com/topic/292729-help-with-pullingdisplaying-data-from-table/#findComment-1497778 Share on other sites More sharing options...
Sherlocked Posted November 26, 2014 Author Share Posted November 26, 2014 I just echoed out user Id and it's incorrect. I don't know if that sheds any light on anything? It's showing as 1, when it should be 33. Quote Link to comment https://forums.phpfreaks.com/topic/292729-help-with-pullingdisplaying-data-from-table/#findComment-1497781 Share on other sites More sharing options...
Sherlocked Posted November 27, 2014 Author Share Posted November 27, 2014 I figured it out! It was the escape function. So, I deleted them all and it now works, but obviously I'm not comfortable with that setup. I tried slightly amending the syntax from: <?php echo escape($user->data()->name); ?> to: <?php echo (escape($user->data()->name)); ?> ...but it didn't work. Do you know how I could include it without it messing up my functionality, or why it's messing it up to begin with? Thanks D Quote Link to comment https://forums.phpfreaks.com/topic/292729-help-with-pullingdisplaying-data-from-table/#findComment-1497791 Share on other sites More sharing options...
ginerjm Posted November 27, 2014 Share Posted November 27, 2014 I have no idea what this 'escape' function does so I cannot offer any help. And your edit attempt really doesn't accomplish anything here. Adding parens is meaningless. Quote Link to comment https://forums.phpfreaks.com/topic/292729-help-with-pullingdisplaying-data-from-table/#findComment-1497837 Share on other sites More sharing options...
Sherlocked Posted November 27, 2014 Author Share Posted November 27, 2014 Sorry, I'm not well-versed in which terms are universal, and which are not. I appreciate you bearing with me! The escape function, in this instance, is for the purpose of sanitising: <?php function escape($string) { return htmlentities($string, ENT_Quotes, 'UTF-8'); } Quote Link to comment https://forums.phpfreaks.com/topic/292729-help-with-pullingdisplaying-data-from-table/#findComment-1497853 Share on other sites More sharing options...
Barand Posted November 27, 2014 Share Posted November 27, 2014 The "ENT_Quotes" constant should be uppercase - ENT_QUOTES Quote Link to comment https://forums.phpfreaks.com/topic/292729-help-with-pullingdisplaying-data-from-table/#findComment-1497856 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.