CodeIgniter View & Models & Controller, codeigniter_php Tutorial

Source: Internet
Author: User
Tags php basics php error php foreach uppercase letter codeigniter

CodeIgniter View & models & Controllers, CodeIgniter


--------------------------------------------------------------------------------------------------------

Load view

$this->load->view (' home/name '); You can store views with subfolders, the default view file ends with '. php '

Load multiple views

$data [' title '] = ' chenwei '; Add Dynamic Data to a view

$data [' message '] = ' Your message ';

$this->load->view (' header ', $data); When loading multiple views at once, you only need to pass in the data in the first view (header view displays title, content view displays a message)

$this->load->view (' menu ');

$this->load->view (' content ');

$this->load->view (' footer ');

Examples of using objects:

$data = new SomeClass ();

$this->load->view (' Blogview ', $data);

Variables in the View file

  <?php echo $title;?>

  

Creating loops

Class Blog extends ci_controller{

Function index ()

{

$data [' todo_list '] = Array (' Clean house ', ' Call Mom ', ' run errands ');

$data [' title '] = ' my real title ';

$data [' heading '] = ' my real heading ';

      

$this->load->view (' Blogview ', $data);

}

}

  <?php echo $title;?>

  

  

          

          

    •     

        

Get view content (assign to a variable)

$buffer = $this->load->view (' Blogview ', $data, true);

The third optional parameter of the view function can change the behavior of the function. If you set the view third argument to True (Boolean), the function returns data. The default behavior of the view function is false, which sends data to the browser. If you want to return data, remember to assign it to a variable.

@ Black-eyed poet: reference user manual PHP alternative syntax

PHP Alternative syntax for view files =

config/config.php Open $config[' rewrite_short_tags '), then if your server does not support the short tag, CodeIgniter will rewrite all the short tokens.

Note: If you use this feature, if a PHP error occurs in your view file, the error message and line number will not be displayed accurately. Instead, all errors are displayed as eval () errors.

Normal echo form:

Use alternate syntax:

Alternative control structures

  

          

          

    •     

        

Note: There are no curly braces. Instead, the closing curly braces are replaced with Endforeach. Each control structure listed above also has similar closing syntax: EndIf, ENDfor, Endforeach and Endwhile, and take care not to use semicolons after each structure (except for the last one), with colons!

  

    

Hi Chenwei.

  

    

Hi Joe

  

    

Hi Unknow User

  

---------------------------------------- ----------------------------------------

Model class files are stored in the Application/models directory, of course, can also create sub-directories for large-scale project development management.

Basic Model Classes

1. The first letter of the class name must be uppercase, the other letter lowercase, to ensure that the basic model class Ci_model, the file name is a lowercase form of the model class name.

2. The model can be referenced in the controller.

such as: $this->load->model (' User_model '); or $this->load->model (' Home/user_model ');

Once the model is loaded it can be used, and by default the model name is directly referenced as the object name.

such as: $this->user_model->function ();

Of course, you can rename the object name by specifying a second parameter in the Load model function.

such as: $this->load->model (' User_model ', ' fubar ');

$this->fubar->function ();

Auto Load Model

If you need a particular model to work throughout the project, you can have the CI automatically loaded at initialization time by adding the model to the auto-mount array of the application/config/autoload.php file.

Connecting to a database

The database is not automatically connected when the model is loaded, and the following methods enable you to connect to the database.

1. Standard method to connect to a database

2. Set the third parameter to true to have the model load function automatically connect to the database

$this->load->model (' User_model ', ', TRUE);

3. Manually set a third parameter to load your custom database configuration

$config [' hostname '] = ' localhost ';

$config [' username '] = ' root ';

$config [' password '] = ' root ';

$config [' database '] = ' test ';

$config [' dbdriver '] = ' mysql ';

$config [' dbprefix '] = ';

$config [' pconnect '] = FALSE;

$config [' db_debug '] = TRUE;

  

$this->load->model (' User_model ', ', $config);

Note: When you automatically connect to the database and manually connect to the database, the memory consumption is the same.

Complete Example:

Class User_model extends ci_model{

var $title = ';

var $connect = ';

var $data = ';

function __construct ()

{

Parent::__construct ();

}

function Get_last_ten_entries ()

{

$query = $this->db->get (' entries ', 10);

return $query->result ();

}

function Insert_entry ()

{

$this->title = $this->input->post (' title '); Receive post-submitted data, using the input class

$this->content = $this->input->post (' content ');

$this->date = time ();

$this->db->insert (' entries ', $this);

}

function Update_entry ()

{

$this->title = $this->input->post (' title ');

$this->content = $this->input->post (' content ');

$this->date = time ();

$this->db->update (' entries ', $this, array (' id ' = = $this->input->post (' id ')));

}

}

The function used above is the Active Record database function

-----------------------------------------------------------------------------------------------------

Controller files are generally saved in the application/controllers/folder:

Default URL routing configuration $config [' uri_protocol '] = ' AUTO '; Default is PathInfo mode, optional

Note: The class name must begin with an uppercase letter, and the first letter lowercase is invalid.

The basic controller class

Class Blog extends ci_controller{

Public Function __construct ()

{

Parent::__construct ();

The constructor does not return a value, but it can be used to set some default functionality. Make sure that your controller extends from the parent controller class so that it can inherit all of its methods.

}

Public Function Index ()

{

Echo ' Hello world! ';

}

Public Function Comments ()

{

$this->load->view (' comment ');

}

}

Using example.com/index.php/blog/comments to access the comments method

Defining a default Controller

application/config/routes.php in $route [' default_controller '] = ' Blog ';

Put the controller in a sub-folder

Create a new directory under the Application/controllers directory and put it in the controller. Note: If you want to use a feature under a subfolder, make sure that the first fragment of the URI is used to describe the folder. Application/index.php/home/blog/comments/123

Private methods:

Private Function _test ()

{

return $variable = ' AAA '; Even if you do not add a modifier to the private, as long as the method name preceded by an underscore (_) prefix, that is, a private method, cannot be accessed through a URL.

}

Reserved method Name:

The controller class name cannot be index, such as Class Index extends ci_controller{}, because Index is the default method name for the CI, which is contained within the reserved word, with the specific reference reserved word.

To redefine the calling rule for a method:

_remap ();

Processing output:

_output (); Refer to the output class in detail.

----------------------------------------------------------------------------------------------


The frameset Problem of solving CodeIgniter

Man, first of all, the server program and the page program understand clearly. Frame is the page code, and CI does not matter, if you want to control the src of a frame, first to assign a value to the frame's Name property, such as
Control his src,link</a>.

What is CodeIgniter?

CodeIgniter is a toolkit for people who write Web applications in PHP. Its goal is to enable you to develop projects faster than you can write code from scratch, and for this reason, CI provides a rich set of class libraries to meet the usual task requirements, and provides a simple interface and logical structure to invoke these libraries. CodeIgniter can minimize the amount of task code that needs to be done, so you can put more effort into project development.
CodeIgniter is free.
CodeIgniter is licensed by the Apache/bsd-style Open source license and can be used if you wish. Read the license agreement to get more information.
CodeIgniter is lightweight.
The real lightweight. Our core system requires only a few very small libraries, which are the exact opposite of those that require more resources. The additional library files are loaded only at the request, so the core system is very fast and light, depending on the requirements.
CodeIgniter is fast.
The speed is very fast. It should be difficult to find a framework that is better than CodeIgniter.
CodeIgniter using the M-V-C model
CodeIgniter uses the model-view-Controller (Controllers) approach to better decouple the presentation layer from the logical layer. This is very useful for the project's template designer, which minimizes the amount of program code in the template. We have introduced this in more detail in the respective pages of MVC.
CodeIgniter Generate a clean URL
The URLs generated by CodeIgniter are very clean and friendly to search engines. Unlike the standard string query method, CodeIgniter uses a segment-based approach:
example.com/news/article/345 Note: The index.php file is included by default in the URL, but you can change this setting by changing the. htaccess file.
CodeIgniter features Powerful
CodeIgniter has a full range of class libraries that can perform most of the network development tasks typically required, including reading databases, sending e-mails, confirming data, saving sessions, manipulating images, and supporting XML-RPC data transfer.
The CodeIgniter is extensible
This system can be easily extended by custom class libraries, helper functions, or by extension classes, system hooks, and so on.
CodeIgniter does not require a template engine
Although CodeIgniter does bring an optional template parser program, it does not require that you have to use a template. The template engine is completely inconsistent with the performance requirements of the localized PHP code, and using the template engine we have to learn its syntax, which is at least a little easier than learning PHP basics. Consider the following PHP code: <>
<><><>
{foreach from= $addressbook item=name}
<><>
{/foreach}</ul Indeed, the code for the template engine in the example is clearer, but this poses a performance problem because the pseudo-code must first be converted to PHP to run. Our goal is to maximize performance
, so we chose not to use a dedicated template engine.
CodeIgniter has been thoroughly documented
Programmers like to write code and hate to write documents. Of course we are the same, but since the document is as important as the code itself, we are going to finish it. Moreover, our code resources are extremely clean and easy to annotate.
CodeIgniter has a friendly user community
You can be in our community forum ... Remaining full text >>

http://www.bkjia.com/PHPjc/851761.html www.bkjia.com true http://www.bkjia.com/PHPjc/851761.html techarticle CodeIgniter View model controller, CodeIgniter---------------------------------------------------------------------------- ----------------------------load View $this-load-vi ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.