0

I have a problem on this line 'href' => 'index.php?action=profile&user='.escape($user->data()->username).'',

When the user is logged out, i get that error, but when i login, i dont get the error.

function toolbar_section()
{
    $user = new User();
    global $variables;
    $variables['menu_buttons'] = array(
        'home' => array(
            'title' => 'Pocetna',
            'href' => 'index.php',
            'show' => true,
        ),
        'profile' => array(
            'title' => 'Profil',
            'href' => 'index.php?action=profile&user='.escape($user->data()->username).'',
            'logged' => true,
        ),
        'logout' => array(
            'title' => 'Odjava',
            'href' => 'index.php?action=logout',
            'logged' => true,
        ),
    );
}
Monk
  • 105
  • 9
  • What is output of `var_dump($user);` ? – AnkiiG Jan 04 '16 at 07:33
  • Where did you get that? – Monk Jan 04 '16 at 07:36
  • Does your user object contain an username while the user is logged out? I'm assuming no, and the error is because the data you are trying to print doesn't actually exist? – Natalie Hedström Jan 04 '16 at 07:50
  • Thats right. When the user is logged in, all is ok, but when i he logout...the code is still loading `escape($user->data()->username)` since its only showing for logged users. – Monk Jan 04 '16 at 07:55

1 Answers1

1

You have To check Condition before like

Code:

function toolbar_section()
{
    $user = new User();
    global $variables;
    if(count($user) > 0)
    {
       $href =  'index.php?action=profile&user='.escape($user->data()->username).'';
    }
    else
    {
       $href = 'index.php?action=profile&user='.escape("abc").'';
    }
    $variables['menu_buttons'] = array(
        'home' => array(
            'title' => 'Pocetna',
            'href' => 'index.php',
            'show' => true,
        ),
        'profile' => array(
            'title' => 'Profil',
            'href' => $href,
            'logged' => true,
        ),
        'logout' => array(
            'title' => 'Odjava',
            'href' => 'index.php?action=logout',
            'logged' => true,
        ),
    );
}
ekta singh
  • 138
  • 1
  • 8
  • Now i get `Notice: Undefined variable: href in...` – Monk Jan 04 '16 at 07:39
  • Ok try this condition in if statement if($user->data()->username != "" ) – ekta singh Jan 04 '16 at 07:46
  • `Parse error: syntax error, unexpected '!'` on that line – Monk Jan 04 '16 at 07:49
  • dont give space between != if($user->data()->username != "" ) – ekta singh Jan 04 '16 at 07:49
  • Yeah right...but now i get the first error on line `$href = 'index.php?action=profile&user='.escape($user->data()->username).'';` – Monk Jan 04 '16 at 07:51
  • write print_r($user);die; before if condition and what output generate provide me – ekta singh Jan 04 '16 at 07:53
  • `User Object ( [_db:User:private] => DB Object ( [_pdo:DB:private] => PDO Object ( ) [_query:DB:private] => [_error:DB:private] => [_results:DB:private] => [_count:DB:private] => 0 ) [_data:User:private] => [_sessionName:User:private] => user [_cookieName:User:private] => hash [isLoggedIn:User:private] => )` – Monk Jan 04 '16 at 07:56
  • I found a solution. I put `if(Input::exists())` instead of your `if` and now everything works fine. Can you just explain me what does this mean `escape("abc")` because you wrote it. – Monk Jan 04 '16 at 08:17