CodeIgniter User Guide Basic Tutorial Summary

Source: Internet
Author: User
Tags php foreach codeigniter

CI Application :

    1. Index.php as the front-end controller, initializes the basic resources required to run the CodeIgniter.
    2. Router checks the HTTP request to determine who will handle the request.
    3. If the cache file exists, it bypasses the usual order of system execution and is sent directly to the browser.
    4. Security. The HTTP request and any user-submitted data will be filtered before the application controller (application controllers) is loaded.
    5. The controller loads the model, the core library, the auxiliary functions, and any other resources required to process a particular request.
    6. The final view renders the content that is sent to the Web browser. If the cache is turned on (Caching), the view is first cached, so it will be available for future requests.

Model-View-controller:

    1. The model represents your data structure. In general, your model class will include the ability to remove, insert, and update your database data.
    2. Views are information that is presented to the user. A view is typically a Web page, but in CodeIgniter, a view can also be a page fragment, such as a page header or footer. It can also be an RSS page, or any other type of "page".
    3. A controller is a mediation between a model, a view, and any other resource necessary to process an HTTP request, and generates a Web page.



CI to the URL using segmented parsing, for such a url:http://example.com/news/latest/10,ci understanding such as: Http://example.com/[Controller class name/[controller method name]/[Required parameters]

First, create a new controller pages.php, placed in the application/controllers/directory, this class derives from the Ci_controller class, defines a view method, which is the center of each request of the site program, called the Super Object

 
class Pages extends CI_Controller
{
    
    function __construct()
    {
        parent::__construct();
    }

    function view($page = ‘home‘)
    {
        
    }
}

Next, make a few basic page templates, the page header and footer, respectively

header.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><?php echo $title ?> - CodeIgniter 2 Tutorial</title>
</head>
<body>
    <h1>CodeIgniter 2 Tutorial</h1>
footer.php


  <strong>&copy;2011</strong>
</body>
</html>
function view($page = ‘home‘)
{
    // Check if the view file exists
    if (!file_exists(APPPATH . "/views/pages/" . $page . ".php"))
    {
        show_404();
    }

    $data[‘title‘] = ucfirst($page);

    $this->load->view(‘templates/header‘, $data);
    $this->load->view(‘pages/‘ . $page, $data);
    $this->load->view(‘templates/footer‘, $data);
}
You can see the effect through http://localhost:8080/studyci/1/index.php/pages/view/home. In this URL, index.php is the entry file, pages is the controller file, and view is the method name. Home is the parameter value

The operation of the database is not done in the control class, but in the data model, so that it can be easily used repeatedly. The data model is where the database or other data storage methods are retrieved, inserted, and updated. New models_model.php under the models/ directory, CI stipulates that the file name of the model must be added _model, the class name is the same as the file name and the first letter of the class name must be capitalized.
class News_model extends CI_Model
{
    
    function __construct()
    {
        $this->load->database();
    }
}
Create a local database with the following script


CREATE TABLE news (
  id int(11) NOT NULL AUTO_INCREMENT,
  title varchar(128) NOT NULL,
  slug varchar(128) NOT NULL,
  text text NOT NULL,
  PRIMARY KEY (id),
  KEY slug (slug)
);


Add a few test data to the database
Next, add a query code to the model file to get the eligible news.

function get_news($slug = FALSE)
{
    if ($slug === FALSE) 
    {
        $query = $this->db->get(‘news‘);
        return $query->result_array();
    }

    $query = $this->db->get_where(‘news‘, array(‘slug‘ => $slug));
    return $query->row_array();
}
Added view index.php to display the news list


<?php foreach ($news as $item): ?>
    <h2><?php echo $item[‘title‘]; ?></h2>
    <div class="main">
        <?php echo $item[‘text‘]; ?>
    </div>
    <p>
        <a href="http://localhost:8080/studyci/1/index.php/news/view/<?php echo $item[‘slug‘]; ?>">
            View article
        </a>
    </p>
<?php endforeach ?>
Added view.php to show news details


<?php
    echo "<h2>{$news_item[‘title‘]}</h2>";
    echo $news_item[‘text‘] . "<br />";
?>


After preparing the model and view, add the news controller, which contains two functions, index and view, which jump to two views respectively.

function index()
{
    $data[‘news‘] = $this->news_model->get_news();
    $data[‘title‘] = ‘News archive‘;

    $this->load->view(‘templates/header‘, $data);
    $this->load->view(‘news/index‘, $data);
    $this->load->view(‘templates/footer‘, $data);
}

function view($slug)
{
    $data[‘news_item‘] = $this->news_model->get_news($slug);

    if (empty($data[‘news_item‘])) 
    {
        show_404();
    }

    $data[‘title‘] = $data[‘news_item‘][‘title‘];

    $this->load->view(‘templates/header‘, $data);
    $this->load->view(‘news/view‘, $data);
    $this->load->view(‘templates/footer‘, $data);
}
At this point, a simple MVC pattern is implemented.

The following code shows how to insert a record with a form, first create a form to enter data /views/news/create.php
<h2>Create a news item</h2>
<?php echo validation_errors(); ?>
<?php echo form_open(‘news/create‘) ?>
    <label for="title">Title</label> 
    <input type="input" name="title" /><br />
    <label for="text">Text</label>
    <textarea name="text"></textarea><br />
    <input type="submit" name="submit" value="Create news item" /> 
</form>
Where validation_errors() provides form validation, form_open() can call controller functions

Then create a new view /views/news/success.php that is displayed when the input is successful.
<?php
    echo "Add news success<br />";
?>


News controller new create function

public function create()
{
    $this->load->helper(‘form‘);
    $this->load->library(‘form_validation‘);

    $data[‘title‘] = ‘Create a news item‘;

    $this->form_validation->set_rules(‘title‘, ‘Title‘, ‘required‘);
    $this->form_validation->set_rules(‘text‘, ‘text‘, ‘required‘);

    if ($this->form_validation->run() === FALSE)
    {
        $this->load->view(‘templates/header‘, $data);  
        $this->load->view(‘news/create‘);
         $this->load->view(‘templates/footer‘);
    }
    else
    {
        $this->news_model->set_news();
        $this->load->view(‘templates/header‘, $data); 
        $this->load->view(‘news/success‘);
        $this->load->view(‘templates/footer‘);
    }
}
When the insert record fails, the create view is redisplayed, and the insert successfully displays the success view.

Finally, add the set_news() method in the model.
public function set_news()
{
    $this->load->helper(‘url‘);

    $slug = url_title($this->input->post(‘title‘), ‘dash‘, TRUE);

    $data = array(
        ‘title‘ => $this->input->post(‘title‘),
        ‘slug‘ => $slug,
        ‘text‘ => $this->input->post(‘text‘)
    );

    return $this->db->insert(‘news‘, $data);
}
The insert function is complete, you can access the new page through http://localhost:8080/studyci/1/index.php/news/create


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.