Basi di Dati e Web - unipr.it · Università degli Studi di Parma Simone Cirani, Ph.D. Basi di Dati...

26
Università degli Studi di Parma Simone Cirani, Ph.D. 2013/2014 - Parma Basi di Dati e Web Introduction to CodeIgniter 14/01/2014 Basi di Dati e Web martedì 14 gennaio 14

Transcript of Basi di Dati e Web - unipr.it · Università degli Studi di Parma Simone Cirani, Ph.D. Basi di Dati...

Università degli Studi di Parma

Simone Cirani, Ph.D. 2013/2014 - ParmaBasi di Dati e Web

Introduction to CodeIgniter14/01/2014

Basi di Dati e Web

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

CodeIgniter

- CodeIgniter is an Application Framework to build web applications using PHP

- CodeIgniter provides a rich set of libraries for commonly needed tasks, as well as a simple interface and logical structure to access these libraries

- It allows to develop projects much faster than if writing code from scratch

- It can be downloaded for free at http://ellislab.com/codeigniter

- CodeIgniter is installed by:

- unzipping the package.

- uploading the CodeIgniter folders and files to your server; normally the index.php file will be at your root

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

Model-View-Controller

- CodeIgniter uses the Model-View-Controller development pattern

- MVC is a software approach that separates application logic from presentation

- MVC divides code into three components:

- the Model represents data structures: typically model classes will contain functions that help you retrieve, insert, and update information in your database.

- the View is the information that is being presented to a user: a view will normally be a web page, but in CodeIgniter, a view can also be a page fragment like a header or footer

- the Controller serves as an intermediary between the Model, the View, and any other resources needed to process the HTTP request and generate a web page.

- CodeIgniter does not require a model (if no access to a database is needed, for instance)

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

CodeIgniter URLs

- URLs in CodeIgniter are designed to be search-engine and human friendly

- CodeIgniter uses a segment-based approach:

http://example.com/index.php/news/article/123456

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

CodeIgniter URLs

- URLs in CodeIgniter are designed to be search-engine and human friendly

- CodeIgniter uses a segment-based approach:

http://example.com/index.php/news/article/123456

- example.com is the website domain

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

CodeIgniter URLs

- URLs in CodeIgniter are designed to be search-engine and human friendly

- CodeIgniter uses a segment-based approach:

http://example.com/index.php/news/article/123456

- example.com is the website domain

- index.php is the CodeIgniter engine file, residing in the installation folder of CodeIgniter

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

CodeIgniter URLs

- URLs in CodeIgniter are designed to be search-engine and human friendly

- CodeIgniter uses a segment-based approach:

http://example.com/index.php/news/article/123456

- example.com is the website domain

- index.php is the CodeIgniter engine file, residing in the installation folder of CodeIgniter

- news is the (lowercase) name of the controller’s class

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

CodeIgniter URLs

- URLs in CodeIgniter are designed to be search-engine and human friendly

- CodeIgniter uses a segment-based approach:

http://example.com/index.php/news/article/123456

- example.com is the website domain

- index.php is the CodeIgniter engine file, residing in the installation folder of CodeIgniter

- news is the (lowercase) name of the controller’s class

- article is one of the controller’s methods

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

CodeIgniter URLs

- URLs in CodeIgniter are designed to be search-engine and human friendly

- CodeIgniter uses a segment-based approach:

http://example.com/index.php/news/article/123456

- example.com is the website domain

- index.php is the CodeIgniter engine file, residing in the installation folder of CodeIgniter

- news is the (lowercase) name of the controller’s class

- article is one of the controller’s methods

- 123456 is the argument of the method

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

CodeIgniter URLs

- URLs in CodeIgniter are designed to be search-engine and human friendly

- CodeIgniter uses a segment-based approach:

http://example.com/index.php/news/article/123456

- example.com is the website domain

- index.php is the CodeIgniter engine file, residing in the installation folder of CodeIgniter

- news is the (lowercase) name of the controller’s class

- article is one of the controller’s methods

- 123456 is the argument of the method

- other arguments (in exact order) will be written after this, separated by a slash

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

Application Flow Chart

1. The index.php serves as the front controller, initializing the base resources needed to run CodeIgniter

2. The Router examines the HTTP request to determine what should be done with it

3. If a cache file exists, it is sent directly to the browser, bypassing the normal system execution

4. Before the application controller is loaded, the HTTP request and any user submitted data is filtered for security

5. The Controller loads the model, core libraries, helpers, and any other resources needed to process the specific request

6. The finalized View is rendered then sent to the web browser to be seen. If caching is enabled, the view is cached first so that on subsequent requests it can be served

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

Database access

- CodeIgniter comes with a full-featured and very fast abstracted database class that supports both traditional structures and Active Record patterns

- The Database class already implements all the details about connecting to the database and performing queries

- In order to use the Database class, it must be loaded:

$this->load->database();

- This loads the class based on the settings in the application/config/database.php file

- Once loaded, the Database class can be accessed using $this->db

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

Database configuration- CodeIgniter has a config file to store database connection values (username, password, database

name, etc.)

- The config file is located at application/config/database.php

$db['default']['hostname'] = "localhost";$db['default']['username'] = "root";$db['default']['password'] = "";$db['default']['database'] = "database_name";$db['default']['dbdriver'] = "mysql";$db['default']['dbprefix'] = "";$db['default']['pconnect'] = TRUE;$db['default']['db_debug'] = FALSE;$db['default']['cache_on'] = FALSE;$db['default']['cachedir'] = "";$db['default']['char_set'] = "utf8";$db['default']['dbcollat'] = "utf8_general_ci";$db['default']['swap_pre'] = "";$db['default']['autoinit'] = TRUE;$db['default']['stricton'] = FALSE;

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

Active Record class

- CodeIgniter uses a modified version of the Active Record Database Pattern

- The Active Record pattern allows information to be retrieved, inserted, and updated in database with minimal scripting

- Benefits to using the Active Record:

- database independent applications, since the query syntax is generated by each database adapter

- safer queries, since the values are escaped automatically by the system

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

Selecting data- $this->db->get();

- runs the selection query and returns the result

- the first argument (optional) is the table name, if missing must be used with the $this->db->from() function

- accepts two other optional arguments, for LIMIT clause

- $this->db->select();

- permits to write the SELECT portion of your query: $this->db->select('title, content, date');

- $this->db-> select_max(), select_min(), select_avg(), select_sum()

- argument is the column to use for aggregate function

- $this->db->from();

- Permits to write the FROM portion of your query

- argument is the table name

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

Selecting data- $this->db->join();

- Permits to write the JOIN portion of your query

- the first argument is the table name, the second is the join condition

- must be used in conjunction with the $this->db->from() function

- $this->db->where();

- enables to set WHERE clauses

- $this->db->where('name', $name); // Produces: WHERE name = 'Joe'

- $this->db->where('id <', $id); // Produces: WHERE id < 45

- $this->db->where(array('name' => $name, 'title' => $title)); // Produces: WHERE name = 'Joe' AND title = 'boss'

- $this->db->where("name='Joe' AND status='boss'"); // same as above

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

Selecting data

- Other functions for selecting data:

- $this->db->distinct()

- $this->db->group_by()

- $this->db->having()

- $this->db->limit()

- Many other functions are provided to create complex queries: refer to the user guide

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

Retrieving data- The get() function returns the results in a variable: $query = $this->db->get(‘user’);

- This variable is an object that has a number of methods that can be used to get access to the returned rows

- result(): returns the query result as an array of objects or an empty array on failure

- result_array(): returns the query result as an array of associative arrays

$query = $this->db->get('user');

foreach ($query->result() as $user){   echo $user->name; }

$query = $this->db->get('user');

foreach ($query->result_array() as $user){   echo $user['name']; }

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

Retrieving data

- row() and row_array(): return a single result row as an object or as associative array, respectively

- num_rows(): returns the number of rows returned by the query

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

Inserting data

- $this->db->insert();

- generates an insert string based on the data supplied, and runs the query

- takes an optional argument that can be an array or object (if missing, must be used in conjunction with the set() method)

$data = array(   'title' => 'My title' ,   'name' => 'My Name' ,   'date' => 'My date');

$this->db->insert('mytable', $data);

// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

Inserting data- $this->db->set();

- set values for inserts or updates

- first argument is the column name, the second is the value

- an optional boolean third parameter can be used to prevent escaping (set to FALSE)

$this->db->set('name', $name); $this->db->insert('mytable');

// Produces: INSERT INTO mytable (name) VALUES ('{$name}')

$this->db->set('time', 'NOW()', FALSE);$this->db->insert('mytable'); // gives INSERT INTO mytable (time) VALUES (NOW())

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

Updating data- $this->db->update();

- generates an update string based on the data supplied, and runs the query

- first argument is the name of the table

- takes an optional argument that can be an array or object (if missing, must be used in conjunction with the set() method)

- a call to where() can be used to set the WHERE clause

$data = array(               'title' => $title,               'name' => $name,               'date' => $date            );

$this->db->where('id', $id);$this->db->update('mytable', $data);

// UPDATE mytable // SET title = '{$title}', name = '{$name}', date = '{$date}'// WHERE id = $id

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

Deleting data- $this->db->delete();

- generates a delete SQL string and runs the query

- first argument is the name of the table

- second parameter (optional) is the where clause (if missing, must be used in conjunction with the where() method)

$this->db->delete('mytable', array('id' => $id));

// Produces:// DELETE FROM mytable // WHERE id = $id

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

Method Chaining

- Method chaining allows to simplify your syntax by connecting multiple functions

- It can be simpler to read but harder to work with, it is up to the developer to choose

$this->db->select('title')->from('mytable')->where('id', $id)->limit(10, 20);

$query = $this->db->get();

martedì 14 gennaio 14

Università degli Studi di Parma

2013/2014 - ParmaBasi di Dati e WebSimone Cirani, Ph.D.

CodeIgniter

- CodeIgniter comes with a great user guide that documents all the features of the framework

- It is included in the home of every CodeIgniter installation

- Online: http://ellislab.com/codeigniter/user-guide/index.html

martedì 14 gennaio 14

Università degli Studi di Parma

Simone Cirani, Ph.D. 2013/2014 - ParmaBasi di Dati e Web

Basi di Dati e WebIntroduction to CodeIgniter

14/01/2014

martedì 14 gennaio 14