Welcome Guest, Not a member yet? Register   Sign In
Finding it so hard to understand how to start.
#1

[eluser]SteveBluck[/eluser]
Hey everyone,

I have read everything in the user guide and also many forum topics to help me get started.
I'm not a PHP guru and am still learning a lot as I go. All i'm doing is trying to create a dynamic website with two different user interfaces, one for public users and one for registered users.

Now, I created all this without hassle but after I finished, I looked at my code and decided it was incredibly messy and so much was repeated, especially loading view code.

I am using the correct MVC approach (I think). My models handle any database querys, my controllers filter any form data and pass variables to the view and my view just displays variables.

My code is constantly repeating the following:
Code:
$this->load->view('header_view', $data);
$this->load->view('home_view', $data);  
$this->load->view('footer_view', $data);
Code:
$this->load->view('header_view', $data);
$this->load->view('register_view', $data);  
$this->load->view('footer_view', $data);
Code:
$this->load->view('header_view', $data);
$this->load->view('login_view', $data);  
$this->load->view('footer_view', $data);
//etc
Code:
$this->load->view('member/header_view', $data);
$this->load->view('member/home_view', $data);  
$this->load->view('member/footer_view', $data);
Code:
$this->load->view('member/header_view', $data);
$this->load->view('member/register_view', $data);  
$this->load->view('member/footer_view', $data);
Code:
$this->load->view('member/header_view', $data);
$this->load->view('member/login_view', $data);  
$this->load->view('member/footer_view', $data);
//etc

I have tried extending the Controller but can't seem to get my head around what to put in there exactly so that I don't need to repeat my code all the time.

If anyone can understand what I'm on about (I'm very frustrated at the moment Sad ), I would be very greatfull for some help.
#2

[eluser]rogierb[/eluser]
You can go about different ways, there are plenty of examples on the forum.

One way is using one template view which has a fixed header and footer and load what you need into it.
Code:
$data["content"] = $this->load->view('member/home_view', $data, true);  
$this->load->view('template/some_template_view', $data)

another way is to load header and footer in your contructor and pass that along to your view.
#3

[eluser]SteveBluck[/eluser]
[quote author="rogierb" date="1239118041"]You can go about different ways, there are plenty of examples on the forum.

One way is using one template view which has a fixed header and footer and load what you need into it.
Code:
$data["content"] = $this->load->view('member/home_view', $data, true);  
$this->load->view('template/some_template_view', $data)

another way is to load header and footer in your contructor and pass that along to your view.[/quote]

Sorry, but I don't understand what you mean. I would still need to repeat that the same amount of times I have repeated the views in my code. I need a way to load two different views (header and footer) just the once.
#4

[eluser]xwero[/eluser]
By extending the controller you have use a base page to echo the header and footer in so instead of writing
Code:
$this->load->view('header_view', $data);
$this->load->view('home_view', $data);  
$this->load->view('footer_view', $data);
You will write
Code:
$data['content'] = $this->load->view('home_view', $data,true);  
$this->load->view('layout_view', $data);
And because the header and footer are loaded before the method you can't add data to them.

A nice way is to use a post_controller hook. You gather all data you need in the controller method that gets called, using load->vars, and the hook function looks like this
Code:
function show()
{
    $ci =& get_instance();
    $ci->load->vars('header'=>$this->load->view($ci->config->item('header_view'),'',true));
    $ci->load->vars('footer'=>$this->load->view($ci->config->item('footer_view'),'',true));
    $ci->load->view($ci->config->item('layout_view'));
}
The config items are needed because you can change them and you autoload the config file with the default views.

Another way to go is to use a template/layout/partial library.
#5

[eluser]SteveBluck[/eluser]
Right so in the MY_Controller.php under the MY_Controller function, I would put this:
Code:
$this->load->view('header_view', $data);
$this->load->view('footer_view', $data);

Then in my controller I place the code you provided? How does it know to put the content in the middle of the header and footer? Thanks for your help so far but can you please speak to me like I'm a 5 year old so I know exactly what you mean.
#6

[eluser]zutis[/eluser]
When I first started with CI and came accorss this problem, this worked really well for me:

Create /application/libraries/MY_Controller.php with the following in it:
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends Controller
{
   function MY_Controller()
   {
       parent::Controller;
   }

   function my_view($real_view_file, $data = array(), $switch = false)
   {
      $header  = $this->load->view('header_view', $data, true);
      $content = $this->load->view($real_view_file, $data, true);
      $footer  = $this->load->view('footer_view', $data, true);
      $this->load->view($header . $content . $footer, $data, $switch);
   }
}

?>
then your code will look like this:
Code:
<?php
class Home extends MY_Controller
{  
    function Home(){
        parent::Controller();    
    }
    
    function index(){    
        $this->my_view('home_view', $data);
    }
?>

note the $this->my_view and not $this->load->view
#7

[eluser]SteveBluck[/eluser]
Thanks zutis, that does do the trick and I think I understand what it all means. Although I keep getting an error:

An Error Was Encountered
Unable to load the requested file:

But the filename is blank, any ideas?
#8

[eluser]jedd[/eluser]
[quote author="SteveBluck" date="1239120018"]Right so in the MY_Controller.php under the MY_Controller function, I would put this:
Code:
$this->load->view('header_view', $data);
$this->load->view('footer_view', $data);
[/quote]
Nearly correct. As noted, you have to actually assign those values to a variable.

So you'd do:
Code:
$header = $this->load->view('header_view', $data , TRUE);
In addition to noting that you have a variable $header now, take note of the TRUE third parameter to load->view. This instructs it to return the data generated by 'header_view', rather than displaying it.

Alternatively, and to save you a smidgen of time, you could do this:
Code:
$this->data['header'] = $this->load->view('header_view', $data , TRUE);

This results in $this->data array being pre-loaded with the header view content.

So, that's in the MY_Controller file.

In your actual various controllers, you'd now have $this->data['header'] available and ready to use. This means that if you load up other components of $this->data[] - the obvious example being 'content' (which is the 'main' bit of your page), you can then zap straight out to your primary view, and utilise $header.

Your $header, of course, in this case is generated once only, in MY_Controller.

Quote:Then in my controller I place the code you provided? How does it know to put the content in the middle of the header and footer? Thanks for your help so far but can you please speak to me like I'm a 5 year old so I know exactly what you mean.

Sit up straight.

How does 'it' know to put the content anywhere? 'It', of course, does not - that's a question for your view file. Your view file then looks like this:

Code:
<html>
<head>
<?php
    echo "\n". link_tag('assets/stylesheets/COMMON.css');
    echo "\n". link_tag('assets/stylesheets/'. $theme .'.css');
?>


<title>
    <?php echo $title; ?>
</title>
</head>

<body>

<?php
?>

<div id="div_everything_wrapper">

    <div id="div_topbar_wrapper">
        &lt;?php echo $top_bar_view; ?&gt;
    </div>

    <div class="break">
    </div>

    <div id="div_torso_wrapper">

        <div id="div_navigation_menu">
            &lt;?php echo $main_menu_view; ?&gt;
        </div>

        <div id="div_main_content">
            &lt;?php echo $main_content_view;  ?&gt;
        </div>

    </div>

    <div class="break">
    </div>

    <div id="div_footer_wrapper">
        <table width="100%">
            <tr width="100%">
            <td width="25%" align="left">
                <b>
                &lt;!-- Can't pre-render this, as it'll throw the reported results --&gt;
                Rendered in {elapsed_time}s, and {memory_usage}.
                </b>
            </td>
            <td width="50%" align="center">
                $other_stuff
            </td>
            <td width="25%" align="right">
                Developed using
                &lt;?php echo anchor_popup ("http://codeigniter.com/", "Code Igniter"); ?&gt;
            </td>
            </tr>
        </table>
    </div>
</div>


&lt;/body&gt;
&lt;/html&gt;

Or thereabouts. Substitute in your own CSS file, and variable names for content bits and pieces.
#9

[eluser]zutis[/eluser]
Nice.
#10

[eluser]SteveBluck[/eluser]
Hmm think I understand, sort of.

So you have a main view page? Called views/page_view.php for example. Then you echo $header, $content and $footer within that view file and not the controller?




Theme © iAndrew 2016 - Forum software by © MyBB