taller de php + code igniter
DESCRIPTION
Taller básico de PHP y Code IgniterTRANSCRIPT
![Page 1: Taller de PHP + Code Igniter](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/1.jpg)
INTEGRACIÓN DE EXT JS CON CODE IGNITER
Ing. Crysfel Villa
![Page 2: Taller de PHP + Code Igniter](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/2.jpg)
Objetivo
Conocer el Framework Code Igniter e integrarlo con Ext JS para generar
aplicaciones RIA
![Page 3: Taller de PHP + Code Igniter](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/4.jpg)
Conocimiento previo
Xhtml/Html CSS Javascript básico
![Page 5: Taller de PHP + Code Igniter](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/5.jpg)
Instalación
Apache 2 PHP MySQL Ext JS Notepad++
![Page 6: Taller de PHP + Code Igniter](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/6.jpg)
¿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](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/7.jpg)
¡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](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/8.jpg)
Condiciones
$numero = 2;If($numero == 2){
$numero --;}else if($numero == 1){
$numero++;}else{
$numero = 0;}
![Page 9: Taller de PHP + Code Igniter](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/9.jpg)
Concatenación
$nombre = “Crysfel”;$apellido = “Villa”;
echo “Me llamo ”.$nombre.” ”.$apellido;
![Page 10: Taller de PHP + Code Igniter](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/13.jpg)
Recorrer un Array
foreach($hash as $persona){foreach($persona as $key=>$value){
echo “$key: $value, ";}
}
![Page 14: Taller de PHP + Code Igniter](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/17.jpg)
Servidor Web
![Page 18: Taller de PHP + Code Igniter](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/18.jpg)
Patrón MVC
![Page 19: Taller de PHP + Code Igniter](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/20.jpg)
Instalación
Descargar Descomprimir Copiar al servidor Web
![Page 21: Taller de PHP + Code Igniter](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/21.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/22.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/24.jpg)
CRUD
Create (crear) Read (leer) Update (actualizar) Delete (borrar)
![Page 25: Taller de PHP + Code Igniter](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/25.jpg)
Creación de la Base de datos Miblog
Post Id Titulo Fecha Contenido Autor
![Page 26: Taller de PHP + Code Igniter](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/26.jpg)
Model (Leer)
class Post_model extends Model{
function findAllPosts(){$query = $this->db-
>get('posts');return $query->result();
}}
![Page 27: Taller de PHP + Code Igniter](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/27.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/28.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/29.jpg)
Model (Crear)
function addPost($post){$this->db->insert('posts',$post);
}
![Page 30: Taller de PHP + Code Igniter](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/30.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/31.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/32.jpg)
Model (borrar)
function deletePost($id){$this->db->where('id',$id);$this->db->delete('posts');
}
![Page 33: Taller de PHP + Code Igniter](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/33.jpg)
Controller (Borrar)
function delete(){$this->post_model->deletePost(
$this->uri->segment(3));
$this->index();}
![Page 34: Taller de PHP + Code Igniter](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/34.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/35.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/36.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/37.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/38.jpg)
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](https://reader035.vdocuments.co/reader035/viewer/2022062307/557e2d21d8b42a51508b476f/html5/thumbnails/39.jpg)
Preguntas
Crysfel Villawww.quizzpot.com