• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do this code for most functions

#1
[eluser]_Smilie_[/eluser]
Hi,
Okay, so I'm loading a header file all the time. To this file I'd like to send some information, weather or not the user is logged in, the username, etc.
What would be the best way to do this? It's required for many controllers, and many functions.

#2
[eluser]CroNiX[/eluser]
You could create a simple library that all of your controllers could use.
You don't really need to send it the login status, let it figure it out and create the appropriate "header" automatically.

All I do is send my "template" library the main view created from a controller and it does the rest as far as header, footer, menus, sidebars, notifications, etc.

#3
[eluser]_Smilie_[/eluser]
That was my first thought as well, but I'm quite new to PHP OOP, and when I try I receive this error:
Call to a member function isLoggedIn() on a non-object

But if I parse the object to the template, then again, I need to do this everywhere. Any workaround?

#4
[eluser]CroNiX[/eluser]
Post your code that you are trying

#5
[eluser]_Smilie_[/eluser]
if($ipbwi->member->isLoggedIn()){
$data['loggedin'] = true;
$member = ipbwi->member->info();
$data['accountName'] = $member['members_display_name'];
$data['lastVisit'] = $member['last_visit'];
}
else{
$data['loggedin'] = false;
}

ipbwi is on autoload.

#6
[eluser]CroNiX[/eluser]
From within libraries, to access CI, you need to bring CI into the library or you will get that error because it can't find the object.
Code:
class Something {
  private $CI;  //variable to hold CI object

  function __construct($settings = array())
  {
    parent::__construct($settings);

    $this->CI =& get_instance(); //bring the CI object into your library
  }

  public function check_login()
  {
    //Use CI object in your library, $this->CI instead of just $this like in controllers/etc.
    if ($this->CI->ipbwi->member->isLoggedIn())
    {
      //logged in
    }
    else
    {
      //not logged in
    }
  }
}

#7
[eluser]_Smilie_[/eluser]
Thanks a bunch, worked as a charm.

#8
[eluser]_Smilie_[/eluser]
Hm, yea so it worked liked a charm, just unsure of how to access the variables and the functions in the class

#9
[eluser]CroNiX[/eluser]
Not sure what you mean, but it's really no different than any other php class. You'll need to be a little more explicit or show some code.
But, basically, local variables/methods in the library:
Code:
$this->variable;
$this->method();

From another CI object that's loaded
Code:
$this->CI->class->variable;
$this->CI->class->method();

#10
[eluser]_Smilie_[/eluser]
So this is the top of my view file:
Code:
<?php
class MasterHeader {
  private $CI;  //variable to hold CI object

  function __construct($settings = array())
  {
    //parent::__construct($settings);

    $this->CI =& get_instance(); //bring the CI object into your library
    
   if ($this->CI->ipbwi->member->isLoggedIn())
    {
      $loggedin = TRUE;
  $member = $this->CI->ipbwi->member->info();
  $accountName = $member['members_display_name'];
  $lastVisit = $member['last_visit'];
    }
    else
    {
      $loggedin = FALSE;
    }
  }
  
  function returnTrue(){
   return true;
  }
}
  
new MasterHeader();  
?>

How do I call the returnTrue method from below (among all the html), or how do I access the variable $loggedin Smile


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.