Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8154133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:14:23+00:00 2026-06-06T16:14:23+00:00

I was looking for a flexible way to generate different sidebars in different pages.

  • 0

I was looking for a flexible way to generate different sidebars in different pages. The goal is to pass custom links to each sidebar. The template libraries seemed overkill for my application so I developed an easy solution. I’m not certain if it’s the best solution. My question is what do you think? Your advice is immensely appreciated!

1. In your controller add a private function that loads your sidebar view:

/**
 * The function has 2 arguments. 
 * $title is the sidebar widget title.
 * $widget will contain an array of links to be added in the sidebar widget.
 */

private function sidebar($title, $widget)   
{
    $widget['title'] = $title;
    $this->load->view('includes/sidebar', $widget); 
}

2. In the controller functions that you want to load a custom sidebar, call the private function sidebar() and pass desired sidebar data into it.

Below is a controller function named edit used to edit posts. In the example I need to load a sidebar with options to view and delete the post I’m on:

function edit($post_id = '')
{
    //your code, form validation, etc...

    //Prepare sidebar widget links
    //Array key is link url, array value is link name
    $widget['links'] = array (
        'posts/single/' . $post_id => 'View post',
        'posts/remove/' . $post_id => 'Remove post'
    );
    $this->sidebar('Options', $widget);  //load sidebar  
}

3. Finally the sidebar view displaying custom data passed from the controller:

<div id="sidebar">
    <ul>
        <li class="widget">
            <div class="label"><?php echo $title; ?></div>
            <ul>
                <?php foreach ($links as $link => $value): ?>
                    <li><?php echo anchor($link, $value); ?></li>
                <?php endforeach; ?>
            </ul>
        </li>
    </ul>
</div>

Conclusion

Add the following code to any controller function for custom sidebar title and links:

$widget['links'] = array (
            'controller/function' => 'Link Name 1',
            'controller/function' => 'Link Name 2',
            'controller/function' => 'Link Name 3'
);
$this->sidebar('Widget Title', $widget);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T16:14:25+00:00Added an answer on June 6, 2026 at 4:14 pm

    I think that gets it done. I’m not an expert in CodeIgniter, but I can tell you that’s just fine. One thing you should always make sure is to validate the data passed to a function. In this case:

    private function sidebar($title=FALSE, $widget=FALSE) {
       if ($title && $widget) {
          //process
       }
       else
          return FALSE
       }
    }
    

    Another way to do it is just to pass the links to the template (not the sidebar template, but your main template:

    $data['sidebar'] = array('link/link'=>'My Link');
    $this->load->view('mytemplate', $data);
    

    And in the template you load the sidebar template and pass it the data:

    <html>
    <!--All my html-->
    <?php $this->load->view('includes/sidebar', $data['sidebar']); ?>
    </html>
    

    This is just another option. But what you did is just fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're looking for a fast, flexible way of generating templated content (in this case
I'm looking for an ultra-easy way to generate a list of numbers, 1-200. (it
Im looking for a way to present a flexible font, that will increase and
I'm looking for a language that, while being flexible and very light on memory
I'm looking for a way to use two DateTimePicker controls to select a range
I'm looking for a way to figure out whether a given date is a
I'm looking for a way to interact with the models of a django project
Looking for a way to read the following config file sample using a multi
I'm looking for a platform for showing custom maps on android mobile phones and
I'm looking for a way to have to write and maintain a certain algorithm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.