Professional Documents
Culture Documents
20131
Prctica2.1
Aplicacincompleta
1. 2. 3. 4. Generarunamatrizdenmerosenterosaleatorios. Adicionarunamatrizdondeseindiqueelsignodelnmerogenerado(+,,0). Crearunatablaconlainformacindelasmatricesanteriores. Generarencabezadosparalatabla(obtenidosdirectamentedesdeladefinicinde laclaseynoarbitrariamentedesdeelarchivotemplate). 5. Adicionar una columna a la tabla donde se indique el acumulado (suma) de los nmerosgenerados. FuncionesPHPutilizadas: rand(intmin,intmax):generaunnmeroaleatorio.Opcionalmentesepuedendefinirlos valoreslmites. Unaposiblesolucinqueseexplicaenesedocumentosecomponedecuatroarchivos: template.php Esel programaque muestra losresultados (plantilla). Principalmentesecompone decdigo HTML. El cdigo PHP se limita amostrarelcontenidodeunaobjeto (o variables determinada). Por si slo es un archivovlido, aunque lasvariables no estarandefinidas. estilo.css Es la hoja de estilo (cascading style sheet) que definecomosevan a desplegar loselementosenelnavegador. index.php Es el programa de inicio. Puede tener cualquier nombre. Se utiliza "index.php" para que el servidor web lo despliegue automticamente y llamar a los dems componentesdelprograma. NombreClase.php Es la definicin de cada una de las clases que forman la solucin. Se compone nicamente de cdigo PHP, no genera cdigo HTML ni muestra resultados directamente. Pueden considerarse como la representacin de las reglas del negocio. template.php
PedroJimnez
ComercioyNegociosDigitales(BSI512)
20131
<!DOCTYPEhtml> <htmllang=es419> <head> <metacharset="utf8"/> <linkrel="stylesheet"type="text/css"href="estilo.css"/> <title>Aleatorios</title> </head> <body> <!NumerosAleatorios(ysuacumulado)> <h1>Resultados</h1> <hr/> <pre> <?phpprint_r($resultado)?> <hr/> </pre> </body> </html>
estilo.css
/ * * * / H o j ad eE s t i l o
b o d y{ b a c k g r o u n d :# D 4 E 6 F 4 ; / / t e x t a l i g n :c e n t e r ; } t a b l e{ b o r d e r s t y l e :s o l i d ; b o r d e r w i d t h :2 p x ; t e x t a l i g n :c e n t e r ; / / m a r g i n l e f t :a u t o ; / / m a r g i n r i g h t :a u t o ; } t h ,t d{ b o r d e r s t y l e :s o l i d ; b o r d e r w i d t h :1 p x ; b a c k g r o u n d c o l o r :w h i t e ; } t h{ b a c k g r o u n d c o l o r :# C 0 C 0 C 0 ; }
index.php
< ? p h p
PedroJimnez
ComercioyNegociosDigitales(BSI512)
20131
/ * * * * * /
L l a m aa la r c h i v od ed e f i n i c i nd el ac l a s e S ei n s t a n c i al ac l a s e S el l a m aa la r c h i v oq u eg e n e r al o sr e s u l t a d o s
NumerosAleatorios.php(v1) Utilizacin de la pseudovariable '$this' para llamar a variables y mtodos dentro del contextodelobjeto. Utilizacin del mtodo constructor ( _ _ construct ), que se ejecuta automticamente al instanciar la clase. Existe tambin el mtodo destructor ( _ _ destruct ) que se ejecuta cuandoelobjetosedestruyealfinaldelscriptoutilizandounset($obj). Elmtodoconstructorsepuedeutilizarparadefinirvaloresinicialesdeunobjeto.
< ? p h p c l a s sN u m e r o s A l e a t o r i o s { / /D e f i n i c i nd ep a r m e t r o s p r i v a t e$ n u m e r o s=a r r a y ( ) ;
PedroJimnez
ComercioyNegociosDigitales(BSI512)
20131
/ /D e f i n i c i nd em t o d o s / /S ed e f i n ee lt a m a oon m e r od ee l e m e n t o sd el am a t r i z( d e f a u l t :5 ) p u b l i cf u n c t i o n_ _ c o n s t r u c t ( $ n=5 ){ f o r ( $ i=0 ;$ i<$ n ;$ i + + ){ $ x=r a n d ( 9 , 9 ) ; $ t h i s > n u m e r o s [ $ i ]=$ x ; $ t h i s > s i g n o s [ $ i ]=$ t h i s > o b t e n e r S i g n o ( $ x ) ; } } p r i v a t ef u n c t i o no b t e n e r S i g n o ( $ n ){ i f ( $ n<0 ){ r e t u r n' ' ; }e l s e i f( $ n>0 ){ r e t u r n' + ' ; }e l s e{ r e t u r n' 0 ' ; } } }
PedroJimnez
ComercioyNegociosDigitales(BSI512)
20131
NumerosAleatorios.php(v3) Seagregaelsiguientemtodopblicoaladefinicindelaclase.
< ? p h p c l a s sN u m e r o s A l e a t o r i o s { . . . p u b l i cf u n c t i o nm o s t r a r R e s u l t a d o s ( ){ $ r e s u l t a d o=a r r a y ( ) ; f o r e a c h ( $ t h i s > n u m e r o sa s$ i = > $ j ){ $ r e s u l t a d o [ $ i ] [ 0 ]=$ t h i s > n u m e r o s [ $ i ] ; $ r e s u l t a d o [ $ i ] [ 1 ]=$ t h i s > s i g n o s [ $ i ] ; } r e t u r n$ r e s u l t a d o ; } }
index.php(v2) Se llama al mtodo pblico que se acaba de crear y se almacena el resultado en una variable.
< ? p h p r e q u i r e _ o n c e' N u m e r o s A l e a t o r i o s . p h p ' ; $ r e s u l t a d o=n e wN u m e r o s A l e a t o r i o s ( ) ; $ t a b l a=$ r e s u l t a d o > m o s t r a r R e s u l t a d o s ( ) ; r e q u i r e _ o n c e' t e m p l a t e . p h p ' ;
template.php(v2) Utilizando un ciclo 'foreach'anidado para recorrer los valoresque componen la matriz se obtienenloselementosparallenarloscamposdelastablas. Elresultadoesunatablacompuestade2columnas(valoresysigno). Latablanotieneencabezados.
<!DOCTYPEhtml> <htmllang=es419> <head> <metacharset="utf8"/>
PedroJimnez
ComercioyNegociosDigitales(BSI512)
20131
<linkrel="stylesheet"type="text/css"href="estilo.css"/> <title>Aleatorios</title> </head> <body> <!NumerosAleatorios(ysuacumulado)> <h1>Resultados</h1> <hr/> <pre> <?php echo'<table>' foreach($tablaas$registro){ echo'<tr>' foreach($registroas$dato){ echo"<td>{$dato}</td>" } echo'</tr>' } echo'</table>' ?> <?phpprint_r($tabla)?> <?phpprint_r($resultado)?> <hr/> </pre> </body> </html>
NumerosAleatorios.php(v4) Seagregaelsiguientecdigoparagenerarlosencabezadosdelatabladesdelaclase
< ? p h p c l a s sN u m e r o s A l e a t o r i o s { . . . p u b l i cf u n c t i o nm o s t r a r R e s u l t a d o s ( ){ $ r e s u l t a d o=a r r a y ( ) ; f o r e a c h ( $ t h i s > n u m e r o sa s$ i = > $ j ){ $ r e s u l t a d o [ $ i ] [ ' N u m e r o s ' ]=$ t h i s > n u m e r o s [ $ i ] ; $ r e s u l t a d o [ $ i ] [ ' S i g n o ' ]=$ t h i s > s i g n o s [ $ i ] ; } r e t u r n$ r e s u l t a d o ; } }
PedroJimnez
ComercioyNegociosDigitales(BSI512)
20131
template.php(v3) Se utiliza la funcin array_keys( ) para obtener las llaves de la matriz asociativa quese acabadecrear. Elresultadoesunatablaconsusrespectivosencabezados(valoresysigno).
<?php echo'<table>' echo'<tr>' foreach(array_keys($tabla[0])as$i){ echo"<th>{$i}</th>" } echo'</tr>' foreach($tablaas$registro){ echo'<tr>' foreach($registroas$dato){ echo"<td>{$dato}</td>" } echo'</tr>' } echo'</table>' ?> <?phpprint_r($tabla)?> <?phpprint_r($resultado)?> </pre> </body> </html>
NumerosAleatorios.php(v5) Paraincorporarelclculodelacumuladoosumaseinsertaelsiguientecdigo.
< ? p h p c l a s sN u m e r o s A l e a t o r i o s { / /D e f i n i c i nd ep a r m e t r o s p r i v a t e$ n u m e r o s=a r r a y ( ) ; p r i v a t e$ s i g n o s=a r r a y ( ) ;
/ /D e f i n i c i nd em t o d o s
PedroJimnez
ComercioyNegociosDigitales(BSI512)
20131
/ /S ed e f i n ee lt a m a oon m e r od ee l e m e n t o sd el am a t r i z( d e f a u l t :5 ) p u b l i cf u n c t i o n_ _ c o n s t r u c t ( $ n=5 ){ $ t e m p=0 ; f o r ( $ i=0 ;$ i<$ n ;$ i + + ){ $ x=r a n d ( 9 , 9 ) ; $ t h i s > n u m e r o s [ $ i ]=$ x ; $ t h i s > s i g n o s [ $ i ]=$ t h i s > o b t e n e r S i g n o ( $ x ) ; $ t e m p+ =$ x ; $ t h i s > a c u m u l a d o [ $ i ]=$ t e m p ; } } p r i v a t ef u n c t i o no b t e n e r S i g n o ( $ n ){ i f ( $ n<0 ){ r e t u r n' ' ; }e l s e i f( $ n>0 ){ r e t u r n' + ' ; }e l s e{ r e t u r n' 0 ' ; } } p u b l i cf u n c t i o nm o s t r a r R e s u l t a d o s ( ){ $ r e s u l t a d o = a r r a y ( ) ; f o r e a c h ( $ t h i s > n u m e r o sa s$ i = > $ j ){ $ r e s u l t a d o [ $ i ] [ ' N u m e r o s ' ]=$ t h i s > n u m e r o s [ $ i ] ; $ r e s u l t a d o [ $ i ] [ ' S i g n o ' ]=$ t h i s > s i g n o s [ $ i ] ; $ r e s u l t a d o [ $ i ] [ ' A c u m u l a d o ' ]=$ t h i s > a c u m u l a d o [ $ i ] ; } r e t u r n$ r e s u l t a d o ; } }
Se"limpia"eltemplate.php,paraqueslosemuestrelatablacreada. ResultadoFinal.
PedroJimnez
ComercioyNegociosDigitales(BSI512)
20131
PedroJimnez