Jump to content

Help with pulling/displaying data from table?


Sherlocked

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Sherlocked
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.