Ver Mensaje Individual
  #1 (permalink)  
Antiguo 22/04/2007, 12:53
reimonfranco
 
Fecha de Ingreso: febrero-2006
Mensajes: 858
Antigüedad: 18 años, 2 meses
Puntos: 4
Encuesta,, no tabla

Hola, tengo estos archivos para una encuesta:

admin.php
Código PHP:
<?
include ("funciones.php");
?>
<link href="estilo.css" rel="stylesheet" type="text/css">

<table border="1" bordercolor="000000" cellspacing="0" width="300" align="center" bgcolor="FFFFFF">
<tr>
<th bgcolor="000000"  class="up">Encuesta!</th>
</tr>
<tr>
<td>
<table width="100%">
        <tr>
          <td>
<?
if($_POST['submit'] && $_POST['accion'] == 'titulo'){
?>
    <table border=0 cellspacing="0">
    <form action="admin.php" method="POST" name="f1">
    <tr><td>Titulo de la encuesta:</td><td><input type="text" name="titulo" value="<? echo $_POST['titulo'];?>" readonly></td></tr>
    <input type="hidden" name="titulo" value="<? echo $_POST['titulo'];?>">
    <input type="hidden" name="cantidad" value="<? echo $_POST['cantidad'];?>">
<?
$cantidad 
$_POST['cantidad'];
$i 1;
while(
$i <= $cantidad){
  echo 
"<tr><td>Opción $i</td><td><input type='text' name='$i'></td></tr>";
  
$i++;
}
?>
    <tr><td><input type="hidden" name="accion" value="opciones"></td><td><input type="submit" name="submit"   value="Siguiente" class="botones"></td></tr>
    </form>
    </table>
<?
}else{
    if(
$_POST['submit'] && $_POST['accion'] = 'opciones'){
        
$conexion conectar();
        
$cantidad $_POST['cantidad'];
        
$i 1;
        
$sql "insert into encuestas(titulo) values('$_POST[titulo]')";
        
$res query($sql);
        
$id obtener_titulo_id();
        while(
$i <= $cantidad){
            
$sql "insert into preguntas(encuesta, pregunta, votos) values($id,'$_POST[$i]',0)";
            
query($sql);
            
$i++;
        }
        
desconectar($conexion);
        echo 
"Grabando Datos...";
        
redireccionar(2,'encuestas.php');
    }else{
?>
<table border=0 cellspacing="0">
<form action="admin.php" method="POST" name="f1">
<tr><td>Titulo de la encuesta:</td><td><input type="text" name="titulo"></td></tr>
<tr><td>Cantidad de opciones:</td><td><input type="text" name="cantidad" size="3" maxlengtH="3"></td></tr>
<tr><td><input type="hidden" name="accion" value="titulo"></td><td><input type="submit" name="submit" value="Siguiente" class="botones"></td></tr>
</form>
</table>
<?
    
}
}
?>
          </td>
        </tr>
</table>
</td>
</tr>
</table>
encuestas.php
Código PHP:
<?
include("funciones.php");
?>
<link href="estilo.css" rel="stylesheet" type="text/css">
<script>
function set_cookie(){
    document.encuestas.submit()
    //abrir();
}

function abrir() {
    window.open("setear.php",'1','width=10,height=10');
}
</script>

<table border="1" bordercolor="000000" cellspacing="0" width="200" align="center" bgcolor="FFFFFF">
<tr>
<th bgcolor="000000"  class="up">Encuesta!</th>
</tr>
<tr>
<td>
<table width="100%">
        <tr>
          <td>
<table border=0 cellspacing="0">
<?
$conexion 
conectar();
$cook $_COOKIE['votot'];
$encuesta $_POST['encuesta'];
$pregunta $_POST['pregunta'];
if(
$cook == ""){
    if(
$encuesta == "" && $pregunta == ""){
        echo 
"<form name='encuestas' action='$_SERVER[PHP_SELF]' method='POST'>";
        echo 
obtener_titulo();
        echo 
obtener_preguntas();
        echo 
"<tr><td align='right'><input type='button' value='Votar' class='botones' OnClick='set_cookie()'></td></tr>";
        echo 
"</form>";
    }else{
        
update($encuesta,$pregunta);
    }
}else{
    echo 
mostrar_resultados();
    echo 
"<tr><td>Ya has votado gracias</td></tr>";
}
desconectar($conexion);
?>
</table>
          </td>
        </tr>
</table>
</td>
</tr>
</table>
<br><br>
<center><a href="admin.php">[Agregar una encuesta]</a></center>
estilo.
body {

scrollbar-face-color: gray;
scrollbar-highlight-color: ffffff;
scrollbar-shadow-color: black;
scrollbar-3dlight-color: white;
scrollbar-arrow-color: white;
scrollbar-track-color: gray;
scrollbar-darkshadow-color: gray;
font-family: Verdana;
font-size: 10px;
color: #FFFFFF;
}
body,td,th {
font-family: Verdana;
font-size: 10px;
color: #000000;
}
a:link {
color: #000000;
text-decoration:none;
}
a:visited {
color: #000000;
text-decoration:none;
}
a:hover {
color: #000000;
text-decoration:none;
}
a:active {
color: #000000;
text-decoration:none;
}
.botones {
font-family: Verdana;
font-size: 8pt;
color: #000000;
background-color: white;
border-color: gray;
border-top-width: 1pix;
border-right-width: 1pix;
border-bottom-width: 1pix;
border-left-width: 1pix
}
.td {
border: #000000;
border-style: solid;
border-top-width: 1px;
border-right-width: 1px;
border-bottom-width: 0px;
border-left-width: 1px;
background-color: #FFFFFF
}

.td1 {
border: #000000;
border-style: solid;
border-top-width: 1px;
border-right-width: 1px;
border-bottom-width: 0px;
border-left-width: 1px;
background-color: #F0F0F0;
}
.copyrght{
font-family: Verdana;
font-size: 10px;
color: #ffffff;
}
.titulo{
background-color: #cdcec1;
}
.general{
font-family: Verdana;
font-size: 10px;
color: #000000;
}

.up{
font-family: Verdana;
font-size: 10px;
color: #FFFFFF;
background-color: #000000;
}

funciones.php

Código PHP:
<?
$id 
0;
$preguntas['0'] = "";
function 
conectar(){
    
$host "localhost";
    
$usuariodb "usuario";
    
$pwddb "contraseña*";
    
$db "base";
    
$enlace mysql_connect($host,$usuariodb,$pwddb);
    if (!
$enlace) {
        die(
'No conectado: ' mysql_error());
    }
    
$seldb mysql_select_db($db,$enlace);
    if (!
$seldb) {
        die (
'No se puede usar $db: ' mysql_error());
    }
    return 
$enlace;
}

function 
desconectar($conexion){
    
mysql_close($conexion);
}

function 
query($sql){
    
$res mysql_query($sql) or die (mysql_error());
    return 
$res;
}

function 
obtener_titulo(){
    
$id obtener_titulo_id();
    
$sql "select titulo from encuestas where id = $id";
    
$res query($sql);
    
$fila mysql_fetch_array($res);
    
$id $fila['id'];
    
$titulo "<tr><td>".$fila['titulo']."</td></tr>";
    return 
$titulo;
}

function 
obtener_titulo_id(){
    
$sql "select max(id) as id from encuestas";
    
$res query($sql);
    
$fila mysql_fetch_array($res);
    
$id $fila['id'];
    return 
$id;
}

function 
obtener_preguntas(){
    
$id obtener_titulo_id();
    
$sql "select id, pregunta from preguntas where encuesta = $id order by id";
    
$res query($sql);
    while(
$fila mysql_fetch_array($res)){
        echo 
"<tr><td><input type='radio' name='pregunta' value='".$fila['id']."'>".$fila['pregunta']."</td></tr>";
    }
    echo 
"<input type=hidden name='encuesta' value='$id'>";
}

function 
total_votos($id){
    
$sql "select sum(votos) as cantidad from preguntas where encuesta = $id";
    
$res query($sql);
    
$fila mysql_fetch_array($res);
    return 
$fila['cantidad'];
}

function 
mostrar_resultados(){
    
$id obtener_titulo_id();
    
$total_votos total_votos($id);
    echo 
"<tr><td><B>".obtener_titulo()."</B></td></tr>";
    
$sql "select pregunta, votos from preguntas where encuesta = $id";
    
$res query($sql);
    if(
$total_votos 0){
            while(
$fila mysql_fetch_array($res)){
                
$graf = ($fila['votos'] * 100) / $total_votos;
                
$graf round($graf);
                echo 
"<tr><td>".$fila['pregunta']." - ".$graf."% ".$fila['votos']." V </td></tr>";
                echo 
"<tr><td><table border=0><tr><td bgcolor='FF0000' width='$graf' height=5></td></tr></table></td></tr>";
        }
    }else{
              
$graf 0;
            while(
$fila mysql_fetch_array($res)){
                echo 
"<tr><td>".$fila['pregunta']." - ".$graf."% ".$fila['votos']." V </td></tr>";
                   echo 
"<tr><td><table border=0><tr><td bgcolor='FF0000' width='$graf' height=5></td></tr></table></td></tr>";
        }
    }
}

function 
obtener_votos($encuesta,$pregunta){
    
$sql "select votos from preguntas where encuesta = '$encuesta' and id = '$pregunta'";
    
$res query($sql);
    
$votos mysql_fetch_array($res);
    return 
$votos['votos'];
}

function 
update($encuesta,$pregunta){
    
$votos obtener_votos($encuesta,$pregunta);
    
$votos++;
    
$sql "UPDATE preguntas SET votos = $votos where encuesta = '$encuesta' and id = '$pregunta'";
    
$res query($sql);
    if(
$res){
        echo 
"Procesando...";
        
redireccionar('1','setear.php');
    }else{
        echo 
"Error al actualizar la Base de datos.";
    }
}

function 
redireccionar($time,$url){
    print 
"<meta http-equiv=Refresh content=\"$time ; url=$url\">";
}

?>
setear.php
Código PHP:
<?php
include("funciones.php");
$conexion conectar();
$id obtener_titulo_id();
//setcookie("voto", votaste, time()+86400,"/","");
setcookie("votot"$idtime()+60000,"/","");
desconectar($conexion);
redireccionar(0,'encuestas.php');
?>

pero entonces me faltan las dos tablas :

preguntas, y respuestas.. pero no se como hacerlas.. alguien me las podria pasar

saludos
__________________
*La amistad se multiplica cuando se divide*