39
INTEGRACIÓN DE EXT JS CON CODE IGNITER Ing. Crysfel Villa

Taller de PHP + Code Igniter

Embed Size (px)

DESCRIPTION

Taller básico de PHP y Code Igniter

Citation preview

Page 1: Taller de PHP + Code Igniter

INTEGRACIÓN DE EXT JS CON CODE IGNITER

Ing. Crysfel Villa

Page 2: Taller de PHP + Code Igniter

Objetivo

Conocer el Framework Code Igniter e integrarlo con Ext JS para generar

aplicaciones RIA

Page 3: Taller de PHP + Code Igniter

Agenda

Introducción a PHP Conociendo el lenguaje Conexiones a base de datos Patrón MVC Code Igniter Integración con Ext JS Uniendo las piezas

Page 4: Taller de PHP + Code Igniter

Conocimiento previo

Xhtml/Html CSS Javascript básico

Page 5: Taller de PHP + Code Igniter

Instalación

Apache 2 PHP MySQL Ext JS Notepad++

Page 6: Taller de PHP + Code Igniter

¿Qué es PHP?

PHP: Hypertext Preprocessor Lenguaje interpretado Es Open Source Normalmente es ejecutado del lado

del servidor Tiene soporte para varias bases de

datos (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)

Page 7: Taller de PHP + Code Igniter

¡Hola mundo!

<?php$msg = "¡Hola mundo!";

?><html>

<head><title>Ejemplo</title>

</head><body>

<h1><?php echo $msg; ?></h1></body>

</html>

Page 8: Taller de PHP + Code Igniter

Condiciones

$numero = 2;If($numero == 2){

$numero --;}else if($numero == 1){

$numero++;}else{

$numero = 0;}

Page 9: Taller de PHP + Code Igniter

Concatenación

$nombre = “Crysfel”;$apellido = “Villa”;

echo “Me llamo ”.$nombre.” ”.$apellido;

Page 10: Taller de PHP + Code Igniter

Ciclos

for($i = 0; $i < 10 ; $i++){echo “i = $i ”;

}

$j = 5;while($j < 0){

echo “j = $j”;$j--;

}

Page 11: Taller de PHP + Code Igniter

Arrays

$alumnos = array(“Juan”,”Pedro”,”Karina”);

echo $alumnos[1];

$alumnos[] = “Maria”; array_push($alumnos,”Carlos”);

unset($alumnos[1]);

Page 12: Taller de PHP + Code Igniter

Arrays

$hash = array(array(

"nombre"=>"Juan","apellido"=>"Perez","edad"=>28

),array(

"nombre"=>"Maria","apellido"=>"Martinez","edad"=>21

));

echo $hash[1]['nombre'];

Page 13: Taller de PHP + Code Igniter

Recorrer un Array

foreach($hash as $persona){foreach($persona as $key=>$value){

echo “$key: $value, ";}

}

Page 14: Taller de PHP + Code Igniter

Conexión a una Base de Datos//host, user, passwd$link = mysql_connect('localhost', 'root', '');if (!$link) {

die('no se pudo conectar: ' . mysql_error());

}echo '¡Se conectó correctamente!';

mysql_close($link);

Page 15: Taller de PHP + Code Igniter

Seleccionar una DB

$db_selected = mysql_select_db('testing', $link);

if (!$db_selected) {die ('No se puede usar "testing" : '. mysql_error());

}

echo 'Usando "testing"';

Page 16: Taller de PHP + Code Igniter

Leyendo información

$result = mysql_query('SELECT * FROM personas');

if (!$result) {die('Invalid query: ' . mysql_error());}

$personas = array();while($row = mysql_fetch_assoc($result)){

array_push($personas, $row);}

Page 17: Taller de PHP + Code Igniter

Servidor Web

Page 18: Taller de PHP + Code Igniter

Patrón MVC

Page 19: Taller de PHP + Code Igniter

Code Igniter

Es un Framework para desarrollo de aplicaciones Web open source basado en el lenguaje PHP.

Fácil de implementar Buena documentación No utiliza la línea de comandos

Page 20: Taller de PHP + Code Igniter

Instalación

Descargar Descomprimir Copiar al servidor Web

Page 21: Taller de PHP + Code Igniter

Configuranción básica

application/config/config.php URL base

application/config/autoload.php DataBase, Form y URL Helper, Modelos

application/config/database.php Credenciales

application/config/routes.php Ruta de inicio

Page 22: Taller de PHP + Code Igniter

Controller

class Post extends Controller{

function index(){$data['title'] = “Bienvenidos";$data['date'] = date('d-m-Y');

$this->load->view("post/page", $data);}

}

Page 23: Taller de PHP + Code Igniter

View

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html><head>

<title>Page title</title></head><body>

<h1><?php echo $title; ?></h1><p><?php echo $date; ?></p>

</body></html>

Page 24: Taller de PHP + Code Igniter

CRUD

Create (crear) Read (leer) Update (actualizar) Delete (borrar)

Page 25: Taller de PHP + Code Igniter

Creación de la Base de datos Miblog

Post Id Titulo Fecha Contenido Autor

Page 26: Taller de PHP + Code Igniter

Model (Leer)

class Post_model extends Model{

function findAllPosts(){$query = $this->db-

>get('posts');return $query->result();

}}

Page 27: Taller de PHP + Code Igniter

Controller (Leer)

function index(){ //mostrar todos los posts$data['title'] = "Bienvenido";$data['date'] = date('d-m-Y');

$posts = $this->post_model->findAllPosts();if(!isset($posts))$posts = array();

$data['posts'] = $posts;

$this->load->view("post/list",$data);}

*Para usar el “post_model” se debe cargar primero

Page 28: Taller de PHP + Code Igniter

View (Leer)

<h1><?php echo $title; ?></h1><p><?php echo $date; ?></p>

<ul><?php foreach($posts as $post): ?>

<li><?php echo $post->titulo; ?></li>

<?php endforeach ?></ul>

Page 29: Taller de PHP + Code Igniter

Model (Crear)

function addPost($post){$this->db->insert('posts',$post);

}

Page 30: Taller de PHP + Code Igniter

Controller (Crear)

function create(){$post = array('titulo'=>$this->input->post('titulo'),'contenido'=>$this->input->post('contenido'),'author'=>$this->input->post('autor'),'fecha'=>date('Y-m-d'));

$this->post_model->addPost($post);

$this->index();}

Page 31: Taller de PHP + Code Igniter

View (Crear)

<?php echo form_open('post/create'); ?><p>

<label for="titulo">Titulo:</label><input type="text" name="titulo" id="titulo" />

</p><p>

<label for="autor">Autor:</label><input type="text" name="autor" id="autor" />

</p><p>

<label for="contenido">Contenido:</label><textarea name="contenido" id="contenido"></textarea>

</p><input type="submit" value="Guardar" /><?php echo form_close(); ?> *Para usar el

“form_open” se debe cargar el “form” helper

Page 32: Taller de PHP + Code Igniter

Model (borrar)

function deletePost($id){$this->db->where('id',$id);$this->db->delete('posts');

}

Page 33: Taller de PHP + Code Igniter

Controller (Borrar)

function delete(){$this->post_model->deletePost(

$this->uri->segment(3));

$this->index();}

Page 34: Taller de PHP + Code Igniter

View (Borrar)

<?php echo anchor("post/delete/$post-

>id","Borrar"); ?>

*Para utilizar el ”anchor” se debe cargar el ”url” helper

Page 35: Taller de PHP + Code Igniter

Model (Actualizar)

function findPostById($id){$this->db->where('id',$id);$query = $this->db->get('posts');if ($query->num_rows() > 0) return $query->row();

}

function updatePost($post){$this->db->where('id',$post['id']);$this->db->update('posts',$post);

}

Page 36: Taller de PHP + Code Igniter

Controller (Actualizar)

function edit(){$data['post'] = $this->post_model->findPostById($this->uri->segment(3));

$this->load->view('post/edit',$data);}

Page 37: Taller de PHP + Code Igniter

Controller (Actualizar)

function update(){$post = array('id'=>$this->input->post('id'),'titulo'=>$this->input->post('titulo'),'contenido'=>$this->input->post('contenido'),'author'=>$this->input->post('autor'),'fecha'=>date('Y-m-d'));

$this->post_model->updatePost($post);

$this->index();}

Page 38: Taller de PHP + Code Igniter

View (Actualizar)

<?php echo form_open('post/update'); ?><input type="hidden" name="id" value="<?php echo $post->id;?>" /><p>

<label for="titulo">Titulo:</label><input type="text" name="titulo" id="titulo" value="<?php echo $post->titulo;?>" />

</p><p>

<label for="autor">Autor:</label><input type="text" name="autor" id="autor" value="<?php echo $post->author;?>" />

</p><p>

<label for="contenido">Contenido:</label><textarea name="contenido" id="contenido"><?php echo $post->contenido;?></textarea>

</p><input type="submit" value="Guardar" /><?php echo form_close(); ?>

Page 39: Taller de PHP + Code Igniter

Preguntas

Crysfel Villawww.quizzpot.com

[email protected]