Foros del Web » Programando para Internet » PHP »

Problema con variables de session que es un array

Estas en el tema de Problema con variables de session que es un array en el foro de PHP en Foros del Web. Os voy a poner el código, no esta muy otimizado pero bueno, como no sale bien tampoco quiero meterme a más hasta que supere la ...
  #1 (permalink)  
Antiguo 23/05/2006, 04:29
 
Fecha de Ingreso: abril-2006
Mensajes: 14
Antigüedad: 18 años
Puntos: 0
Problema con variables de session que es un array

Os voy a poner el código, no esta muy otimizado pero bueno, como no sale bien tampoco quiero meterme a más hasta que supere la crisis.

Código PHP:
function push()
{
        
session_start();
        global 
$_CONT_BUTACAS$_BUTACAS$_SALA,$_HORA,$_FECHA,$_USUARIO,$_CINE;
        
//$arr_but=array();
        
$contenido="";
        if (
$_CONT_BUTACAS 9)
        {
            
$rep 0;
            for (
$i=0;$i<count($_BUTACAS);$i++)
            {
                if (
$_BUTACAS[$i]==$_REQUEST['id'])
                {
                    
$rep=1;
                }
            }
            if (
$rep == 0)
            {
                
$link=conectadb();
                
$pos=0;
                for (
$q=0;$q<count($_BUTACAS);$q++)
                {
                    
$pos=$q;
                }
                
$_BUTACAS[$pos+1]=$_REQUEST['id'];
                
$_CONT_BUTACAS$_CONT_BUTACAS 1;
                
$_SALA=$_REQUEST['sala'];
                
$_HORA=$_REQUEST['hora'];
                
$_FECHA=date("dmY");
                
$_USUARIO=$_REQUEST['user'];
                
$_CINE=$_REQUEST['cine'];
                
$sql="select * from prc_butaca where id_sala=".$_SALA;
                
$res=mysql_query($sql,$link) or die("<html>
                                                <head>
                                                <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
                                                <link href=\"estilo.css\" rel=\"stylesheet\" type=\"text/css\">
                                                </head>
                                                    <body>
                                                    <br><br><br><br>
                                                    <table width=\"800\" border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"3\" class=\"tableb\">
                                                    <tr><td>
                                                    <h3 align=\"center\">No se pudo completar la operación:"
.mysql_error()."<br></h3>
                                                    <h3 align=\"center\">No se puedo completar la operación:"
.$sql."<br></h3>
                                                    
                                                    </td></tr></table>
                                                </body>
                                            </html>"
);
                
$arr_but = array();
                while (
$but mysql_fetch_array($res,MYSQL_ASSOC))
                {
                    
$arr_but[]=$but['id'];
                }
                
$sql="select * from prc_sala where id=".$_SALA;
                
$resala=mysql_query($sql,$link) or die("<html>
                        <head>
                        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
                        <link href=\"estilo.css\" rel=\"stylesheet\" type=\"text/css\">
                        </head>
                            <body>
                            <br><br><br><br>
                            <table width=\"800\" border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"3\" class=\"tableb\">
                            <tr><td>
                            <h3 align=\"center\">No se pudo completar la operación:"
.mysql_error()."<br></h3>
                            <h3 align=\"center\">No se puedo completar la operación:"
.$sql."<br></h3>
                            </td></tr></table>
                        </body>
                    </html>"
);
                
$dat_sala=mysql_fetch_array($resala,MYSQL_ASSOC);
                
$filas=$dat_sala['fila'];
                
$columnas=$dat_sala['columna'];
                
$contenido .="<html>
                            <head>
                            <title>Documento sin t&iacute;tulo</title>
                            <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
                            <link href=\"estilo.css\" rel=\"stylesheet\" type=\"text/css\">
                            </head>
                            
                            <body>
                            <table width=\"400\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"tableb\">
                              <tr>
                                <td>"
;
                
$contenido .="<div style=\"padding-top:20px; padding-left:20px; padding-right:20px; padding-bottom:20px; background-color:#FFFFFF\">
                            <p align=\"center\" style=\"font-family:'Trebuchet MS', Verdana, Tahoma; font-size:10px; font-weight:bold; color:#000000\">Hoy::::Mañana::::Pasado Mañana &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{HORAS}</p>
                        </div>
                            <div style=\"padding-top:20px; padding-left:20px; padding-right:20px; padding-bottom:20px; background-color:#FFFFFF\"><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"
;
                
$butid=reset($arr_but);
                for (
$i=1;$i<=$filas;$i++)
                {
                  
$contenido.="<tr>";
                    for(
$x=1;$x<=$columnas;$x++)
                     {
                        
$butid=current($arr_but);
                        
$sql="select * from prc_compran where id_butaca=".$butid." AND fecha= ".$_FECHA." AND hora=".$_HORA;
                        
$res=mysql_query($sql,$link) or die("<html>
                                                                <head>
                                                                <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
                                                                <link href=\"estilo.css\" rel=\"stylesheet\" type=\"text/css\">
                                                                </head>
                                                                    <body>
                                                                    <br><br><br><br>
                                                                    <table width=\"800\" border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"3\" class=\"tableb\">
                                                                    <tr><td>
                                                                    <h3 align=\"center\">No se pudo completar la operación:"
.mysql_error()."<br></h3>
                                                                    <h3 align=\"center\">No se puedo completar la operación:"
.$sql."<br></h3>
                                                                    </td></tr></table>
                                                                </body>
                                                            </html>"
);
                        
// recorremos aqui todo el array de las butacas, y comprobamos que alguno de ellos no sea igual que la butaca que tenemos ahora en butid, si es asi
                        // en la parte del if diferenciaremos entre libre y por el usuario, si la variable esta a 0 tal y sino pascual
                        
$bingo=0;
                        
$aux=$butid;
                        for (
$m=1;$m<=count($_BUTACAS);$m++)
                        {
                            
//echo ($_BUTACAS[$m]. " == " . $butid." - ". $_REQUEST['id'] );
                            
if ($_BUTACAS[$m]==$aux)
                            {
                                
$bingo=1;
                            }
                        }
                        
//die($aux. " - ". $_BUTACAS[$m]);
                        //die("FIN");
                        
if (!mysql_num_rows($res))
                        {    
                                if (
$bingo==1)
                                {
                                    
$contenido.= "<td width=\"32\" height=\"30px\"><a href=\"comprar.php?id=".$butid."&cine=".$_CINE."&sala=".$_SALA."&hora=".$_HORA."&acc=2&user=".$_USUARIO."\"><img src=\"imagenes/butaca_usu.jpg\" border=\"0\"></a></td>";
                                }
                                else
                                {
                                    
$contenido.= "<td width=\"32\" height=\"30px\"><a href=\"comprar.php?id=".$butid."&cine=".$_CINE."&sala=".$_SALA."&hora=".$_HORA."&acc=1&user=".$_USUARIO."\"><img src=\"imagenes/butaca_libre.jpg\" border=\"0\"></a></td>";
                                }
                        }
                        else
                        {
                                
$contenido.= "<td width=\"32\" height=\"30px\"><img src=\"imagenes/butaca_ocu.jpg\" border=\"0\"></td>";
    
                        }
                        
next($arr_but);
                     }
                  
$contenido.="</tr>";
                }        
              
            
$contenido.="</table>
            <a>Continuar</a>
            </div>
            </td>
              </tr>
            </table>
            </body>
            </html>"
;
            echo 
$contenido;    
            }
            else
            {
                
            }
        }
        else
        {
        }

El problema está en que en $_BUTACAS que esta declarada como session_register("_BUTACAS") en otro script no hay manera que se le incluya en el array más de un elemento.

Es decir, que cada vez que entra solo tiene un valor, en vez de acumularlos en el array

Es en esta parte:

for ($q=0;$q<count($_BUTACAS);$q++)
{
$pos=$q;
}
$_BUTACAS[$pos+1]=$_REQUEST['id'];

Esto se podria hacer directamente con un count, pero estaba comprobando cosillas

Pero es que además descubro que tampoco me funciona el contador de butacas, por tanto las variables de sesion, las debo estar haciendo o llamando mal porque no me funcionan ninguna, ya que el contador da igual lo que haga que siempre es 1.

LAs variables de sesion las registro con session_register("VARIABLE") y las llamo como $VARIABLE.

A ver si me podeis decir donde tengo el error. qeu estoy atascadisimo

MUCHAS GRACIAS POR ADELANTADO
  #2 (permalink)  
Antiguo 23/05/2006, 04:37
Avatar de jerkan  
Fecha de Ingreso: septiembre-2005
Mensajes: 1.607
Antigüedad: 18 años, 7 meses
Puntos: 19
Si quieres trabajar con variables de sesión hazlo de la siguiente manera:
Código PHP:
<?php
$_SESSION
['nombre'] = valor;   // para crearla e instanciarla
$_SESSION['nombre'] = array();   // igual para arrays
?>
  #3 (permalink)  
Antiguo 23/05/2006, 08:00
 
Fecha de Ingreso: abril-2006
Mensajes: 14
Antigüedad: 18 años
Puntos: 0
Ok, voy a intentarlo asi.

gracias
  #4 (permalink)  
Antiguo 23/05/2006, 08:52
 
Fecha de Ingreso: abril-2006
Mensajes: 14
Antigüedad: 18 años
Puntos: 0
Código PHP:
function push()
{
        
session_start();
        global 
$_CONT_BUTACAS$_BUTACAS$_SALA,$_HORA,$_FECHA,$_USUARIO,$_CINE;
        
//$arr_but=array();
        
$contenido="";
        if (
$_SESSION['_CONT_BUTACAS'] < 9)
        {
            
$rep 0;
            for (
$i=0;$i<count($_SESSION['_BUTACAS']);$i++)
            {
                if (
$_SESSION['_BUTACAS'][$i]==$_REQUEST['id'])
                {
                    
$rep=1;
                }
            }
            if (
$rep == 0)
            {
                
$link=conectadb();
                
$pos=0;
                for (
$q=0;$q<count($_SESSION['_BUTACAS']);$q++)
                {
                    
$pos=$q;
                }
                
$_SESSION['_BUTACAS'][$pos+1]=$_REQUEST['id'];
                
$_SESSION['_CONT_BUTACAS']= $_CONT_BUTACAS 1;
                
$_SESSION['_SALA']=$_REQUEST['sala'];
                
$_SESSION['_HORA']=$_REQUEST['hora'];
                
$_SESSION['_FECHA']=date("dmY");
                
$_SESSION['_USUARIO']=$_REQUEST['user'];
                
$_SESSION['_CINE']=$_REQUEST['cine'];
                
$sql="select * from prc_butaca where id_sala=".$_SESSION['_SALA'];
                
$res=mysql_query($sql,$link) or die("<html>
                                                <head>
                                                <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
                                                <link href=\"estilo.css\" rel=\"stylesheet\" type=\"text/css\">
                                                </head>
                                                    <body>
                                                    <br><br><br><br>
                                                    <table width=\"800\" border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"3\" class=\"tableb\">
                                                    <tr><td>
                                                    <h3 align=\"center\">No se pudo completar la operación:"
.mysql_error()."<br></h3>
                                                    <h3 align=\"center\">No se puedo completar la operación:"
.$sql."<br></h3>
                                                    
                                                    </td></tr></table>
                                                </body>
                                            </html>"
);
                
$arr_but = array();
                while (
$but mysql_fetch_array($res,MYSQL_ASSOC))
                {
                    
$arr_but[]=$but['id'];
                }
                
$sql="select * from prc_sala where id=".$_SESSION['_SALA'];
                
$resala=mysql_query($sql,$link) or die("<html>
                        <head>
                        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
                        <link href=\"estilo.css\" rel=\"stylesheet\" type=\"text/css\">
                        </head>
                            <body>
                            <br><br><br><br>
                            <table width=\"800\" border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"3\" class=\"tableb\">
                            <tr><td>
                            <h3 align=\"center\">No se pudo completar la operación:"
.mysql_error()."<br></h3>
                            <h3 align=\"center\">No se puedo completar la operación:"
.$sql."<br></h3>
                            </td></tr></table>
                        </body>
                    </html>"
);
                
$dat_sala=mysql_fetch_array($resala,MYSQL_ASSOC);
                
$filas=$dat_sala['fila'];
                
$columnas=$dat_sala['columna'];
                
$contenido .="<html>
                            <head>
                            <title>Documento sin t&iacute;tulo</title>
                            <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
                            <link href=\"estilo.css\" rel=\"stylesheet\" type=\"text/css\">
                            </head>
                            
                            <body>
                            <table width=\"400\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"tableb\">
                              <tr>
                                <td>"
;
                
$contenido .="<div style=\"padding-top:20px; padding-left:20px; padding-right:20px; padding-bottom:20px; background-color:#FFFFFF\">
                            <p align=\"center\" style=\"font-family:'Trebuchet MS', Verdana, Tahoma; font-size:10px; font-weight:bold; color:#000000\">Hoy::::Mañana::::Pasado Mañana &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{HORAS}</p>
                        </div>
                            <div style=\"padding-top:20px; padding-left:20px; padding-right:20px; padding-bottom:20px; background-color:#FFFFFF\"><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"
;
                
$butid=reset($arr_but);
                for (
$i=1;$i<=$filas;$i++)
                {
                  
$contenido.="<tr>";
                    for(
$x=1;$x<=$columnas;$x++)
                     {
                        
$butid=current($arr_but);
                        
$sql="select * from prc_compran where id_butaca=".$butid." AND fecha= ".$_SESSION['_FECHA']." AND hora=".$_SESSION['_HORA'];
                        
$res=mysql_query($sql,$link) or die("<html>
                                                                <head>
                                                                <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
                                                                <link href=\"estilo.css\" rel=\"stylesheet\" type=\"text/css\">
                                                                </head>
                                                                    <body>
                                                                    <br><br><br><br>
                                                                    <table width=\"800\" border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"3\" class=\"tableb\">
                                                                    <tr><td>
                                                                    <h3 align=\"center\">No se pudo completar la operación:"
.mysql_error()."<br></h3>
                                                                    <h3 align=\"center\">No se puedo completar la operación:"
.$sql."<br></h3>
                                                                    </td></tr></table>
                                                                </body>
                                                            </html>"
);
                        
// recorremos aqui todo el array de las butacas, y comprobamos que alguno de ellos no sea igual que la butaca que tenemos ahora en butid, si es asi
                        // en la parte del if diferenciaremos entre libre y por el usuario, si la variable esta a 0 tal y sino pascual
                        
$bingo=0;
                        
$aux=$butid;
                        for (
$m=1;$m<=count($_SESSION['_BUTACAS']);$m++)
                        {
                            
//echo ($_BUTACAS[$m]. " == " . $butid." - ". $_REQUEST['id'] );
                            
if ($_SESSION['_BUTACAS'][$m]==$aux)
                            {
                                
$bingo=1;
                            }
                        }
                        
//die($aux. " - ". $_BUTACAS[$m]);
                        //die("FIN");
                        
if (!mysql_num_rows($res))
                        {    
                                if (
$bingo==1)
                                {    
                                    echo 
$_SESSION['_CONT_BUTACAS'];
                                    
$contenido.= "<td width=\"32\" height=\"30px\"><a href=\"comprar.php?id=".$butid."&cine=".$_SESSION['_CINE']."&sala=".$_SESSION['_SALA']."&hora=".$_SESSION['_HORA']."&acc=2&user=".$_SESSION['_USUARIO']."\"><img src=\"imagenes/butaca_usu.jpg\" border=\"0\"></a></td>";
                                }
                                else
                                {
                                    
$contenido.= "<td width=\"32\" height=\"30px\"><a href=\"comprar.php?id=".$butid."&cine=".$_SESSION['_CINE']."&sala=".$_SESSION['_SALA']."&hora=".$_SESSION['_HORA']."&acc=1&user=".$_SESSION['_USUARIO']."\"><img src=\"imagenes/butaca_libre.jpg\" border=\"0\"></a></td>";
                                }
                        }
                        else
                        {
                                
$contenido.= "<td width=\"32\" height=\"30px\"><img src=\"imagenes/butaca_ocu.jpg\" border=\"0\"></td>";
    
                        }
                        
next($arr_but);
                     }
                  
$contenido.="</tr>";
                }        
              
            
$contenido.="</table>
            <a>Continuar</a>
            </div>
            </td>
              </tr>
            </table>
            </body>
            </html>"
;
            echo 
$contenido;    
            }
            else
            {
                
            }
        }
        else
        {
        }

Lo he cambiado todo como me has indicaddo, y me siguen sin funcionar las variables de session cuando vuelvo a llamar al script, es decir que solo me incluye un elemento en el array, y que el contador siempre marca 1



Alguna ayudita

Muchas gracias
  #5 (permalink)  
Antiguo 23/05/2006, 12:05
 
Fecha de Ingreso: abril-2006
Mensajes: 14
Antigüedad: 18 años
Puntos: 0
NAdie? yo aqui sigo sin saber que hacer, tiene que ser una parida, pero no hay manera.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:02.