Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Return a list of users except logged in user
  • Hi there,

    I'm new to PHP frameworks and utilizing MVC models and have a question regarding the procedure for obtaining a list of users with the exception of the logged in user who is viewing aforementioned list.

    I've created my current situation by following this tutorial. I created the 'users' scaffold following the structure of the command for administering 'posts' as shown in the tutorial. The command used was:

    oil g admin users username:varchar[50] password:string group:int email:string last_login:int login_hash:string profile_fields:text

    I've cleaned up the parts I didn't need for now such as viewing the password hash and profile field from the views. My question is for the view of _index where it displays the list of users. The list also displays the logged in user which is not what I want. I want to display the list of users with the exception of the logged in user.

    This is what my action_index() in the Admin_Users controller looks like. My custom function in the model looks like this. The problem is when I visit the page, I get an error which says:

    Undefined variable: current_user

    APPPATH/classes/model/user.php @ line 44


    And yes, I am logged in as an Admin when I view that page. Can someone please tell me what I'm doing wrong and point me in the right direction?

    Thanks for your time.
  • HarroHarro
    Accepted Answer
    In your custom method, $current_user is a local variable, but it's not set anywhere.

    Assuming you're using the Auth package for auhtentication, you can use

    \Auth::check()

    to see if someone is logged in or not, and

    \Auth::get('username')

    to get the username of the current logged-in user.
  • I really appreciate your timely responses.

    Yes I'm using the 'auth' package. Based on this tutorial, I'm lead to believe it's 'SimpleAuth'. I've edited my function following your suggestion (I hope it follows the right conventions and usage) and can be viewed here. But now I'm getting an error from the view file which can be viewed here. What should I change to make it work again?

    Thanks.
  • Alhamdulillah I was able to solve the problem myself. I changed the function in my controller to this:


    public static function all_users_except_current_user() {
    $logged_in_username = \Auth::get('username');
    $result = Model_User::find('all', array(
    'where' => array(
    array('username', '<>', $logged_in_username),
    ),
    ));
    return $result;
    }


    Thanks a lot, Harro Verton.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion