Taller de PHP + Code Igniter

5,976 views
5,861 views

Published on

Taller básico de PHP y Code Igniter

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,976
On SlideShare
0
From Embeds
0
Number of Embeds
762
Actions
Shares
0
Downloads
154
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Taller de PHP + Code Igniter

  1. 1. Integración de Ext JS con CodeIgniter<br />Ing. Crysfel Villa<br />
  2. 2. Objetivo<br />Conocer el Framework CodeIgniter e integrarlo con Ext JS para generar aplicaciones RIA<br />
  3. 3. Agenda<br />Introducción a PHP<br />Conociendo el lenguaje<br />Conexiones a base de datos<br />Patrón MVC<br />CodeIgniter<br />Integración con Ext JS<br />Uniendo las piezas<br />
  4. 4. Conocimiento previo<br />Xhtml/Html<br />CSS<br />Javascript básico<br />
  5. 5. Instalación<br />Apache 2<br />PHP<br />MySQL<br />Ext JS<br />Notepad++<br />
  6. 6. ¿Qué es PHP?<br />PHP: HypertextPreprocessor<br />Lenguaje interpretado<br />Es Open Source<br />Normalmente es ejecutado del lado del servidor<br />Tiene soporte para varias bases de datos (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)<br />
  7. 7. ¡Hola mundo!<br />&lt;?php<br />$msg= &quot;¡Hola mundo!&quot;; <br />?&gt;<br />&lt;html&gt;<br /> &lt;head&gt;<br /> &lt;title&gt;Ejemplo&lt;/title&gt;<br /> &lt;/head&gt;<br /> &lt;body&gt;<br /> &lt;h1&gt;&lt;?phpecho$msg; ?&gt;&lt;/h1&gt;<br /> &lt;/body&gt;<br />&lt;/html&gt;<br />
  8. 8. Condiciones<br />$numero = 2;<br />If($numero == 2){<br /> $numero --;<br />}elseif($numero == 1){<br /> $numero++;<br />}else{<br /> $numero = 0;<br />}<br />
  9. 9. Concatenación<br />$nombre = “Crysfel”;<br />$apellido = “Villa”;<br />echo“Me llamo ”.$nombre.” ”.$apellido;<br />
  10. 10. Ciclos<br />for($i = 0; $i &lt; 10 ; $i++){<br />echo“i = $i ”;<br />}<br />$j = 5;<br />while($j &lt; 0){<br />echo“j = $j”;<br /> $j--;<br />}<br />
  11. 11. Arrays<br />$alumnos = array(“Juan”,”Pedro”,”Karina”);<br />echo $alumnos[1];<br />$alumnos[] = “Maria”; <br />array_push($alumnos,”Carlos”);<br />unset($alumnos[1]);<br />
  12. 12. Arrays<br />$hash = array(<br />array(<br /> &quot;nombre&quot;=&gt;&quot;Juan&quot;,<br /> &quot;apellido&quot;=&gt;&quot;Perez&quot;,<br /> &quot;edad&quot;=&gt;28<br /> ),<br />array(<br /> &quot;nombre&quot;=&gt;&quot;Maria&quot;,<br /> &quot;apellido&quot;=&gt;&quot;Martinez&quot;,<br /> &quot;edad&quot;=&gt;21<br /> )<br /> );<br />echo $hash[1][&apos;nombre&apos;];<br />
  13. 13. Recorrer un Array<br />foreach($hash as $persona){<br />foreach($persona as $key=&gt;$value){<br />echo“$key: $value, &quot;;<br /> }<br />}<br />
  14. 14. Conexión a una Base de Datos<br />//host, user, passwd<br />$link = mysql_connect(&apos;localhost&apos;, &apos;root&apos;, &apos;&apos;);<br />if (!$link) {<br />die(&apos;no se pudo conectar: &apos; . mysql_error());<br />}<br />echo&apos;¡Se conectó correctamente!&apos;;<br />mysql_close($link);<br />
  15. 15. Seleccionar una DB<br />$db_selected = mysql_select_db(&apos;testing&apos;, $link);<br />if (!$db_selected) {<br /> die (&apos;No se puede usar &quot;testing&quot; : &apos;. mysql_error());<br />}<br />echo&apos;Usando &quot;testing&quot;&apos;;<br />
  16. 16. Leyendo información<br />$result = mysql_query(&apos;SELECT * FROM personas&apos;);<br />if (!$result) {die(&apos;Invalidquery: &apos; . mysql_error());}<br />$personas = array();<br />while($row = mysql_fetch_assoc($result)){<br />array_push($personas, $row);<br />}<br />
  17. 17. Servidor Web<br />
  18. 18. Patrón MVC<br />
  19. 19. CodeIgniter<br />Es un Framework para desarrollo de aplicaciones Web open source basado en el lenguaje PHP.<br />Fácil de implementar<br />Buena documentación<br />No utiliza la línea de comandos<br />
  20. 20. Instalación<br />Descargar<br />Descomprimir<br />Copiar al servidor Web<br />
  21. 21. Configuranción básica<br />application/config/config.php<br />URL base<br />application/config/autoload.php<br />DataBase, Form y URL Helper, Modelos<br />application/config/database.php<br />Credenciales<br />application/config/routes.php<br />Ruta de inicio<br />
  22. 22. Controller<br />class Post extendsController{<br />functionindex(){<br /> $data[&apos;title&apos;] = “Bienvenidos&quot;;<br /> $data[&apos;date&apos;] = date(&apos;d-m-Y&apos;);<br />$this-&gt;load-&gt;view(&quot;post/page&quot;, $data);<br /> }<br />}<br />
  23. 23. View<br />&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; <br /> &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br /> &lt;title&gt;Page title&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;h1&gt;&lt;?phpecho $title; ?&gt;&lt;/h1&gt;<br />&lt;p&gt;&lt;?phpecho $date; ?&gt;&lt;/p&gt;<br />&lt;/body&gt;<br />&lt;/html&gt; <br />
  24. 24. CRUD<br />Create(crear)<br />Read (leer)<br />Update (actualizar)<br />Delete (borrar)<br />
  25. 25. Creación de la Base de datos<br />Miblog<br />Post<br />Id<br />Titulo<br />Fecha<br />Contenido<br />Autor<br />
  26. 26. Model (Leer)<br />classPost_modelextendsModel{<br />functionfindAllPosts(){<br /> $query = $this-&gt;db-&gt;get(&apos;posts&apos;);<br />return $query-&gt;result();<br />}<br />}<br />
  27. 27. Controller (Leer)<br />functionindex(){ //mostrar todos los posts<br /> $data[&apos;title&apos;] = &quot;Bienvenido&quot;;<br /> $data[&apos;date&apos;] = date(&apos;d-m-Y&apos;);<br /> $posts = $this-&gt;post_model-&gt;findAllPosts();<br />if(!isset($posts))$posts =array();<br /> $data[&apos;posts&apos;] = $posts;<br />$this-&gt;load-&gt;view(&quot;post/list&quot;,$data);<br /> }<br />*Para usar el “post_model” se debe cargar primero<br />
  28. 28. View (Leer)<br />&lt;h1&gt;&lt;?phpecho $title; ?&gt;&lt;/h1&gt;<br />&lt;p&gt;&lt;?phpecho $date; ?&gt;&lt;/p&gt;<br />&lt;ul&gt;<br />&lt;?phpforeach($postsas $post):?&gt;<br /> &lt;li&gt;&lt;?phpecho $post-&gt;titulo; ?&gt;&lt;/li&gt;<br />&lt;?phpendforeach?&gt;<br />&lt;/ul&gt;<br />
  29. 29. Model (Crear)<br />functionaddPost($post){<br />$this-&gt;db-&gt;insert(&apos;posts&apos;,$post);<br />}<br />
  30. 30. Controller (Crear)<br />functioncreate(){<br /> $post = array(<br /> &apos;titulo&apos;=&gt;$this-&gt;input-&gt;post(&apos;titulo&apos;),<br /> &apos;contenido&apos;=&gt;$this-&gt;input-&gt;post(&apos;contenido&apos;),<br /> &apos;author&apos;=&gt;$this-&gt;input-&gt;post(&apos;autor&apos;),<br /> &apos;fecha&apos;=&gt;date(&apos;Y-m-d&apos;)<br /> );<br />$this-&gt;post_model-&gt;addPost($post);<br />$this-&gt;index();<br /> }<br />
  31. 31. View (Crear)<br />&lt;?phpechoform_open(&apos;post/create&apos;); ?&gt;<br />&lt;p&gt;<br /> &lt;labelfor=&quot;titulo&quot;&gt;Titulo:&lt;/label&gt;<br /> &lt;input type=&quot;text&quot; name=&quot;titulo&quot; id=&quot;titulo&quot; /&gt;<br />&lt;/p&gt;<br />&lt;p&gt;<br /> &lt;labelfor=&quot;autor&quot;&gt;Autor:&lt;/label&gt;<br /> &lt;input type=&quot;text&quot; name=&quot;autor&quot; id=&quot;autor&quot; /&gt;<br />&lt;/p&gt;<br />&lt;p&gt;<br /> &lt;labelfor=&quot;contenido&quot;&gt;Contenido:&lt;/label&gt;<br /> &lt;textareaname=&quot;contenido&quot; id=&quot;contenido&quot;&gt;&lt;/textarea&gt;<br />&lt;/p&gt;<br />&lt;input type=&quot;submit&quot; value=&quot;Guardar&quot; /&gt;<br />&lt;?phpechoform_close(); ?&gt;<br />*Para usar el “form_open” se debe cargar el “form” helper<br />
  32. 32. Model (borrar)<br />functiondeletePost($id){<br /> $this-&gt;db-&gt;where(&apos;id&apos;,$id);<br /> $this-&gt;db-&gt;delete(&apos;posts&apos;);<br />}<br />
  33. 33. Controller (Borrar)<br />function delete(){<br />$this-&gt;post_model-&gt;deletePost( <br /> $this-&gt;uri-&gt;segment(3)<br /> );<br />$this-&gt;index();<br />}<br />
  34. 34. View (Borrar)<br />&lt;?php <br />echoanchor(&quot;post/delete/$post-&gt;id&quot;,&quot;Borrar&quot;); <br />?&gt;<br />*Para utilizar el ”anchor” se debe cargar el ”url” helper<br />
  35. 35. Model (Actualizar) <br />functionfindPostById($id){<br />$this-&gt;db-&gt;where(&apos;id&apos;,$id);<br />$query = $this-&gt;db-&gt;get(&apos;posts&apos;);<br />if($query-&gt;num_rows() &gt; 0) return $query-&gt;row(); <br />}<br />functionupdatePost($post){<br />$this-&gt;db-&gt;where(&apos;id&apos;,$post[&apos;id&apos;]);<br />$this-&gt;db-&gt;update(&apos;posts&apos;,$post);<br />}<br />
  36. 36. Controller (Actualizar)<br />functionedit(){<br />$data[&apos;post&apos;] = $this-&gt;post_model-&gt;findPostById($this-&gt;uri-&gt;segment(3));<br />$this-&gt;load-&gt;view(&apos;post/edit&apos;,$data);<br />}<br />
  37. 37. Controller (Actualizar)<br />functionupdate(){<br /> $post = array(<br /> &apos;id&apos;=&gt;$this-&gt;input-&gt;post(&apos;id&apos;),<br /> &apos;titulo&apos;=&gt;$this-&gt;input-&gt;post(&apos;titulo&apos;),<br /> &apos;contenido&apos;=&gt;$this-&gt;input-&gt;post(&apos;contenido&apos;),<br /> &apos;author&apos;=&gt;$this-&gt;input-&gt;post(&apos;autor&apos;),<br /> &apos;fecha&apos;=&gt;date(&apos;Y-m-d&apos;)<br /> );<br />$this-&gt;post_model-&gt;updatePost($post);<br />$this-&gt;index();<br /> }<br />
  38. 38. View (Actualizar)<br />&lt;?phpechoform_open(&apos;post/update&apos;); ?&gt;<br />&lt;input type=&quot;hidden&quot; name=&quot;id&quot; value=&quot;&lt;?phpecho $post-&gt;id;?&gt;&quot; /&gt;<br />&lt;p&gt;<br /> &lt;labelfor=&quot;titulo&quot;&gt;Titulo:&lt;/label&gt;<br /> &lt;input type=&quot;text&quot; name=&quot;titulo&quot; id=&quot;titulo&quot; value=&quot;&lt;?phpecho $post-&gt;titulo;?&gt;&quot; /&gt;<br />&lt;/p&gt;<br />&lt;p&gt;<br /> &lt;labelfor=&quot;autor&quot;&gt;Autor:&lt;/label&gt;<br /> &lt;input type=&quot;text&quot; name=&quot;autor&quot; id=&quot;autor&quot; value=&quot;&lt;?phpecho $post-&gt;author;?&gt;&quot; /&gt;<br />&lt;/p&gt;<br />&lt;p&gt;<br /> &lt;labelfor=&quot;contenido&quot;&gt;Contenido:&lt;/label&gt;<br /> &lt;textareaname=&quot;contenido&quot; id=&quot;contenido&quot;&gt;&lt;?phpecho $post-&gt;contenido;?&gt;&lt;/textarea&gt;<br />&lt;/p&gt;<br />&lt;input type=&quot;submit&quot; value=&quot;Guardar&quot; /&gt;<br />&lt;?phpechoform_close(); ?&gt;<br />
  39. 39. Preguntas<br />Crysfel Villa<br />www.quizzpot.com<br />training@quizzpot.com<br />

×