• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tabbed selection on home page - passing variable to index controller

[eluser]Andy UK[/eluser]

I have a tabbed box on my home page - basically three different views of the same page with just the div and active tab changing.

My question is what is the best way to manage this in Codeigniter. Should I have three different controllers with three different views? Or can I pass a variable to the one index controller that selects one of the three views?

I tried passing the variable via uri segments, but while I can do this with other controllers, It doesn't seem to work with the index controller.

Please ask if my question is not clear...


You can do the following.

$data['my_data'] = 'your data';

$tabs['tab1'] = $this->load->view('tab1', $data, true);
$tabs['tab2'] = $this->load->view('tab2', $data, true);
$tabs['tab3'] = $this->load->view('tab3', $data, true);

$data['tabs'] = $tabs;

$this->load->view('page' $data);

Return true will send back the data to the variable. this is just one example but there are many ways to achieve what you want.


[eluser]Andy UK[/eluser]
Forgive my ignorance, but I don't quite follow.

How am I passing the variable from the view back to the controller? Does the code you wrote go in the controller?

I'm used to using key-value arrays to pass data from a controller to a view, But how does the controller become aware of when a certain tab is clicked in the view? As I mentioned, I tried passing the variable in the uri, but with the home page, that doesn't work and just gives a 404 message.


Yes all this would be in the controller.

As I have said by adding the TRUE to the view method it returns the view data back to the variables its being assigned to.

So in my example I have loaded 3 views back to an array which I have then passed to the last view.

As for the tabs on the front end you would need some form of javascript to show a specific tab.

[eluser]Andy UK[/eluser]
Hmm nope. Still not following. All I really want to do is pass a variable from a view to the default controller. With any other controller I would just add it as an extra segment of the uri after the controller segment.

Not sure why javascript is necessary, since each view will show its respective tab as active. It's just a case of adding class="active" for the respective <li>.

Perhaps your method is valid, but without expanding the explanation a bit, I don't think I'll ever understand it. For example, how is a variable assigned when clicking on a tab? Bear in mind that my tabs are just padded <a> tags inside of <li> tags.

What I want to do is something like:

Home page with tab No. 1 active (html snippet from view home_tab1)

<li class="active"><a href="default_controller/tab_1">TAB 1</a></li>
<li><a href="default_controller/tab_2">TAB 2</a></li>
<li><a href="default_controller/tab_3">TAB 3</a></li>
Home page with tab No. 2 active (html snippet from view home_tab2)

<li"><a href="default_controller/tab_1">TAB 1</a></li>
<li class="active><a href="default_controller/tab_2">TAB 2</a></li>
<li><a href="default_controller/tab_3">TAB 3</a></li>

Home page with tab No. 3 active (html snippet from view home_tab3)

<li><a href="default_controller/tab_1">TAB 1</a></li>
<li><a href="default_controller/tab_2">TAB 2</a></li>
<li class="active><a href="default_controller/tab_3">TAB 3</a></li>

OK Andy.

I was showing you a way to load all your content on to one page and then just use javascript to show the tab content you want.

Do you really want people to have the page refresh when you click on a different tab?


[eluser]Andy UK[/eluser]
Ahh ok, then your answer makes more sense now. The javascript method is an option for the future once I've learnt a bit more, but I would still be interested in using a basic reload for now. Can you help with with passing a variable from a view to the default controller?

Thanks for your time!

You would just pass a var in the url. ie,

<li class="&lt;?=($selected == 'tab_1') ? 'active' : ''?&gt;"><a href="site_url() .tab_1">TAB 1</a></li>

Then in the index method you could pick up the parameter ie,

function index($tab = '')
$data['selected'] = ($tab) ? $tab : 'tab_1;

Have fun


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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