Más contenido relacionado Taller de PHP + Code Igniter2. Objetivo Conocer el Framework CodeIgniter e integrarlo con Ext JS para generar aplicaciones RIA 3. Agenda Introducción a PHP Conociendo el lenguaje Conexiones a base de datos Patrón MVC CodeIgniter Integración con Ext JS Uniendo las piezas 6. ¿Qué es PHP? PHP: HypertextPreprocessor 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.) 7. ¡Hola mundo! <?php $msg= "¡Hola mundo!"; ?> <html> <head> <title>Ejemplo</title> </head> <body> <h1><?phpecho$msg; ?></h1> </body> </html> 8. Condiciones $numero = 2; If($numero == 2){ $numero --; }elseif($numero == 1){ $numero++; }else{ $numero = 0; } 10. Ciclos for($i = 0; $i < 10 ; $i++){ echo“i = $i ”; } $j = 5; while($j < 0){ echo“j = $j”; $j--; } 11. Arrays $alumnos = array(“Juan”,”Pedro”,”Karina”); echo $alumnos[1]; $alumnos[] = “Maria”; array_push($alumnos,”Carlos”); unset($alumnos[1]); 12. Arrays $hash = array( array( "nombre"=>"Juan", "apellido"=>"Perez", "edad"=>28 ), array( "nombre"=>"Maria", "apellido"=>"Martinez", "edad"=>21 ) ); echo $hash[1]['nombre']; 13. Recorrer un Array foreach($hash as $persona){ foreach($persona as $key=>$value){ echo“$key: $value, "; } } 14. 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); 15. Seleccionar una DB $db_selected = mysql_select_db('testing', $link); if (!$db_selected) { die ('No se puede usar "testing" : '. mysql_error()); } echo'Usando "testing"'; 16. Leyendo información $result = mysql_query('SELECT * FROM personas'); if (!$result) {die('Invalidquery: ' . mysql_error());} $personas = array(); while($row = mysql_fetch_assoc($result)){ array_push($personas, $row); } 19. CodeIgniter 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 22. Controller class Post extendsController{ functionindex(){ $data['title'] = “Bienvenidos"; $data['date'] = date('d-m-Y'); $this->load->view("post/page", $data); } } 23. 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><?phpecho $title; ?></h1> <p><?phpecho $date; ?></p> </body> </html> 25. Creación de la Base de datos Miblog Post Id Titulo Fecha Contenido Autor 27. Controller (Leer) functionindex(){ //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 28. View (Leer) <h1><?phpecho $title; ?></h1> <p><?phpecho $date; ?></p> <ul> <?phpforeach($postsas $post):?> <li><?phpecho $post->titulo; ?></li> <?phpendforeach?> </ul> 30. Controller (Crear) functioncreate(){ $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(); } 31. View (Crear) <?phpechoform_open('post/create'); ?> <p> <labelfor="titulo">Titulo:</label> <input type="text" name="titulo" id="titulo" /> </p> <p> <labelfor="autor">Autor:</label> <input type="text" name="autor" id="autor" /> </p> <p> <labelfor="contenido">Contenido:</label> <textareaname="contenido" id="contenido"></textarea> </p> <input type="submit" value="Guardar" /> <?phpechoform_close(); ?> *Para usar el “form_open” se debe cargar el “form” helper 34. View (Borrar) <?php echoanchor("post/delete/$post->id","Borrar"); ?> *Para utilizar el ”anchor” se debe cargar el ”url” helper 35. Model (Actualizar) functionfindPostById($id){ $this->db->where('id',$id); $query = $this->db->get('posts'); if($query->num_rows() > 0) return $query->row(); } functionupdatePost($post){ $this->db->where('id',$post['id']); $this->db->update('posts',$post); } 37. Controller (Actualizar) functionupdate(){ $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(); } 38. View (Actualizar) <?phpechoform_open('post/update'); ?> <input type="hidden" name="id" value="<?phpecho $post->id;?>" /> <p> <labelfor="titulo">Titulo:</label> <input type="text" name="titulo" id="titulo" value="<?phpecho $post->titulo;?>" /> </p> <p> <labelfor="autor">Autor:</label> <input type="text" name="autor" id="autor" value="<?phpecho $post->author;?>" /> </p> <p> <labelfor="contenido">Contenido:</label> <textareaname="contenido" id="contenido"><?phpecho $post->contenido;?></textarea> </p> <input type="submit" value="Guardar" /> <?phpechoform_close(); ?>