• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't understand how to use the view

#1
[eluser]miauksius[/eluser]
Hi everyone,

I have a problem, I don't understand, when and how do I have to use the view. Before starting coding php oop (i mean withoust using classes), i was coding my pages like this:

Code:
<?php

$id = $_GET [ 'id' ];

if ( $last_update_date == $today_date )
{
    echo 'The page was updated today. YAY!
    <br />
    ----
    <br />';
}

if ( $id == '' )
{
    echo 'Hi, this is the main page';
}

if ( $id == 'about_me' )
{
    echo 'This page contains information about me.
    <br />
    Your requests:
    <br />
    [Input and link to a page where data from input is written to a file]';
}

?&gt;

Can you please explain me, or give me a link to some tutorial that would explain how to fit that output to a view?

Thank you.

P.S. Sorry for the broken english :red:

#2
[eluser]GSV Sleeper Service[/eluser]
this diagram should help you understand when and how to use views.
http://ellislab.com/codeigniter/user-gui...pflow.html

to fit that output to a view you will need a controller and a view.
One important concept to understand when using CI is that by default $_GET no longer exists, you pass data from the URL using the URI class, or by using parameters for your controller methods.
quick and dirty example
Code:
// controllers/foo.php
&lt;?php
class Foo extends Controller {
    
    public function __construct()
    {
        parent::__construct();
    }
    
    public function test($id = null)
    {
        $data[id] = $id;
        $data[last_update_date] = time();
        $data[today_date] = time();
        
        $this->load->view('testview', $data);
    }
    
}
?&gt;
// views/testview.php
&lt;?php
if ( $last_update_date == $today_date )
{
    echo 'The page was updated today. YAY!
    <br />
    ----
    <br />';
}

if ( $id == '' )
{
    echo 'Hi, this is the main page';
}

if ( $id == 'about_me' )
{
    echo 'This page contains information about me.
    <br />
    Your requests:
    <br />
    [Input and link to a page where data from input is written to a file]';
}
?&gt;
you'd then call this by calling eg domain.com/foo/test/about_me

read the manual, then read it again, go outside for a bit, then read the manual once more

#3
[eluser]miauksius[/eluser]
OK, that was easy enough to understand, thanks Smile but one more example (this version is without classes), let's say I have a guestbook.php file, and it contains something like that:
Code:
&lt;?php

$id = $_GET [ 'id' ];

if ( $id == '')
{
   //link to page for posting a comment
   //messages, pagination, etc is in this id
}

if ( $id == 'post' )
{
    echo 'You are in the post id
    <br />';

    if ( strlen ( $_POST [ 'comment' ] ) > 0 )
    {
        //writing post to db
    }
    else
    {
        //input
    }
}

?&gt;

How should the "post" id look in the view? Should I create two views: one with inputs and other to display result? Or somehow I could fit these two to one view by passing them different data to display (or maybe that's impossible?)? I have readed the manual several times, studyed a few projects which were coded on CI, but for me as a newbie it's quite hard to understand how to use it wisely, that the code would be very tidy. I know how to get sent data by user, today I made myself a guestbook on CI, but the code is very messy Sad

#4
[eluser]Johan André[/eluser]
Dude! This is covered in the screencast on the codeigniter site and in the userguide.

Codeigniter favors the MVC-pattern. Read up on MVC the userguide. It's really not that hard.
There are alot of tutorials spread over the net too...

Basics:

Controller handles logic
Models handles data
Views handles the output to screen


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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