summaryrefslogtreecommitdiffstats
path: root/user_guide_src/source/tutorial
diff options
context:
space:
mode:
Diffstat (limited to 'user_guide_src/source/tutorial')
-rw-r--r--user_guide_src/source/tutorial/create_news_items.rst2
-rw-r--r--user_guide_src/source/tutorial/index.rst2
-rw-r--r--user_guide_src/source/tutorial/news_section.rst176
3 files changed, 90 insertions, 90 deletions
diff --git a/user_guide_src/source/tutorial/create_news_items.rst b/user_guide_src/source/tutorial/create_news_items.rst
index 794b67eed..bfaf13537 100644
--- a/user_guide_src/source/tutorial/create_news_items.rst
+++ b/user_guide_src/source/tutorial/create_news_items.rst
@@ -94,7 +94,7 @@ Model
-----
The only thing that remains is writing a method that writes the data to
-the database. You'll use the Active Record class to insert the
+the database. You'll use the Query Builder class to insert the
information and use the input library to get the posted data. Open up
the model created earlier and add the following:
diff --git a/user_guide_src/source/tutorial/index.rst b/user_guide_src/source/tutorial/index.rst
index c959d04d2..b1ab331d1 100644
--- a/user_guide_src/source/tutorial/index.rst
+++ b/user_guide_src/source/tutorial/index.rst
@@ -16,7 +16,7 @@ This tutorial will primarily focus on:
- Model-View-Controller basics
- Routing basics
- Form validation
-- Performing basic database queries using "Active Record"
+- Performing basic database queries using "Query Builder"
The entire tutorial is split up over several pages, each explaining a
small part of the functionality of the CodeIgniter framework. You'll go
diff --git a/user_guide_src/source/tutorial/news_section.rst b/user_guide_src/source/tutorial/news_section.rst
index 38e4214ca..b64ea2aae 100644
--- a/user_guide_src/source/tutorial/news_section.rst
+++ b/user_guide_src/source/tutorial/news_section.rst
@@ -22,19 +22,19 @@ your database properly as described
::
- <?php
- class News_model extends CI_Model {
+ <?php
+ class News_model extends CI_Model {
- public function __construct()
- {
- $this->load->database();
- }
- }
+ public function __construct()
+ {
+ $this->load->database();
+ }
+ }
This code looks similar to the controller code that was used earlier. It
-creates a new model by extending CI\_Model and loads the database
+creates a new model by extending ``CI_Model`` and loads the database
library. This will make the database class available through the
-$this->db object.
+``$this->db`` object.
Before querying the database, a database schema has to be created.
Connect to your database and run the SQL command below. Also add some
@@ -42,41 +42,41 @@ seed records.
::
- 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)
- );
+ 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)
+ );
Now that the database and a model have been set up, you'll need a method
to get all of our posts from our database. To do this, the database
abstraction layer that is included with CodeIgniter — `Active
-Record <../database/active_record.html>`_ — is used. This makes it
+Record <../database/query_builder.html>`_ — is used. This makes it
possible to write your 'queries' once and make them work on `all
supported database systems <../general/requirements.html>`_. Add the
following code to your model.
::
- public function get_news($slug = FALSE)
- {
- if ($slug === FALSE)
- {
- $query = $this->db->get('news');
- return $query->result_array();
- }
+ public 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();
- }
+ $query = $this->db->get_where('news', array('slug' => $slug));
+ return $query->row_array();
+ }
With this code you can perform two different queries. You can get all
news records, or get a news item by its `slug <#>`_. You might have
noticed that the $slug variable wasn't sanitized before running the
-query; Active Record does this for you.
+query; :doc:`Query Builder <../database/query_builder>` does this for you.
Display the news
----------------
@@ -89,30 +89,30 @@ application/controllers/news.php.
::
- <?php
- class News extends CI_Controller {
+ <?php
+ class News extends CI_Controller {
- public function __construct()
- {
- parent::__construct();
- $this->load->model('news_model');
- }
+ public function __construct()
+ {
+ parent::__construct();
+ $this->load->model('news_model');
+ }
- public function index()
- {
- $data['news'] = $this->news_model->get_news();
- }
+ public function index()
+ {
+ $data['news'] = $this->news_model->get_news();
+ }
- public function view($slug)
- {
- $data['news'] = $this->news_model->get_news($slug);
- }
- }
+ public function view($slug = NULL)
+ {
+ $data['news_item'] = $this->news_model->get_news($slug);
+ }
+ }
Looking at the code, you may see some similarity with the files we
-created earlier. First, the "\_\_construct" method: it calls the
-constructor of its parent class (CI\_Controller) and loads the model, so
-it can be used in all other methods in this controller.
+created earlier. First, the ``__construct()`` method: it calls the
+constructor of its parent class (``CI_Controller``) and loads the model,
+so it can be used in all other methods in this controller.
Next, there are two methods to view all news items and one for a
specific news item. You can see that the $slug variable is passed to the
@@ -125,15 +125,15 @@ the views.
::
- public function index()
- {
- $data['news'] = $this->news_model->get_news();
- $data['title'] = 'News archive';
+ public 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');
- }
+ $this->load->view('templates/header', $data);
+ $this->load->view('news/index', $data);
+ $this->load->view('templates/footer');
+ }
The code above gets all news records from the model and assigns it to a
variable. The value for the title is also assigned to the $data['title']
@@ -143,20 +143,20 @@ and add the next piece of code.
::
- <?php foreach ($news as $news_item): ?>
+ <?php foreach ($news as $news_item): ?>
- <h2><?php echo $news_item['title'] ?></h2>
- <div id="main">
- <?php echo $news_item['text'] ?>
- </div>
- <p><a href="<?php echo $news_item['slug'] ?>">View article</a></p>
+ <h2><?php echo $news_item['title'] ?></h2>
+ <div id="main">
+ <?php echo $news_item['text'] ?>
+ </div>
+ <p><a href="<?php echo $news_item['slug'] ?>">View article</a></p>
- <?php endforeach ?>
+ <?php endforeach ?>
Here, each news item is looped and displayed to the user. You can see we
wrote our template in PHP mixed with HTML. If you prefer to use a
template language, you can use CodeIgniter's `Template
-Parser <../libraries/parser.html>`_ class or a third party parser.
+Parser <../libraries/parser>`_ class or a third party parser.
The news overview page is now done, but a page to display individual
news items is still absent. The model created earlier is made in such
@@ -166,32 +166,32 @@ news controller and add the following lines to the file.
::
- public function view($slug)
- {
- $data['news_item'] = $this->news_model->get_news($slug);
+ public function view($slug = NULL)
+ {
+ $data['news_item'] = $this->news_model->get_news($slug);
- if (empty($data['news_item']))
- {
- show_404();
- }
+ if (empty($data['news_item']))
+ {
+ show_404();
+ }
- $data['title'] = $data['news_item']['title'];
+ $data['title'] = $data['news_item']['title'];
- $this->load->view('templates/header', $data);
- $this->load->view('news/view', $data);
- $this->load->view('templates/footer');
- }
+ $this->load->view('templates/header', $data);
+ $this->load->view('news/view', $data);
+ $this->load->view('templates/footer');
+ }
-Instead of calling the get\_news() method without a parameter, the $slug
-variable is passed, so it will return the specific news item. The only
-things left to do is create the corresponding view at
-application/views/news/view.php. Put the following code in this file.
+Instead of calling the ``get_news()`` method without a parameter, the
+``$slug`` variable is passed, so it will return the specific news item.
+The only things left to do is create the corresponding view at
+*application/views/news/view.php*. Put the following code in this file.
::
- <?php
- echo '<h2>'.$news_item['title'].'</h2>';
- echo $news_item['text'];
+ <?php
+ echo '<h2>'.$news_item['title'].'</h2>';
+ echo $news_item['text'];
Routing
-------
@@ -205,10 +205,10 @@ a slug to the view method in the news controller.
::
- $route['news/(:any)'] = 'news/view/$1';
- $route['news'] = 'news';
- $route['(:any)'] = 'pages/view/$1';
- $route['default_controller'] = 'pages/view';
+ $route['news/(:any)'] = 'news/view/$1';
+ $route['news'] = 'news';
+ $route['(:any)'] = 'pages/view/$1';
+ $route['default_controller'] = 'pages/view';
Point your browser to your document root, followed by index.php/news and
-watch your news page.
+watch your news page. \ No newline at end of file