Welcome Guest, Not a member yet? Register   Sign In
Custom Breadcrumbs
#11

[eluser]frankthetank[/eluser]
Hey Guys,

Here is my current solution to my problem. First I made a custom Breadcrumb class with some initial values such as delimiter and my forum home index.

Code:
class Breadcrumb  {

        private $delimiter=" > ";
        private $forum_home="Forum Index";
        private $url="";
        private $obj;

        public function makeBreadCrumbs($title){

            $this->obj =& get_instance();
            $currentpage= $this->obj->uri->rsegment(1);

            switch($currentpage) {
                case "":
                    return "Forum Index";
                    break;
                case "viewforum":
                    $this->url=anchor('',$this->forum_home) . $this->delimiter . $title;
                    $this->obj->session->set_userdata('prevurl',anchor('viewforum/' . $this->obj->uri->rsegment(3),$title));
                    return $this->url;
                    break;
                case "viewthread":
            $this->url=anchor('',$this->forum_home) . $this->delimiter . $this->obj->session->userdata('prevurl') . $this->delimiter . $title;
            return $this->url;
            break;
.....

I pass the name how I like my URL to be seen and then get the first segment of my URI which is always the current controller, for example viewforum.php.
Then I can do a case on this variable. For the first case it is my forum home. The second case for example, viewforum, is more difficult. I need to store this url in the session because when we view the thread we want a direct link back this forum using the variables given in the previous url. Using this session variable we can then do a case on viewthread and construct a new breadcrumb using the previous url and the current url.

Also, depending on where your variable is set in the uri you have to fetch the correct rsegment (also static Sad ).

Then in the header I simply say:

Code:
<?=$this->breadcrumb->makeBreadCrumbs($breadcrumb);?>

Printing the breadcrum with the given title, which is given in the $breadcrumb variable.

Hope this helps someone who has the same problem.
Regards Frank
#12

[eluser]frankthetank[/eluser]
Update:

This does not work when you end with an editmessage crumb. Therefore you have to store an array of urls in your session data and select which one you need for each static breadcrumb defined in your switch.
#13

[eluser]iive[/eluser]
The solution that I am doing is :

Create a Breadcrumb class and a BreadcrumbNode class. Breadcrumb class will have a function called addNode() which accept BreadcrumbNode and a function called build() to build out the whole breadcrumb.

I use a config file to define all the static page in an array and then pass the whole array into Breadcrumb class to build the tree.


BreadcrumbNode take arguements as:
BreadcrumbNode($level, $parent_id, $node_id, $indentifier, $href, $label );

It looks something similiar to:
Code:
$node_array = array();
$node_array[] = new BreadcrumbNode(0,NULL,0,"home", "home", "Home");
$node_array[] = new BreadcrumbNode(1,0,1,"games", "game", "Games");
$node_array[] = new BreadcrumbNode(1,0,2,"member_page", "user", "My Account");

Inside controller,
Code:
class Game extends Controller {

    function Game()
    {
       parent::Controller();
       $this->load->library('breadcrumb');
    }
    
    function index()
    {
       // Will produce Home >> Games
       echo $this->breadcrumb->render("games"));
    }

    function play(){
       //Get game name dynamically from URI
       $game_name = $this->uri->segment(3);
            
       // Add new BreadcrumbNode dynamically by specify the new level and parent id
       // set the third arguement to null so that Breadcrumb class can generate
       // next increment id
       $this->breadcrumb->addNode(new BreadcrumbNode(2,1,null,"games-play","game/play/".$game_name, $game_name));

       // Generate the breadcrumb
       // will produce Home >> Games >> Game Name
       echo $this->breadcrumb->render("games-play");
            
    }
}

I hope you get what I tried to mean. I am not sure whether this is a good way to work with breadcrumb. I have searched through the forums, but I couldn't get a good solution for this. So I come out these 2 classes to accomplish my task.
#14

[eluser]frankthetank[/eluser]
I kind of do the same thing but less structured since It might not even be in the final product so I want to put my effort in other things.

I keep the static information in the users session variable and update it on each different page using the switch statement in the library. It works fine for me! Smile

Hope our information will help others out there.




Theme © iAndrew 2016 - Forum software by © MyBB