Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
23 views167 pages

Mid Term

The document provides an overview of web application frameworks, focusing on the Model-View-Controller (MVC) architecture and the CodeIgniter framework. It discusses the historical development of web frameworks, the roles of server-side and client-side scripting, and the features of CodeIgniter, including its lightweight design and extensive libraries. Additionally, it covers the setup and configuration of CodeIgniter, including routing and URL management.

Uploaded by

Marlo Diaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views167 pages

Mid Term

The document provides an overview of web application frameworks, focusing on the Model-View-Controller (MVC) architecture and the CodeIgniter framework. It discusses the historical development of web frameworks, the roles of server-side and client-side scripting, and the features of CodeIgniter, including its lightweight design and extensive libraries. Additionally, it covers the setup and configuration of CodeIgniter, including routing and URL management.

Uploaded by

Marlo Diaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 167

Web System Technologies

IT0049
Module 1
Introduction to MVC
• To explore and comprehend one of the fastest tool to
develop a web-based system.
• To understand the importance of using a framework.
• To familiarize student on web application framework
architecture. concepts, kind, models.
• To understand the flow of MVC.
• To introduce CodeIgniter as an MVC framework.
• To familiarize student on CodeIgniter’s features,
design and architectural goals.
• 1993, the Common Gateway Interface (CGI) standard
was introduced.

• 1995, fully integrated server/language development


environments first emerged and new web-specific
languages were introduced.

• In the late 1990s, mature, "full stack" frameworks began to


appear.
A web application framework (WAF) is a software
framework that is designed to support the development of
web applications including web services, web resources, and
web APIs.
Web frameworks provide a standard way to build and deploy
web applications on the World Wide Web.
The framework aims to alleviate the overhead associated
with common activities performed in web development.

For example, many frameworks provide libraries for database


access, templating frameworks and session management,
and they often promote code reuse.
• Model–view–controller (MVC)
• Push-based vs. pull-based
• Three-tier organization

Most web application frameworks are based on the model–


view–controller (MVC) pattern.
Frameworks are built to support the construction of internet
applications based on a single programming language,
ranging in focus from general purpose tools to native-
language programmable packages built around a specific
user application.
Web frameworks must function according to the architectural
rules of browsers and web protocols such as HTTP, which is
a stateless protocol.

• Server-side scripting
• Client-side scripting
Server-side refers to operations that are performed by the
server in a client–server relationship in a computer network.

Server-side scripting is a technique used in web development


which involves employing scripts on a web server which
produce a response customized for each user's (client's)
request to the website. The alternative is for the web server
itself to deliver a static web page.
Typically, a server is a computer application, such as a web
server, that runs on a remote server, reachable from a user's
local computer, smartphone, or other device.
In the context of the World Wide Web, commonly
encountered server-side computer languages include
• C# or Visual Basic in ASP.NET environments
• Java
• Perl
• PHP
• Python
• Ruby
• Node.js
• Swift
However, web applications and services can be implemented
in almost any language, as long as they can return data to
standards-based web browsers (possibly via intermediary
programs) in formats which they can use.
Client-side refers to operations that are performed by the
client in a client–server relationship in a computer network

Client-side scripting is the changing of interface behaviors


within a specific web page in response to mouse or keyboard
actions, or at specified timing events.
Typically, a client is a computer application, such as a web
browser, that runs on a user's local computer, smartphone,
and connects to a server as necessary.
In the context of the World Wide Web, commonly
encountered computer languages which are evaluated or run
on the client side include:

• Cascading Style Sheets (CSS)


• HTML
• JavaScript
Frameworks typically set the control flow of a program. The
common features of WAF are the following :
• Web Template System
• Caching
• Security
• Database access, mapping and configuration
• URL Mapping
• Web Services
• Web Resources
Introduction to MVC
MODEL VIEW CONTROLLER (MVC) is a software design
pattern commonly used for developing user interfaces that
divides the related program logic into three interconnected
elements.

Traditionally used for desktop graphical user interfaces


(GUIs), this pattern has become popular for designing web
applications.
• 1979 - Trygve Reenskaug introduced MVC into Smalltalk-79.
• 1980s - Jim Althoff and others implemented a version of MVC
for the Smalltalk-80 class library. Evolved into other variants
such as hierarchical model–view–controller (HMVC), model–
view–adapter (MVA), model–view–presenter (MVP), model–
view–viewmodel (MVVM).
• 1996 - The use of the MVC pattern in web applications
exploded in popularity after the introduction of NeXT's
WebObjects.
Web applications that uses the WebForm approach
commingle :
• Database Code
• Page Design Code
• Control Flow Code

Unless these elements are separated, larger applications


become difficult to maintain.
• Model – Data Management
• View – User Interface
• Controller – Orchestrates the operation
• Is a part of the application that is responsible for
retrieving data from the database.

• Converting it into objects


• User interface
• Chart, diagram or table
• Presentation of the model
• Data retrieved
• Orchestrates the operation
• Validates user input
• Calling the model
• Choosing which view to render
• Handles data to be displayed
2
model

1
5 4
controller
3
User
6 view
2
model
3
1

controller
4
User
5 view
• XPages
• Cocoa framework
• GNUstep
• GTK+
• JFace.
• Microsoft ASP.NET
• Microsoft Composite UI Application Block
• Java Swing.
• Apache Pivot.
• Adobe Flex
• Visual FoxExpress
• Crank Storyboard Suite
• Eiffel
• ABAP Objects • .NET
• ActionScript • PERL
• C++ • PHP
• CMFL • Phyton
• Flex • Ruby
• Groovy • Smalltalk
• JavaScript • XML
• Simultaneous development
• Code reuse
• Simultaneous development
• High cohesion
• Loose coupling
• Ease of modification
• Multiple views for a model
• Testability
• Code navigability
• Multi-artifact consistency
• Undermined by inevitable clustering
• Excessive boilerplate
• Pronounced learning curve
• Lack of incremental benefit
Introduction to
CodeIgniter
CodeIgniter is an open-source web framework, for creating
web sites with PHP and is loosely based on model–view–
controller (MVC) development pattern.

CodeIgniter can be also modified to use Hierarchical Model


View Controller (HMVC).
• Is an Application Framework
• Free
• Light weight
• Fast
• Uses M-V-C
• Generates clean URLs
• Extensible
• Does Not Require a Template Engine
• Thoroughly Documented
MVC is a software approach that separates application logic
from presentation. In practice, it permits your web pages to
contain minimal scripting since the presentation is separate
from the PHP scripting.
• The Model represents your data structures.
• The View is the information that is being presented to a
user.
• The Controller is the intermediary between the Model, the
View, and any other resources.
• Model-View-Controller Based System
• Extremely Light Weight
• Full Featured database classes with support for several
platforms.
• Query Builder Database Support
• Form and Data Validation
• Security and XSS Filtering
• Session Management
• Email Sending Class. Supports Attachments, HTML/Text
email, multiple protocols (sendmail, SMTP, and Mail) and
more.
• Image Manipulation Library (cropping, resizing, rotating,
etc.). Supports GD, ImageMagick, and NetPBM
• File Uploading Class
• FTP Class
• Localization
• Pagination
• Data Encryption
• Benchmarking
• Full Page Caching
• Error Logging
• Application Profiling
• Calendaring Class
• User Agent Class
• Zip Encoding Class
• Template Engine Class
• Trackback Class
• XML-RPC Library
• Unit Testing Class
• Search-engine Friendly URLs
• Flexible URI Routing
• Support for Hooks and Class Extensions
• Large library of “helper” functions
CodeIgniter’s goal is to obtain maximum performance,
capability, and flexibility.

CodeIgniter was created with the following objectives:


• Dynamic Instantiation
• Loose Coupling
• Component Singularity
Web System Technologies
IT0049
Module 2
CodeIgniter MVC
• To introduce CodeIgniter as an MVC framework.
• To familiarize student on CodeIgniter’s features,
application flow chart, concepts, design and
architectural goals.
• To install CodeIgniter.
• To learn how to manage configuration files.
• To define the structure of URI segmentation
• To understand how to manage the routing
configuration
• To implement regular expressions to create a custom
routing
• To familiarize the reserved routes in CodeIgniter
CodeIgniter is an open-source web framework, for creating
web sites with PHP and is loosely based on model–view–
controller (MVC) development pattern.

CodeIgniter can be also modified to use Hierarchical Model


View Controller (HMVC).
• Is an Application Framework
• Free
• Light weight
• Fast
• Uses M-V-C
• Generates clean URLs
• Extensible
• Does Not Require a Template Engine
• Thoroughly Documented
MVC is a software approach that separates application logic
from presentation. In practice, it permits your web pages to
contain minimal scripting since the presentation is separate
from the PHP scripting.
• The Model represents your data structures.
• The View is the information that is being presented to a
user.
• The Controller is the intermediary between the Model, the
View, and any other resources.
• Model-View-Controller Based System
• Extremely Light Weight
• Full Featured database classes with support for several
platforms.
• Query Builder Database Support
• Form and Data Validation
• Security and XSS Filtering
• Session Management
• Email Sending Class.
• Image Manipulation Library
• File Uploading Class
• FTP Class
• Localization
• Pagination
• Data Encryption
• Benchmarking
• Full Page Caching
• Error Logging
• Application Profiling
• Calendaring Class
• User Agent Class
• Zip Encoding Class
• Template Engine Class
• Trackback Class
• XML-RPC Library
• Unit Testing Class
• Search-engine Friendly URLs
• Flexible URI Routing
• Support for Hooks and Class Extensions
• Large library of “helper” functions
CodeIgniter’s goal is to obtain maximum performance,
capability, and flexibility.

CodeIgniter was created with the following objectives:


• Dynamic Instantiation
• Loose Coupling
• Component Singularity
• OS (Cross Platform)
• Web Browser
• Web Server (Apache)
• Text Editor
CodeIgniter Setup
1. Download CodeIgniter
https://codeigniter.com/userguide3/installation/downloads.html
2. Unzip the package to your web page root directory.
CodeIgniter Directory Structure
3. Open the application/config/config.php file with a text
editor and set your base URL. If you intend to use
encryption or sessions, set your encryption key.
4. If you intend to use a database, open
the application/config/database.php file with a text editor
and set your database settings.
The Config class provides a means to retrieve configuration
preferences. These preferences can come from the default
config file (application/config/config.php) or from your own
custom config files.

This class is initialized automatically.


By default, CodeIgniter has one primary config file, located at
application/config/config.php. If you open the file using
your text editor you’ll see that config items are stored in an
array called $config.
CodeIgniter automatically loads the primary config file
(application/config/config.php).

Manual Loading
To load one of your custom config files you will use the
following function within the controller that needs it:
$this->config->load('filename’);
Where filename is the name of your config file, without
the .php file extension.
CodeIgniter automatically loads the primary config file
(application/config/config.php).

Auto-loading
If you find that you need a particular config file globally,
you can have it loaded automatically by the system. To do
this, open the autoload.php file, located at
application/config/autoload.php, and add your config file as
indicated in the file.
To retrieve an item from your config file, use the following
function:
$this->config->item('item_name’);

Where item_name is the $config array index you want to


retrieve. For example, to fetch your language choice you’ll do
this:
$lang = $this->config->item('language');
If you would like to dynamically set a config item or change
an existing one, you can do so using:

$this->config->set_item('item_name', 'item_value');
$this->config->site_url();
This function retrieves the URL to your site, along with
the "index" value you've specified in the config file.
$this->config->base_url();
This function retrieves the URL to your site, plus an
optional path such as to a stylesheet or image.
$this->config->system_url();
This function retrieves the URL to your system folder.
URL and URI Segments
URLs in CodeIgniter are search-engine and human friendly.
CodeIgniter uses a segment-based approach:

example.com/news/article/my_article
The segments in the URL, in following with the Model-View-
Controller approach, usually represent:
example.com/class/function/ID/foo/bar

Segment 1 – controller class


Segment 2 – class function or method
Segment 3 and additional segments - ID and any
variables that will be passed to the controller.
By default, the index.php file will be included in your URLs:
example.com/index.php/news/article/my_article

You can easily remove this file by using a .htaccess file.


Example:

Note: These rules might not work for all server configurations.
In your config/config.php file you can specify a suffix that
will be added to all URLs generated by CodeIgniter.

For example, a default CodeIgniter URL will give you:


example.com/index.php/products/view/shoes

Optionally, you can add a suffix, such as .html :


example.com/index.php/products/view/shoes.html
In some cases you might prefer to use query strings URLs:
index.php?c=products&m=view&id=345
Optionally, CodeIgniter supports this capability, which can be
enabled in your application/config.php file.

If you change “enable_query_strings” to TRUE this feature


will become active.
URLs in CodeIgniter are search-engine and human friendly.
CodeIgniter uses a segment-based approach:

example.com/news/article/my_article
The segments in the URL, in following with the Model-View-
Controller approach, usually represent:
example.com/class/function/ID/foo/bar

Segment 1 – controller class


Segment 2 – class function or method
Segment 3 and additional segments - ID and any
variables that will be passed to the controller.
By default, the index.php file will be included in your URLs:
example.com/index.php/news/article/my_article

You can easily remove this file by using a .htaccess file.


Example:

Note: These rules might not work for all server configurations.
In your config/config.php file you can specify a suffix that
will be added to all URLs generated by CodeIgniter.

For example, a default CodeIgniter URL will give you:


example.com/index.php/products/view/shoes

Optionally, you can add a suffix, such as .html :


example.com/index.php/products/view/shoes.html
In some cases you might prefer to use query strings URLs:
index.php?c=products&m=view&id=345
Optionally, CodeIgniter supports this capability, which can be
enabled in your application/config.php file.

If you change “enable_query_strings” to TRUE this feature


will become active.
The segments in a URI normally follow this pattern:
example.com/class/function/id/

In some instances, you may want to remap this so that a


different class/method can be called instead of the one
corresponding to the URL.

Example:
example.com/product/1/
example.com/product/2/
Setting your routing rules
Routings are defined in your application/config/routes.php file
where an array $route is created, that permits you to specify
routing criteria. Routes can be expressed by wildcards or
Regular Expressions.

In a route, the array key contains the URI to be matched,


while the array value contains the destination it should be
re-routed to.
Wildcards
A typical wildcard route might look something like this:
$route['product/:num'] = 'catalog/product_lookup’;
If a string “product” is in the first segment of the URL, and followed by a
number as the second segment, “catalog” class and “product_lookup”
method are used.
Two wildcard types for segments:
(:num) a segment containing only numbers.
(:any) a segment containing any character (except for ‘/’).

Note: With respect to regular expressions, the wildcards :any being


translated to [^/]+ and :num to [0-9]+
Examples:
$route['journals'] = 'blogs’;
//a word “journals” in the first segment will be rerouted to “blogs” class.
$route['blog/joe'] = 'blogs/users/34’;
//If the segments blog/joe is found, it will be rerouted to “blogs” class and the
“users” method with ID set to “34”.
$route['product/(:any)'] = 'catalog/product_lookup’;
//A URL with “product” as the first segment, and anything in the second will be
remapped to the “catalog” class and the “product_lookup” method.
$route['product/(:num)'] = 'catalog/product_lookup_by_id/$1’;
// a word “product” as the first segment, and a number in the second will be rerouted
to the “catalog” class and “product_lookup_by_id” method passing matched number as a
variable to the method.
Regular Expressions
A typical RegEx route might look something like this:
$route['products/([a-z]+)/(\d+)'] = '$1/id_$2’;
URI “products/shirts/123” would call the “shirts” controller class and the
“id_123” method.

“products/shirts/123” -> “shirts/id_123”


If you want to learn more about Regular Expressions,
visit regular-expressions.info.
There are three reserved routes:
$route['default_controller'] = ‘controller/method’;
$route[‘default_controller’] = ’welcome/index’;
//route points to the action that should be executed if the URI contains no data.
//In the above example, it is Welcome::index() that would be called.
$route['404_override'] = ‘controller/method’;
//indicates which controller class should be loaded if the URL is not found
$route['translate_uri_dashes'] = FALSE;
// enables you to automatically replace dashes (‘-‘) with underscores in the controller
and method URI segments. If set to true URL ”product-name/category-computer” will
translate to your ”product_name” controller and “category_computer” method
Web System Technologies
IT0049
Module 3
CodeIgniter Controller
and Views
• To have an overview of Controller Class.
• To develop first application using CodeIgniter.
• To familiarize View in the context of Model View
Controller (MVC) architecture.
• To be able to create Views for presentation of User
Interface.
• To manage how the data will be displayed on the
screen.
• To simplify the creation of User Interface by
fragmentation.
Controllers are the heart of your application, as they
determine how HTTP requests should be handled.

What is a Controller?
A Controller is a class file that can be associated with a URI.
The segments in the URL, in following with the Model-View-
Controller approach, usually represent:
example.com/class/function/ID/foo/bar

Segment 1 – controller class


Segment 2 – class function or method
Segment 3 and additional segments - ID and any
variables that will be passed to the controller.
Consider the URI below:
example.com/index.php/blog/

When a controller’s name matches the first segment of a


URI, it will be loaded.
Using your text editor, create a file called Blog.php, and put
the following code in it:
<?php
class Blog extends CI_Controller {
public function index() {
echo 'Hello World!’;
}
}
Then save the file to your application/controllers/ directory.
Visit your site using the URL:
example.com/index.php/blog/

If you did it right, you should see:


Hello World!
This is valid:
<?php class Blog extends CI_Controller { }

This is not valid:


<?php class blog extends CI_Controller { }
<?php
class Blog extends CI_Controller {
public function index() {
echo 'Hello World!’;
}
}

The “index” method is always loaded by default if the second


segment of the URI is empty.
<?php
class Blog extends CI_Controller {
public function index() {
echo 'Hello World!’;
}
}

Another way to show your “Hello World” message would be :


example.com/index.php/blog/index/
Passing URI Segments to your methods:

example.com/index.php/products/shoes/sandals/123

Your method will be passed URI segments 3 and 4 (“sandals”


and “123”):
CodeIgniter can load a default controller if only your site
root URL is requested. To specify a default controller, open
your application/config/routes.php file and set this
variable:

$route['default_controller'] = 'blog’;

‘blog’ - the name of the default controller you want to be


loaded.
Constructors are useful if you need to set some default
values or run a default process when your class is
instantiated.

Constructors can’t return a value, but they can do some


default work.
If you intend to use a constructor in any of your Controllers,
you MUST place the following line of code in it:
parent::__construct();
Example:
<?php
class Blog extends CI_Controller {
public function __construct() {
parent::__construct();
// Your own constructor code
}
}
Views
• To familiarize View in the context of Model View
Controller (MVC) architecture.
• To be able to create Views for presentation of User
Interface.
• To manage how the data will be displayed on the
screen
• To simplify the creation of User Interface by
fragmentation.
A view is simply a web page, or a page fragment (header,
footer) and can be embedded within other views.

Views are never called directly; they must be loaded by


a controller.
Using your text editor, create a file called blogview.php, and
code the following:
<html>
<head>
<title>My Blog</title>
</head>
<body>
<h1>Welcome to my Blog!</h1>
</body>
</html>

Then save the file in your application/views/ directory.


To load a view file you will use the following method:

$this->load->view('name’);

Where name is the file name of your view file.


The “.php” extension can be omitted unless you use other
file type.
Open the controller file you made earlier, and replace the
echo statement:
<?php
class Blog extends CI_Controller {
public function index() {
$this->load->view('blogview’);
}
}

Visit your site using the URL:


example.com/index.php/blog/
CodeIgniter will handle multiple calls to $this->load-
>view() from within a controller. If more than one call
happens, they will be appended together.

Example:
If you stored your view files within sub directories, by doing
so you will need to include the directory name in loading the
view.

Example:
$this->load->view('directory_name/file_name');
You can pass Data as an array from the controller to the
view in the second parameter of the view loading method.

$data = array( 'title' => 'My Title’);


$this->load->view('blogview', $data);
Controller : View :
<?php
class Blog extends CI_Controller { <html>
<head>
public function index() <title><?php echo $title;?></title>
{ </head>
$data['title'] = "My Real Title"; <body>
$data['heading'] = "My Real Heading"; <h1><?php echo $heading;?></h1>
$this->load->view('blogview', $data); </body>
} </html>
}
You can pass multi dimensional arrays, which can be looped to
generate multiple rows. Here’s a simple example.
Controller :
View :
<?php <html>
class Blog extends CI_Controller { <head>
<title><?php echo $title;?></title>
public function index() </head>
{ <body>
$data['todo_list'] = array(‘Ear', ‘Sleep'); <h1><?php echo $heading;?></h1>
$data['title'] = "My Real Title"; <h3>My Todo List</h3>
$data['heading'] = "My Real Heading"; <ul>
<?php foreach ($todo_list as $item):?>
$this->load->view('blogview', $data); <li><?php echo $item;?></li>
} <?php endforeach;?>
} </ul>
</body>
</html>
Web System Technologies
IT0049
Module 4
Model
• To be familiarized with Model; its anatomy and usage.
• To be able to connect to an RDBMS.
• To differentiate native query writing from Query
Builder.
• To implement Query Builder.
• To understand the importance of Query Builder.
Models are PHP classes that are designed to work with
information in your database.

Example:
Model classes are stored in your application/models/ directory.
Model classes can be nested within sub-directories.
The basic prototype for a model class is :

Model_name is the name of your class. Class names must have


the first letter capitalized and should extend the base Model
class.
The file name must match the class name.

class User_model extends CI_Model {

Your filename should be:


application/models/User_model.php
To load a model you will use the following method:

If your model is located in a sub-directory, include the relative


path from your model directory.

For example, a class located at


application/models/blog/Queries.php you’ll load it using:
Once loaded, call your model methods using an object with
the same name as your class:

To assign a different object name, you can specify an


argument in the second parameter of the loading method:
Example of a controller that loads a model and serves a
view:
To use a model globally and auto-load it during system
initialization, open the
application/config/autoload.php file and add the model to
the autoload array :
Upon loading a model, it does NOT connect automatically to
your database. The following options to connect are :

• Manual-connect using the standard database methods.


• Auto-connect using the model loading method.
You can connect using the standard database method :

$this->load->database();

Use this either from within your Controller class or your


Model class.
Example :

“$this->load->database()“ will connect to the database


specified in your database config file located at :
application/config/database.php
application/config/database.php
You can tell the model loading method to auto-connect by
passing TRUE (boolean) via the third parameter, and
connectivity settings, as defined in your database config file
will be used:
setup the database (application/config/database.php)

load the database library (application/config/autoload.php)


Create the database dborder and create a table named tblitem
with the structure given below.

Populate the table with records:


Create a model (application/models/model_item.php)

Create a controller (application/models/item.php)


Create a view (application/views/item/list.php)

Output:
Query Builder
Query Builder Class allows you to perform database operations
with minimal scripting.

In some cases, only one or two lines of code are need to perform
a database action. It provides a more simplified interface.

Allows you to create database independent applications, since


the query syntax is generated by each database adapter.

Query Builder also generates safer queries since the values are
escaped automatically by the system.
The following functions allow you to build SQL SELECT
statements.
$this->db->get()

Optional second and third parameters enables limit and offset clause:
$this->db->get_where()
Identical to the previous example except that it permits you to add a “where”
clause:

NOTE: 3rd and 4th Parameters are optional.


$this->db->select_max();
Writes a SELECT MAX(field) query. Optionally, you may include a second
parameter to rename the resulting field.
$this->db->select_min()
Writes a “SELECT MIN(field)” query. Optionally, you may include a second
parameter to rename the resulting field.

$this->db->select_avg()
Writes a “SELECT AVG(field)” query. Optionally, you may include a second
parameter to rename the resulting field.
$this->db->select_sum()
Writes a “SELECT SUM(field) query. Optionally, you may include a second
parameter to rename the resulting field.

$this->db->from()
Permits you to write the FROM portion of your query:

Note : As shown earlier, the FROM statement can be specified in $this->db->get() function
$this->db->join()
Permits you to write the JOIN portion of your query:

Multiple function calls can be made if you need several joins in one query.
A specific type of JOIN can be specified in the third parameter of the
function.

Options are: left, right, outer, inner, left outer, and right outer.
$this->db->where()
Enables you to set WHERE clauses using one of four methods:
1. Simple key/value method
2. Custom key/value method
3. Associative array method
4. Custom string
1. Simple key/value method:

multiple function calls will be chained with AND between them:


2. Custom key/value method:
include an operator to first parameter to control comparison:
3. Associative array method:

You can also include operators along with key names :


4. Custom string:
You can write your own clauses manually:

$this->db->where() accepts an optional third parameter. If set to


FALSE, CodeIgniter will not escape your values.
$this->db->or_where()
This function is the same with $this->db->where()
, except multiple instances are joined by OR:
$this->db->where_in()
Generates a WHERE field IN (‘item’, ‘item’) SQL query joined with AND if
appropriate.

$this->db->or_where_in()
Generates a WHERE field IN (‘item’, ‘item’) SQL query joined with OR if
appropriate
$this->db->where_not_in()
Generates a WHERE field NOT IN (‘item’, ‘item’) SQL query joined with AND
if appropriate

$this->db->where_not_in()
Generates a WHERE field NOT IN (‘item’, ‘item’) SQL query joined with AND
if appropriate
$this->db->like()
This generates LIKE clauses. Can be used with the following options:
1. Simple key/value method:

multiple method calls will be chained with AND keyword.


To control where the wildcard (%) is placed, use the optional third argument.
The options are ‘before’, ‘after’, ‘none’ and ‘both’ (default).
2. Associative array method:

$this->db->or_like(), same with like(), except that multiple instances are


joined by OR:

$this->db->not_like(), same with like(), except it generates NOT LIKE


statements:
$this->db->or_not_like(), same with not_like(), except multiple instances
are joined by OR:

$this->db->group_by(), generates GROUP BY clause:

Array of multiple values can be accepted as well:


$this->db->distinct()
Equivalent of “DISTINCT” clause.

$this->db->having()
Produces “HAVING” clause.
Two possible syntaxes: 1 or 2 arguments

You can also pass an array of multiple values as well:


$this->db->order_by()
Generates an ORDER BY clause. First parameter contains the name of the
column to order by. Second parameter sets the direction of the result.
Options are ASC, DESC AND RANDOM.

You can also pass a string as an argument.


$this->db->limit()
Limits the number of rows returned by the query:
$this->db->limit(10);
$this->db->get('table’);
// Produces: SELECT * FROM table LIMIT 10
Second parameter lets you set a result offset.
$this->db->limit(10, 20); // Produces: LIMIT 20, 10 (in MySQL. Other databases have
slightly different syntax : Limit 10 Offset 20)
$this->db->count_all()
Determines the number of rows in a particular table.
echo $this->db->count_all('my_table'); // Produces an integer, like 25
$this->db->insert(), generates an insert string statement.

• First parameter contains the table name.


• Second parameter is an associative array of values.

Note: Array keys should be identical with your table columns.


$this->db->insert_batch(), generates an insert string statement in batches.

• First parameter contains the table name.


• Second parameter is a two-dimensional array of values.
Note: Array keys should be identical with your table columns.
$this->db->update(), generates an update statement.

You can also pass the WHERE clause directly into the update function’s third
parameter as a string or an array.
String: Array:
$this->db->delete()
Generates a delete SQL.

First parameter is the table name, the second is the where clause. You can
also use the where() instead of passing the data to the second parameter:

No value in second parameter will generate a simple delete statement.


An array of table names can be passed into delete() if you would like to
delete data from more than 1 table.

If you want to delete all data from a table, you can use the truncate()
function, or empty_table().
$this->db->empty_table(), performs a delete SQL query.

$this->db->truncate(),performs a truncate SQL query

NOTE : If the TRUNCATE command is not permitted, truncate() will execute as “DELETE
FROM table”.

You might also like