Greetings I am not quite sure how to fulfill my vision towards in my project.
Starting off, lets just concentrate on the Order controller.
So my order consists of many subviews. For example, details, history, address, attached files, notes etc. I put them on a jQuery tab interface.
Let's show some code.
Layout.php
<div class="page-content">
<div class="row">
<div class="col-md-12">
<ul class="nav nav-tabs responsive hidden-xs hidden-sm">
<li class="<?php echo ($activeTabSub === "1") ? "active" : ""; ?>">
<a href="<?PHP echo base_url();?>x/y/<?PHP echo $zgl; ?>">Master details</a>
</li>
<li class="<?php echo ($activeTabSub === "2") ? "active" : ""; ?>">
<a href="<?PHP echo base_url();?>x/y/<?PHP echo $zgl; ?>">History</a>
</li>
<li class="<?php echo ($activeTabSub === "wyk") ? "active" : ""; ?>">
<a href="<?PHP echo base_url();?>x/y/<?PHP echo $zgl; ?>">Address</a>
</li>
<li class="<?php echo ($activeTabSub === "3") ? "active" : ""; ?>">
<a href="<?PHP echo base_url();?>x/y/<?PHP echo $zgl; ?>">Files</a>
</li>
<li class="<?php echo ($activeTabSub === "4") ? "active" : ""; ?>">
<a href="<?PHP echo base_url();?>x/y/<?PHP echo $zgl; ?>">Notes</a>
</li>
<li class="<?php echo ($activeTabSub === "7") ? "active" : ""; ?>">
<a href="<?PHP echo base_url();?>x/y/<?PHP echo $zgl; ?>">ETC</a>
</li>
<li class="<?php echo ($activeTabSub === "8") ? "active" : ""; ?>">
<a href="<?PHP echo base_url();?>x/y/<?PHP echo $zgl; ?>">ETC</a>
</li>
<li class="<?php echo ($activeTabSub === "9") ? "active" : ""; ?>"><a href="<?PHP echo base_url();?>x/y/<?PHP echo $zgl; ?>">ETC</a>
</li>
</ul>
</div>
</div>
<?PHP $this->load->view($widget,$activeTabSub); ?>
<?PHP $this->load->view($main_content,$activeTabSub); ?>
</div>
In my example, the $widget view is a subview which is a widget with tools, like different edit/add buttons for each individual activeTabSub. The $main_content is a html view for each individual.
And now for the controller [Order]
public function functiontolink1()
{
$data['main_content'] = 'x/y';
$data['widget'] = 'x/widget';
$data['heading'] = "";
$data['activeTab'] = 'pzlec';
$data['activeTabSub'] = '2';
$data['strona'] = 'text';
$data['dzial'] = 'text';
$seg= $this->uri->segment(3);
$data['zgl'] = $seg;
if(isset($seg)){
$data['pracownicy'] = $this->Zlecenia_model->pracownicy($zgloszenie);
switch($this->userGroup){
case 1 :
$data['main_content'] = 'x/y';
break;
case 2 :
$data['main_content'] = 'x/y';
break;
case 3 :
$data['widget'] = 'Client/x/widget';
$data['main_content'] = 'Client/x/y';
break;
default: show_404(); break;
}
$this->load->view('Client/x/layout',$data);
}else{
show_404();
}
}
public function functiontolink2()
{
$data['widget'] = 'x/widget';
$data['heading'] = "";
$data['activeTab'] = 'pzlec';
$data['activeTabSub'] = '2';
$data['strona'] = 'text';
$data['dzial'] = 'text';
$seg= $this->uri->segment(3);
if(isset($seg)){
$data['zgl'] = $seg;
$data['json'] = $this->Zlecenia_model->getSingle($zgloszenie);
$vvv= json_decode($data['json']);
$data['client'] = $this->Order_model->getclient($vvv[0]->klient);
switch($this->userGroup){
case 1 :
$data['main_content'] = 'x/y';
break;
case 2 :
$data['main_content'] = 'x/y';
break;
case 3 :
$data['widget'] = 'Client/x/widget';
$data['main_content'] = 'Client/x/y';
break;
default: show_404(); break;
}
$this->load->view('Client/x/layout',$data);
$this->load->view('zgloszenie/layout',$data);
}else{
show_404();
}
}
As you can imagine, I have 9 of these view functions for each link. The problem starts when I would like to have some data i.e Client data all across these 9 subviews - only called once not every page hit [ tab ]. I am not quite sure if my design attempt is good at all. Do you have some suggestions?
Here, I will show you how to present multiple view controllers in a single page. Let's start. Create Xamarin iOS Single View Application by navigating to Visual Studio File Menu >> New Project >> In the dialog window >> select left plane App under iOS and center plane select Single View Application and click Next.
If the user taps an item in the master view controller, the item's details are shown in the detail view controller. The master and detail view controllers are both child view controllers of the UISplitViewController instance.
In CodeIgniter, multiple views embedded in the single view either by controller or view. 1. Create Multiple Views Creating multiple views. Create header_view.php file in application/views/ directory. Create footer_view.php file in application/views/ directory. 2. With View
With Controller Using $this->load->view () method to load multiple views and define in the order you want to display on the screen in the controller. Create home_view.php file in application/views directory.
ok - preface this by disclaimer that coding style is personal, and some people will not agree at all with what i'm going to suggest.
my impression of what you are doing - is that you are trying to do everything in either the controller or the view. right here this code in your controller
$data['main_content'] = 'x/y';
$data['widget'] = 'x/widget';
$data['heading'] = "";
$data['activeTab'] = 'pzlec';
$data['activeTabSub'] = '2';
$data['strona'] = 'text';
$data['dzial'] = 'text';
this is way too specific. a controller should be a high level boss - that you do not bother with the low level details. "Get me this from the database - did it come back? if yes then do this. if you not then do that."
put another way - the low level details are going to change constantly. you have to plan for those changes. and you do not want to change your controllers constantly because any error will cause your application to go down.
so thats what Models are for. again people will argue with this and say no it should be Libraries or Helpers or whatever. the point is that you want to isolate these low level details and you want to put them in a place where it makes sense. so six months from now you can look at the file names and have a decent idea where you need to go to make updates. its better to have 10 models with specific names and specific tasks, then a few super models that are trying to do too much.
and speaking of trying to do too much - refactor those controller methods so that they are not trying to do so many different things. for example why would you be pulling in a bunch of data if you are going to show a 404 because $seg is not there? checking if $seg is valid should be the first task. if its valid then go to the next method. and if its not valid then don't show a 404 - show a polite page that is specific to the error. then when it happens you have a chance to figure out what the issue was.
finally i would suggest - make your layout template completely neutral - in other words no html code. it just calls other views. again what does that do? it pushes the low level details - like html and css layout code - to their own files. the layout template just calls the layout views. so then when you need to make changes, its clear where to go and if you make an error you don't suddenly bring your template down - you have confined it to a view file, which will be much easier to deal with.
if your tab menu is working you could buffer all your views into one. I assume you are using bootstrap from your sample code. Here is how they suggest you write your markup for the tabs. http://getbootstrap.com/javascript/#tabs-examples Some javascript configuration may be required to get them working, so you will need to read the docs carefully. I'm not a bootstrap guy myself, so I can offer little help with that.
<ul class="nav nav-tabs" role='tablist'>
<li role="presentation" class='active'>
<a href='#widget1' data-toggle="tab">widget1</a>
</li>
<li role="presentation">
<a href='#widget2' data-toggle="tab">widget2</a>
</li>
</ul>
<section class='tab-content'>
<div class='tab-pane active' id='widget1'>
<?php $this->load->view($widget1); ?>
</div>
<div class='tab-pane' id='widget2'>
<?php $this->load->view($widget2); ?>
</div>
</section>
To build a master template to wrap around all your views, you would want to extend the CI_Controller class and simply set a property called template, that will point to your template
class MY_Controller extends CI_Controller
{
public $template;
public function __construct(){
parent::__construct();
// set the template in the constructor
// as this is where you should assign variables
// If you had an admin controller, you would create an admin controller
// extending this one, and override the $template variable.
$this->template = 'template/index' // views/template/index.php
}
}
views/template/index.php This is your master view we created by extending CI_Controller
<html>
<head></head>
<body>
<?php
// load "view" variable ANY controller sends us
$this->load->view($view);
?>
</body>
</html>
class Controller extends MY_Controller //extending the MY_Controller
{
public function index()
{
$data = array('username'=>'stackoverflow');
return $this->load->view($this->template, array(
'view' => 'your_main_view_with_tab_menu',
// the next two views are buffered as a string
// so you can easily inject them into your main tab menu view
// by setting the third paramter as true
'widget1' => $this->load->view('widget1_view', array('data' => $data), true),
'widget2' => $this->load->view('widget2_view', array('data' => $data), true)
));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With