Download - Taller de PHP + Code Igniter
INTEGRACIÓN DE EXT JS CON CODE IGNITER
Ing. Crysfel Villa
Objetivo
Conocer el Framework Code Igniter e integrarlo con Ext JS para generar
aplicaciones RIA
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
Conocimiento previo
Xhtml/Html CSS Javascript básico
Instalación
Apache 2 PHP MySQL Ext JS Notepad++
¿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.)
¡Hola mundo!
<?php$msg = "¡Hola mundo!";
?><html>
<head><title>Ejemplo</title>
</head><body>
<h1><?php echo $msg; ?></h1></body>
</html>
Condiciones
$numero = 2;If($numero == 2){
$numero --;}else if($numero == 1){
$numero++;}else{
$numero = 0;}
Concatenación
$nombre = “Crysfel”;$apellido = “Villa”;
echo “Me llamo ”.$nombre.” ”.$apellido;
Ciclos
for($i = 0; $i < 10 ; $i++){echo “i = $i ”;
}
$j = 5;while($j < 0){
echo “j = $j”;$j--;
}
Arrays
$alumnos = array(“Juan”,”Pedro”,”Karina”);
echo $alumnos[1];
$alumnos[] = “Maria”; array_push($alumnos,”Carlos”);
unset($alumnos[1]);
Arrays
$hash = array(array(
"nombre"=>"Juan","apellido"=>"Perez","edad"=>28
),array(
"nombre"=>"Maria","apellido"=>"Martinez","edad"=>21
));
echo $hash[1]['nombre'];
Recorrer un Array
foreach($hash as $persona){foreach($persona as $key=>$value){
echo “$key: $value, ";}
}
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);
Seleccionar una DB
$db_selected = mysql_select_db('testing', $link);
if (!$db_selected) {die ('No se puede usar "testing" : '. mysql_error());
}
echo 'Usando "testing"';
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);}
Servidor Web
Patrón MVC
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
Instalación
Descargar Descomprimir Copiar al servidor Web
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
Controller
class Post extends Controller{
function index(){$data['title'] = “Bienvenidos";$data['date'] = date('d-m-Y');
$this->load->view("post/page", $data);}
}
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>
CRUD
Create (crear) Read (leer) Update (actualizar) Delete (borrar)
Creación de la Base de datos Miblog
Post Id Titulo Fecha Contenido Autor
Model (Leer)
class Post_model extends Model{
function findAllPosts(){$query = $this->db-
>get('posts');return $query->result();
}}
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
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>
Model (Crear)
function addPost($post){$this->db->insert('posts',$post);
}
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();}
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
Model (borrar)
function deletePost($id){$this->db->where('id',$id);$this->db->delete('posts');
}
Controller (Borrar)
function delete(){$this->post_model->deletePost(
$this->uri->segment(3));
$this->index();}
View (Borrar)
<?php echo anchor("post/delete/$post-
>id","Borrar"); ?>
*Para utilizar el ”anchor” se debe cargar el ”url” helper
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);
}
Controller (Actualizar)
function edit(){$data['post'] = $this->post_model->findPostById($this->uri->segment(3));
$this->load->view('post/edit',$data);}
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();}
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(); ?>
Preguntas
Crysfel Villawww.quizzpot.com