<?php $a = [2, 5, 2, 3, 5]; foreach ($a as $v) { echo $v . "<br/>"; } $a[7]=9; $a['juan']='pablo'; foreach ($a as $clave=>$valor) { echo $clave." - ".$valor . "<br/>"; } for($i=0;$i<count($a);$i++){ echo $a[$i] . "<br/>"; } ?>
Mes: abril 2018
Php for
<?php for ($i = 0; $i < 10; $i += 2) { echo $i . "<br/>"; } $i = 0; for (; $i < 10; $i += 2) { echo $i . "<br/>"; } $i = 0; for (; $i < 10;) { echo $i . "<br/>"; $i += 2; } $i = 0; for (;;) { echo $i . "<br/>"; $i += 2; if ($i > 10) { break; } } echo $i; ?>
Bucle while
<?php $a = 0; while ($a < 10) { echo "$a<br/>"; $a += 2; } $a = 0; while ($a < 10) { if ($a % 2 == 0) { echo "**<br/>"; } else { echo "*****<br/>"; } $a++; } $a = 0; while ($a < 10) { if ($a % 2 == 0) { ?> <p style="background-color: black">.......</p> <?php } else { ?> <p style="background-color: red">.......</p> <?php } $a++; } ?>
Else if y switch
<?php $a = 1; if ($a < 5) { echo '<div style="background-color: red"> <h2>Menor de 5</h2> <h2>No estás autorizado a entrar</h2> </div>'; } elseif ($a < 10) { echo "Menor de 10"; } elseif ($a < 20) { echo "Menor de 20"; } else { echo "Mayor de 20"; } switch ($a) { case 1: echo "Vale uno"; break; case 2: case 3: echo "Vale 2 o 3"; break; default: echo "Vale más de tres"; } ?>
PHP if
<?php $a = 1; if ($a < 5) { echo '<div style="background-color: red"> <h2>Menor de 5</h2> <h2>No estás autorizado a entrar</h2> </div>'; } else { echo "Mayor de 5"; } if ($a < 5) { ?> <div style="background-color: red"> <h2>Menor de 5</h2> <h2>No estás autorizado a entrar</h2> </div> <?php } else { ?> <h2>Menor de 5</h2> <?php } if ($a < 5): ?> <div style="background-color: red"> <h2>Menor de 5</h2> <h2>No estás autorizado a entrar</h2> </div> <?php else: ?> <h2>Menor de 5</h2> <?php endif ?>
Ejemplos create view
Primero creamos una vista base:
create view country_payment as select country, payment.* from country join city using (country_id) join address using (city_id) join customer using (address_id) join payment using (customer_id)
Después nos basamos en esta para crear las siguientes:
create view country_total as select country, sum(amount) total from country_payment group by country create view country_month as select country, month(payment_date) month, count(payment_id) total from country_payment group by country, month