Tema: foreach
Ver Mensaje Individual
  #7 (permalink)  
Antiguo 21/07/2009, 14:59
Said012784
 
Fecha de Ingreso: julio-2009
Mensajes: 13
Antigüedad: 14 años, 10 meses
Puntos: 0
Respuesta: foreach

Para que me entiendas pongo mis scripts
el primero encuesta.php, aqui pregunto al usuario como se llamara la encuesta y de cuantas preguntas sera la encuesta:

Código PHP:
<?php /* Created on: 20/07/2009 */ ?>
<html>
    <head> <title>  </title> </head>
    <body bgcolor="#A6BDD4">
        <br>
        <form action="encuesta2.php" method="post">
                 <table width=35% border="0" align=center cellpadding=0 background="engranes.jpg">
                    <tr> <td colspan="2"> <font color="#0662AD"> <b><div style="text-align: center">Registro de Encuestas</div></b> </font> </td> </tr>
                    <tr> <td colspan="2"> <br> <br></td> </tr>  
                    <tr> 
                        <td align="center" height="4" width="50%"> <font color="#0662AD"> <b>Titulo de la Encuesta:</b> </font> </td> 
                        <td align="left" height="4" width="50%"><input type="text" name="tit" size="20%"></td>
                    </tr>
                    <tr> <td colspan="2"> <br></td> </tr> 
                    <tr> 
                        <td align="center" height="4" width="50%"> <font color="#0662AD"> <b>Total de Preguntas:</b> </font> </td> 
                        <td align="left" height="4" width="50%"><input type="text" name="tot_preg" size="3%"></td>
                    </tr>
                    <tr> <td colspan="2"> <br> </td> </tr>
                 <tr>
                    <td align="center" height="4" colspan="2"> <input type="submit" name="Guardar" value="Enviar" onClick="validar(this.form)"> </td>
                </tr>
            </table>
        </form>
        
        <!-- Validaciones de captura  -->
        <script LANGUAGE="JavaScript">
           
           
           function validar(form)
               {
                if (form.tit.value == "")
                 { 
                     alert("Por favor introduzca el nombre de la encuesta a registrar");
                    form.tit.focus(); 
                    return;
                }
                if (form.tot_preg.value == "")
                 { 
                     alert("Por favor introduzca el total de preguntas a registrar");
                    form.tot_preg.focus(); 
                    return;
                }
                if(confirm('¿Desea enviar la información proporcionada al siguiente formulario?'))  
                   form.submit();     
                 else return;
                
                
            }
        </script>        
    </body>
</html>
luego esta información la mando al script encuesta2.php, aqui recibo la informacion del script anterior y guardo el nombre de la encuesta y el total de preguntas y le pregunto al usuario cuales son sus preguntas y cuantas respuestas quiere por pregunta

Código PHP:
<?php /* Created on: 20/07/2009 */      
require('conexion_bd.php');    
    
$tit $_POST["tit"];
    
$tot_preg $_POST["tot_preg"];     

    
//Obtenemos la fecha del sistema
    
$fecha date("d-m-Y");
    
    
$sql "INSERT INTO encuestas (titulo, total_preg, fecha) VALUES ('$tit', '$tot_preg', '$fecha') ";
    
$sql mysql_query($sql) or die ("No fue posible agregar la encuesta");    
                         
    
$sqla "SELECT total_preg FROM encuestas WHERE id_titulo=(SELECT max(id_titulo) FROM encuestas)";
    
$sqlb mysql_query($sqla) or die ("No fue posible seleccionar la encuesta");
    
$row=mysql_result($sqlb0);
    
$tot=$row;
    
            
           
?>
<html>
    <head> <title>  </title> </head>
    <body bgcolor="#A6BDD4">
        <br>
        <form action="encuesta3.php" method="post">
                 <table width=35% border="0" align=center cellpadding=0 background="engranes.jpg">
                    <tr> <td colspan="2"> <font color="#0662AD"> <b><div style="text-align: center">Registro de Preguntas</div></b> </font> </td> </tr>
                    <tr> <td colspan="2"> <br> <br></td> </tr> 
                    <?php
                          
for($i=1;$i<=$tot;$i++){ 
                        
?>  
                    
                    <tr> 
                        <td align="center" height="4" width="50%"> <font color="#0662AD"> <b>Pregunta<?php echo $i?></b> </font> </td> 
                        <td align="left" height="4" width="50%"><input type="text" name="p[]" size="20%"></td>
                    </tr>
                    <tr> 
                        <td align="center" height="4" width="50%"> <font color="#0662AD"> <b>Total de Respuestas <?php echo $i;?>:</b> </font> </td> 
                        <td align="left" height="4" width="50%"><input type="text" name="tot_resp[]" size="3%"></td>
                    </tr>
                    <tr> <td colspan="2"> <br> </td> </tr>
                    <?php ?>
                    <tr> <td colspan="2"> <br> </td> </tr>
                 <tr>
                    <td align="center" height="4" colspan="2"> <input type="submit" name="Guardar" value="Enviar"> </td>
                    
                </tr>
            
            </table> 
            

        </form>
            
    </body>
</html>
y esto lo mando al script encuesta3.php, aqui es donde estan los foreach, y en este capturar la informacion en la tabla preguntas y pedirle al usuario las respuestas de cada pregunta solicitada anteriormente

Código PHP:
<?php /* Created on: 20/07/2009 */
require('conexion_bd.php');    

$sqld "SELECT total_preg FROM encuestas WHERE id_titulo=(SELECT max(id_titulo) FROM encuestas)";
$sqle mysql_query($sqld) or die ("No fue posible seleccionar la encuesta");
$rowf=mysql_result($sqle0);
$totg=$rowf;

$sqla "SELECT max(id_titulo) FROM encuestas";
$sqlb mysql_query($sqla);
$row=mysql_result($sqlb0);
$id=$row;

 
foreach(
$_POST['p'] as $preg)
{
    foreach (
$_POST['tot_resp'] as $tot)
    { 
        
$sql "INSERT INTO preguntas(id_titulo, pregunta, total_resp) VALUES ('$id' , '$preg', '$tot')";
        
$sql mysql_query($sql);
    }
}

?>
<html>
    <head> <title>  </title> </head>
    <body bgcolor="#A6BDD4">
        <br>
        <form action="encuesta4.php" method="post">
                 <table width=35% border="0" align=center cellpadding=0 background="engranes.jpg">
                    <tr> <td colspan="2"> <font color="#0662AD"> <b><div style="text-align: center">Registro de Respuestas</div></b> </font> </td> </tr>
                    <tr> <td colspan="2"> <br> <br></td> </tr>
                    <?php for($i=1;$i<=$tot;$i++){ ?>  
                    <tr> 
                        <td align="center" height="4" width="50%"> <font color="#0662AD"> <b>Respuesta <?php echo $i?></b> </font> </td> 
                        <td align="left" height="4" width="50%"><input type="text" name="r<?php echo $i;?>" size="20%"></td>
                    </tr>
                    <tr> <td colspan="2"> <br> </td> </tr>
                    <?php ?>
                    <tr> <td colspan="2"> <br> </td> </tr>
                 <tr>
                    <td align="center" height="4" colspan="2"> <input type="submit" name="Guardar" value="Enviar"> </td>
                </tr>
            </table> 
            <input name="tit" type="hidden" value="<?php echo $tit;?>">
            <input type="hidden" name="tot_preg" value="<?php echo $tot_preg;?>">
            <input type="hidden" name="p" value="<?php echo $tot_preg;?>">

        </form>
            
    </body>
</html>

donde esta el codigo html todavia esta mal ese no lo he revisado ahorita solamente estoy con lo de php lo que esta al principio

Última edición por Said012784; 21/07/2009 a las 15:08