Buenas necesito ayuda con php ya que tengo problemas con dos tipos de ejercicios.
No se necesita mysql, solamente que al enviarlo se muestre el resultado como si fuese una nueva.
El primero, es que que te piden un nº y segun el nº ingresado te devuelve en una tabla tantas filas como nº sea ingresado. Si es par sera de un color todo y si es impar el introducido de otro.
Tengo lo siguiente, que claramente no funciona.
Código PHP:
<!DOCTYPE html>
<html>
<body>
<head>
<style type="text/css">
.color{
color:green;
}
</style>
</head>
<?php
$n1=0;
$numero =$_POST['numero'];
valor($numero);
function valor($numero){
if($numero % 2 ==0)
echo "<p class='color'> $numero </p>";
}
if($_POST['numero'])
{
echo "<table border=1>";
echo "<tr>";
echo "<td> valor($numero) </td>";
for ($n1=0; $n1<=$numero;$n1++){
echo "</tr>";
}
echo "</table>";
}else
{
echo "da error";
}
?>
<form method="post" action="<?=$_SERVER['PHP_SELF']?>" >
<input type="text" name="numero" />
<input type="submit" name="enviar" />
</form>
</body>
</html>
Sin tablas, consigo sacar el nº con color y funnciona, mediante el siguiente codigo:
Código PHP:
<!DOCTYPE html>
<html>
<body>
<head>
<style type="text/css">
.color{
color:green;
}
.color2{
color:red;
}
</style>
</head>
<form method="post">
<h3>Introuce un nº</h3>
<input type="text" name="num" />
<input type="submit" name="enviar" />
</form>
<?php
if(isset($_POST['enviar'])){
$n=$_POST['num'];
}
if($n % 2 == 0){
echo "<p class='color'> $n </p>";
}else{
echo "<p class='color2'> $n </p>";
}
?>
</body>
</html>
Este es el segundo ejercicio.
Mediante un formulario, los datos enviados se añadan en la siguiente pagina en forma de tabla con sus titulos.
He puesto un tipo de formulario y le intente poner las tablas de salida pero nada.
La cuestion es poner por ejemeplo, nombre, apellidos, edad, un checkbox de algo,email y un radiobutton y que lo muestre en una tabla...pero no doy con el modo.
Código PHP:
<html>
<body>
<?php
if($_POST['nombre'] && $_POST['apellido1'] && $_POST['apellido2'])
{
echo ('<h1>Bienvenido, '<br />');
<?php
<table border ="2">
<tr> <th>nombre </th>
<th>apellido</th>
<th> estado civil</th>
<th> dormitorios</th>
<th> precio</th>
</tr>
<tr>
<td> <?php echo ($_POST['nombre']) ?>;</td>
<td> <?php echo ($_POST['epllido']) ?>;</td>
<td> <?php echo ($_POST['civil']) ?>;</td>
<td> <?php if ($_POST['dormitorios']== '1') {
echo ('1')
}
if ($_POST['dormitorios']== '2') {
echo ('2')
}
?> </td>
</tr>
</table>
?>
}
else
{
if($_POST AND empty($_POST['nombre']) || empty($_POST['apellido1']) || empty($_POST['apellido2']))
{
echo ('<span style="color:red"><b>Introduzca los siguientes datos:<br />');
if($_POST['nombre']=='')
echo ('- nombre<br />');
if($_POST['apellido1']=='')
echo ('- primer apellido<br />');
if($_POST['apellido2']=='')
echo ('- segundo apellido<br />');
echo ('</b></span><br />');
}
?>
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<fieldset>
<label for="nombre">Nombre</label>
<input type="text" id="nombre" name="nombre" />
<br />
<label for="apellidos">Primer apellido</label>
<input type="text" id="apellido1" name="apellido1" />
<br />
<label for="apellidos">Segundo apellido</label>
<input type="text" id="apellido2" name="apellido2" />
<br />
<label for="civil">Diga su estado civil:</label>
<input type="radio" name="radio" value="soltero"/> Soltero
<input type="radio" name="radio" value="casado"/>Casado
<br />
<select name="año">
<option> Año nacimiento </option>
<?php
for($i=1900;$i<2017;$i++){
echo("<opton>".$i ."<option>");
}
?>
</select>
<br /><br />
<label for="direccion">Dirección</label>
<input type="text" name="direccion" /><br/>
<label for="dormitorios">Numero de dormitorios:</label>
<input type="radio" name="dormitorios" value="1"/> 1
<input type="radio" name="dormitorios" value="2"/>2
<input type="radio" name="dormitorios" value="3"/>3
<input type="radio" name="dormitorios" value="4"/>4
<input type="radio" name="dormitorios" value="5"/>5 <br />
<label for="precio">Precio:</label>
<input type="text" name="precio" />€ <br /><br />
<label for="texto">Texto</label>
<textarea rows="10" cols="40" name="texto" minlength="10"></textarea>
<input type="submit" name="enviar" value="Enviar" />
</fieldset>
</form>
<?php
}
?>
</body>
</html>
No domino mucho php, cualquier ayuda es bien recibida.
Un saludo.