SlideShare a Scribd company logo
PHP Hypertext Preprocessor
Què és PHP? PHP és un llenguatge de programació d'scripts, de codi lliure, que serveix per programar pàgines web dinàmiques. El codi php va empotrat dins el codi html de la pàgina web, separat pels simbols <?php  .........  ?> El codi php és processat pel servidor web (en el nostre cas apache) després de cada petició. <html> <head> <title>Primera pàgina php</title> </head> <body> <?php  echo &quot;Hola món!&quot;;  ?> </body> </html>
Què farem amb PHP? Generar continguts de pàgines web dinàmicament a partir d'informació emmagatzemada a una base de dades. Rebre informació de formularis i guardar-la a una base de dades. PRÀCTICA: Farem un blog senzill.
Variables Una variable és un objecte que conté un valor que pot anar variant al llarg de l'execució. En PHP  les variables porten a davant el símbol $ Principals tipus de variables: Enters, decimals, cadenes, booleans. <html> <head> <title>Prova de variables</title> </head> <body> <?php  $var = “Hola món!”; echo $var;  ?> </body> </html>
Operadors Permeten fer operacions entre variables. Operadors Aritmètics: +, -, * , /, % Operadors de comparació: != (distint) == (igual) Operadors lògics: And Or Not Operadors de cadenes: . (concatenació)
Exemple d'operadors <html> <head> <title>Prova d'operadors</title> </head> <body> <?php  $variable1 = 10; $variavle2 = 6; $variable3 = ( $variable1 * $variable2 ) / 3; echo $variable3 . “<br>”; $variable4 = “Tecno”; $variable5 = “logia”; echo $variable4 . $variable5; ?> </body> </html>
La clausula if - else Condiciona l'execució del codi <html> <head> <title>Prova del if</title> </head> <body> <?php  $a = 10; $b = 9; if ($a > $b)  { echo $a . &quot; es major que &quot; . $b; } else { echo $b . &quot; es major o igual que &quot; . $a; } ?> </body> </html>
Construcció de bucles amb 'while' Itera mentres es compleixi la condició. <html> <head> <title>Prova de while</title> </head> <body> <?php  $i = 1; while ($i <= 10)  { $i = $i + 1; // o també $i++ echo $i . “<br>”;  } ?> </body> </html>
Construcció de bucles amb 'for' Iteració incremental. <html> <head> <title>Prova de for</title> </head> <body> <?php  for ($i = 1; $i <= 10; $i++) { echo $i . “<br>”; } ?> </body> </html>
Activitat  Construir una web dinàmicament amb PHP  que contengui una taula de 10x10 cel.les on cada cel.la indiqui la seva posició.
Solució activitat <html> <head> <title>Prova taula dinàmica</title> </head> <body> <?php  $contador = 1; echo &quot;<table border=1>&quot;; for ($i = 1; $i <= 10; $i++) { echo &quot;<tr>&quot;; for ($j = 1; $j <= 10; $j++) { echo &quot;<td>&quot; . $contador . &quot;</td>&quot;; $contador++; } echo &quot;</tr>&quot;; } echo &quot;</table>&quot;; ?> </body> </html>
Arrays Col·lecions  de valors <html> <head> <title>Prova arrays</title> </head> <body> <?php  $arr = array(); $arr[0]=1; $arr[1]=3; $arr[2]=5; $count = count($array);   for ($i = 0; $i < $count; $i++)  { echo $arr[$i] . “<br”; } ?> </body> </html>
Funcions Codi de s'empra en multitud d'ocasions. funcions.php <?php function mitja($arg_1, $arg_2) { $ret = ($arg_1 + $arg_2) / 2; return $ret; } ?>  <html> <head> <title>Prova fucnions</title></head> <body> <?php  require 'funcions.php'; $var = mitja(10,2); echo $var; ?> </body> </html>
Valors per referència Codi de s'empra en multitud d'ocasions. funcions.php <?php function mitja($arg_1, $arg_2, &$mitja) { $ret = ($arg_1 * $arg_2) / 2; $mitja = $ret; } ?>  <html> <head> <title>Prova fucions</title></head> <body> <?php  require 'funcions.php'; mitja(10, 2, $var); echo $var; ?> </body> </html>
HTTP_GET_VARS Recepció de paràmetres per url http://servidor/get.php?param1=hola&param2=adeu get.php <html> <head> <title>Prova $HTTP_GET_VARS</title></head> <body> <?php  $var1 = $HTTP_GET_VARS[&quot;param1&quot;]; $var2 = $HTTP_GET_VARS[&quot;param2&quot;]; echo &quot;El primer paràmetre és:&quot; . $var1 . &quot;<br>&quot;; echo &quot;El segon paràmetre és:&quot; . $var2 . &quot;<br>&quot;; ?> </body> </html>
HTTP_POST_VARS Recepció de paràmetres per formulari post.php <html> <head> <title>Prova $HTTP_POST_VARS</title></head> <body> <form  method=&quot;post&quot; action=&quot;post.php&quot; name=&quot;form1&quot;> <input type=&quot;text&quot; name=&quot;num1&quot; size=&quot;15&quot;><br> <input type=&quot;text&quot; name=&quot;num2&quot; size=&quot;15&quot;><br> <input type=&quot;submit&quot; value=&quot;Suma&quot;> <br><br> </form> <?php  if ($HTTP_POST_VARS) {  $var1 = $HTTP_POST_VARS[&quot;num1&quot;]; $var2 = $HTTP_POST_VARS[&quot;num2&quot;]; echo &quot;Resultat: &quot;; echo $var1 + $var2; } ?> </body> </html>
Blog senzill en php http://www.mallorcaweb.net/tsalas/blog.php
Plantilla
Taula <table border=&quot;1&quot; width=&quot;600px&quot;> <tr> <td> <b>Autor:</b>  </td> <td> <b>Títol:</b>  </td> <td> <b>Data:</b>  </td> <td> <a href=&quot;blog.php?elimina=&quot;>Eliminar</a> </td> </tr> <tr> <td colspan=&quot;4&quot;> <b>Comentari:</b>  <hr> <br><br> </td> </tr> </table>
Form <form method=&quot;post&quot; action=&quot;blog.php&quot;> <table border=&quot;0&quot;> <tr> <td>Nom:</td><td><input type=&quot;text&quot; name=&quot;nom&quot; size=&quot;20&quot;></td> </tr> <tr> <td>Tema:</td><td><input type=&quot;text&quot; name=&quot;tema&quot; size=&quot;25&quot;></td> </tr> <tr> <td>Comentari:</td> <td><textarea name=&quot;comentari&quot; rows=&quot;5&quot; cols=&quot;50&quot;> </textarea></td> </tr> <tr> <td>&nbsp;</td><td><input type=&quot;submit&quot; value=&quot;Enviar&quot;></td> </tr> </table> </form>
Descarregar funcions de Base de Dades http://www.mallorcaweb.net/tsalas/funcionsBD.txt
Selecció de registres de BD <? require &quot;funcionsBD.php&quot;; $conn = conectaBD (&quot;host&quot;,&quot;user&quot;,&quot;pass&quot;); $sql = &quot;SELECT id, nom_autor, titol, comentari, data FROM blog&quot;; $consulta_id = ConsultaBD( &quot;nom_bd&quot;, $sql, $conn );  while ( $fila = carregaFilaBD( $consulta_id ) )  { ?> <table border=&quot;0&quot; width=&quot;600px&quot;> <tr> <td> <b>Autor:</b> <? echo $fila[1]; ?> </td> ... <td> <a href=&quot;blog.php?elimina=<? echo $fila[0]; ?>&quot;>Eliminar</a> </td> ... </table> <? } desconnectaDB($conn); ?>
Insertar i borrar de registres a la BD <? require &quot;funcionsBD.php&quot;; $conn = conectaBD (&quot;host&quot;,&quot;user&quot;,&quot;pass&quot;); if( $HTTP_POST_VARS ) { $sql = &quot;INSERT INTO blog(nom_autor, titol, comentari, data) VALUES  ('&quot; . $HTTP_POST_VARS[&quot;nom&quot;] . &quot;', '&quot; .  $HTTP_POST_VARS[&quot;tema&quot;] . &quot;', '&quot; . $HTTP_POST_VARS[&quot;comentari&quot;]  . &quot;', CURDATE())&quot;; ConsultaBD( &quot;bd&quot;, $sql, $conn );  } if( $HTTP_GET_VARS ) { $sql = &quot;DELETE FROM blog WHERE id=&quot; . $HTTP_GET_VARS[&quot;elimina&quot;]; ConsultaBD( &quot;bd&quot;, $sql, $conn );  } ?>

More Related Content

PDF
Llenguatges i Estàndards Web - PAC 1 correccció - Multimedia (UOC) - Paquita ...
PPT
Presentacio Primer Claustre curs 2008-2009
ODP
Presentació primer claustre
PPT
Presentacio Primer Claustre 2007-2008
PPT
Presentacio Acollida Professorat Nou 08 09
PPS
Llega el fin de semana
PPTX
Corporate tax planning
PPT
Corporate tax planning
Llenguatges i Estàndards Web - PAC 1 correccció - Multimedia (UOC) - Paquita ...
Presentacio Primer Claustre curs 2008-2009
Presentació primer claustre
Presentacio Primer Claustre 2007-2008
Presentacio Acollida Professorat Nou 08 09
Llega el fin de semana
Corporate tax planning
Corporate tax planning
Ad

Presentació php

  • 2. Què és PHP? PHP és un llenguatge de programació d'scripts, de codi lliure, que serveix per programar pàgines web dinàmiques. El codi php va empotrat dins el codi html de la pàgina web, separat pels simbols <?php ......... ?> El codi php és processat pel servidor web (en el nostre cas apache) després de cada petició. <html> <head> <title>Primera pàgina php</title> </head> <body> <?php echo &quot;Hola món!&quot;; ?> </body> </html>
  • 3. Què farem amb PHP? Generar continguts de pàgines web dinàmicament a partir d'informació emmagatzemada a una base de dades. Rebre informació de formularis i guardar-la a una base de dades. PRÀCTICA: Farem un blog senzill.
  • 4. Variables Una variable és un objecte que conté un valor que pot anar variant al llarg de l'execució. En PHP les variables porten a davant el símbol $ Principals tipus de variables: Enters, decimals, cadenes, booleans. <html> <head> <title>Prova de variables</title> </head> <body> <?php $var = “Hola món!”; echo $var; ?> </body> </html>
  • 5. Operadors Permeten fer operacions entre variables. Operadors Aritmètics: +, -, * , /, % Operadors de comparació: != (distint) == (igual) Operadors lògics: And Or Not Operadors de cadenes: . (concatenació)
  • 6. Exemple d'operadors <html> <head> <title>Prova d'operadors</title> </head> <body> <?php $variable1 = 10; $variavle2 = 6; $variable3 = ( $variable1 * $variable2 ) / 3; echo $variable3 . “<br>”; $variable4 = “Tecno”; $variable5 = “logia”; echo $variable4 . $variable5; ?> </body> </html>
  • 7. La clausula if - else Condiciona l'execució del codi <html> <head> <title>Prova del if</title> </head> <body> <?php $a = 10; $b = 9; if ($a > $b) { echo $a . &quot; es major que &quot; . $b; } else { echo $b . &quot; es major o igual que &quot; . $a; } ?> </body> </html>
  • 8. Construcció de bucles amb 'while' Itera mentres es compleixi la condició. <html> <head> <title>Prova de while</title> </head> <body> <?php $i = 1; while ($i <= 10) { $i = $i + 1; // o també $i++ echo $i . “<br>”; } ?> </body> </html>
  • 9. Construcció de bucles amb 'for' Iteració incremental. <html> <head> <title>Prova de for</title> </head> <body> <?php for ($i = 1; $i <= 10; $i++) { echo $i . “<br>”; } ?> </body> </html>
  • 10. Activitat Construir una web dinàmicament amb PHP que contengui una taula de 10x10 cel.les on cada cel.la indiqui la seva posició.
  • 11. Solució activitat <html> <head> <title>Prova taula dinàmica</title> </head> <body> <?php $contador = 1; echo &quot;<table border=1>&quot;; for ($i = 1; $i <= 10; $i++) { echo &quot;<tr>&quot;; for ($j = 1; $j <= 10; $j++) { echo &quot;<td>&quot; . $contador . &quot;</td>&quot;; $contador++; } echo &quot;</tr>&quot;; } echo &quot;</table>&quot;; ?> </body> </html>
  • 12. Arrays Col·lecions de valors <html> <head> <title>Prova arrays</title> </head> <body> <?php $arr = array(); $arr[0]=1; $arr[1]=3; $arr[2]=5; $count = count($array); for ($i = 0; $i < $count; $i++) { echo $arr[$i] . “<br”; } ?> </body> </html>
  • 13. Funcions Codi de s'empra en multitud d'ocasions. funcions.php <?php function mitja($arg_1, $arg_2) { $ret = ($arg_1 + $arg_2) / 2; return $ret; } ?> <html> <head> <title>Prova fucnions</title></head> <body> <?php require 'funcions.php'; $var = mitja(10,2); echo $var; ?> </body> </html>
  • 14. Valors per referència Codi de s'empra en multitud d'ocasions. funcions.php <?php function mitja($arg_1, $arg_2, &$mitja) { $ret = ($arg_1 * $arg_2) / 2; $mitja = $ret; } ?> <html> <head> <title>Prova fucions</title></head> <body> <?php require 'funcions.php'; mitja(10, 2, $var); echo $var; ?> </body> </html>
  • 15. HTTP_GET_VARS Recepció de paràmetres per url http://servidor/get.php?param1=hola&param2=adeu get.php <html> <head> <title>Prova $HTTP_GET_VARS</title></head> <body> <?php $var1 = $HTTP_GET_VARS[&quot;param1&quot;]; $var2 = $HTTP_GET_VARS[&quot;param2&quot;]; echo &quot;El primer paràmetre és:&quot; . $var1 . &quot;<br>&quot;; echo &quot;El segon paràmetre és:&quot; . $var2 . &quot;<br>&quot;; ?> </body> </html>
  • 16. HTTP_POST_VARS Recepció de paràmetres per formulari post.php <html> <head> <title>Prova $HTTP_POST_VARS</title></head> <body> <form method=&quot;post&quot; action=&quot;post.php&quot; name=&quot;form1&quot;> <input type=&quot;text&quot; name=&quot;num1&quot; size=&quot;15&quot;><br> <input type=&quot;text&quot; name=&quot;num2&quot; size=&quot;15&quot;><br> <input type=&quot;submit&quot; value=&quot;Suma&quot;> <br><br> </form> <?php if ($HTTP_POST_VARS) { $var1 = $HTTP_POST_VARS[&quot;num1&quot;]; $var2 = $HTTP_POST_VARS[&quot;num2&quot;]; echo &quot;Resultat: &quot;; echo $var1 + $var2; } ?> </body> </html>
  • 17. Blog senzill en php http://www.mallorcaweb.net/tsalas/blog.php
  • 19. Taula <table border=&quot;1&quot; width=&quot;600px&quot;> <tr> <td> <b>Autor:</b> </td> <td> <b>Títol:</b> </td> <td> <b>Data:</b> </td> <td> <a href=&quot;blog.php?elimina=&quot;>Eliminar</a> </td> </tr> <tr> <td colspan=&quot;4&quot;> <b>Comentari:</b> <hr> <br><br> </td> </tr> </table>
  • 20. Form <form method=&quot;post&quot; action=&quot;blog.php&quot;> <table border=&quot;0&quot;> <tr> <td>Nom:</td><td><input type=&quot;text&quot; name=&quot;nom&quot; size=&quot;20&quot;></td> </tr> <tr> <td>Tema:</td><td><input type=&quot;text&quot; name=&quot;tema&quot; size=&quot;25&quot;></td> </tr> <tr> <td>Comentari:</td> <td><textarea name=&quot;comentari&quot; rows=&quot;5&quot; cols=&quot;50&quot;> </textarea></td> </tr> <tr> <td>&nbsp;</td><td><input type=&quot;submit&quot; value=&quot;Enviar&quot;></td> </tr> </table> </form>
  • 21. Descarregar funcions de Base de Dades http://www.mallorcaweb.net/tsalas/funcionsBD.txt
  • 22. Selecció de registres de BD <? require &quot;funcionsBD.php&quot;; $conn = conectaBD (&quot;host&quot;,&quot;user&quot;,&quot;pass&quot;); $sql = &quot;SELECT id, nom_autor, titol, comentari, data FROM blog&quot;; $consulta_id = ConsultaBD( &quot;nom_bd&quot;, $sql, $conn ); while ( $fila = carregaFilaBD( $consulta_id ) ) { ?> <table border=&quot;0&quot; width=&quot;600px&quot;> <tr> <td> <b>Autor:</b> <? echo $fila[1]; ?> </td> ... <td> <a href=&quot;blog.php?elimina=<? echo $fila[0]; ?>&quot;>Eliminar</a> </td> ... </table> <? } desconnectaDB($conn); ?>
  • 23. Insertar i borrar de registres a la BD <? require &quot;funcionsBD.php&quot;; $conn = conectaBD (&quot;host&quot;,&quot;user&quot;,&quot;pass&quot;); if( $HTTP_POST_VARS ) { $sql = &quot;INSERT INTO blog(nom_autor, titol, comentari, data) VALUES ('&quot; . $HTTP_POST_VARS[&quot;nom&quot;] . &quot;', '&quot; . $HTTP_POST_VARS[&quot;tema&quot;] . &quot;', '&quot; . $HTTP_POST_VARS[&quot;comentari&quot;] . &quot;', CURDATE())&quot;; ConsultaBD( &quot;bd&quot;, $sql, $conn ); } if( $HTTP_GET_VARS ) { $sql = &quot;DELETE FROM blog WHERE id=&quot; . $HTTP_GET_VARS[&quot;elimina&quot;]; ConsultaBD( &quot;bd&quot;, $sql, $conn ); } ?>