-2

I'm programming a map of theatre seats. With this code I try to show selected seats on a previous page, but I can't show all of the seats in a color and in another color the selected ones. It shows all of the seats and only one selected. Then all of the seats again, and another selected. And so on... Any suggestions? Thank you very much and here's the piece of code:

foreach ($AsientosSeleccionadosI as $claveAI=>$valorAI) {

$resultButacas = mysql_query("SELECT U.Id AS IdUbicacion, U.IdModoTeatro, U.Numero, U.Fila, U.IdSector, U.CordX, U.CordY,
                                P.IdEspectaculo, P.IdFuncion, P.IdSector, P.Precio
                                FROM TeatroAsiento U, PrecioEspectaculoSector P 
                                WHERE U.IdSector = '$Sector' 
                                AND P.IdSector = '$Sector'
                                AND P.IdFuncion = '$fn'
                                AND P.IdEspectaculo = '$id'


                       "); 

while($rowButacas=mysql_fetch_array($resultButacas)){ 
$fila = $rowButacas["Fila"];
$asiento = $rowButacas["Numero"];
$IdUbicacion = $rowButacas["IdUbicacion"];
$precio = $rowButacas["Precio"];

echo '<div id="Casilla"><label for="asiento'.$fila.''.$asiento.'">';

if($IdUbicacion == $valorAI){
echo '<div id="ubicacion" style="background-image: url(../css/images/asientosReservado.png);"><input id="asiento'.$fila.''.$asiento.'" value="'.$IdUbicacion.'"  name="asientoI[]" type="hidden"><input id="'.$IdUbicacion.''.$precio.'" value="'.$precio.'" type="hidden" name="precioseleccionI[]">';
}else{
echo '<div id="ubicacion" style="background-image: url(../css/images/asientosDisponible.png);">';
}

echo 'F: '.$fila.'<br>Nro: '.$asiento.'<br>'.$precio.'';
echo '</div></label></div>';

}
}    
Mureinik
  • 297,002
  • 52
  • 306
  • 350
pointup
  • 7
  • 6
  • From where are you getting the variables that you are passing to your query ? I think your query needs to somehow make use of `$valorAI` variable if you are wrapping it in a foreach loop. – Maximus2012 May 08 '15 at 16:48
  • You might also want to make sure that the control is flowing correctly through this if/else statement: `if($IdUbicacion == $valorAI){...} else{...}` (Try echo yes/no maybe ?) – Maximus2012 May 08 '15 at 16:50
  • So do you need them colored or `type="hidden"` hidden? – Alex May 08 '15 at 16:53
  • @Maximus2012 I'm getting the variables from a form in a previous page. Where is possible select the seats. echo '
    ';
    – pointup May 08 '15 at 16:55
  • @Alex I need different. Available:
    Unavailable:
    – pointup May 08 '15 at 16:59
  • Please don't dump code in comments. Edit your original post to add any new information. – Jay Blanchard May 08 '15 at 17:22
  • 1
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 08 '15 at 17:22
  • @JayBlanchard Ok! I'll take your advise! And I won't dump code in comments anymore. Thank you! – pointup May 08 '15 at 17:25

1 Answers1

0

If I got your logic correctly IMHO you don't need loop-wrapper.

You just need to check if value $IdUbicacion exists in your array $AsientosSeleccionadosI :

$resultButacas = mysql_query("SELECT U.Id AS IdUbicacion, U.IdModoTeatro, U.Numero, U.Fila, U.IdSector, U.CordX, U.CordY,
                            P.IdEspectaculo, P.IdFuncion, P.IdSector, P.Precio
                            FROM TeatroAsiento U, PrecioEspectaculoSector P 
                            WHERE U.IdSector = '$Sector' 
                            AND P.IdSector = '$Sector'
                            AND P.IdFuncion = '$fn'
                            AND P.IdEspectaculo = '$id'


                   ");

while ($rowButacas = mysql_fetch_array($resultButacas)) {
    $fila = $rowButacas["Fila"];
    $asiento = $rowButacas["Numero"];
    $IdUbicacion = $rowButacas["IdUbicacion"];
    $precio = $rowButacas["Precio"];

    echo '<div id="Casilla"><label for="asiento' . $fila . '' . $asiento . '">';

    if ( array_search($IdUbicacion,$AsientosSeleccionadosI)!= false) {
        echo '<div id="ubicacion" style="background-image: url(../css/images/asientosReservado.png);">'
        . '<input id="asiento' . $fila . '' . $asiento . '" value="' . $IdUbicacion . '"  name="asientoI[]" type="hidden">'
        . '<input id="' . $IdUbicacion . '' . $precio . '" value="' . $precio . '" type="hidden" name="precioseleccionI[]">';
    } else {
        echo '<div id="ubicacion" style="background-image: url(../css/images/asientosDisponible.png);">';
    }

    echo 'F: ' . $fila . '<br>Nro: ' . $asiento . '<br>' . $precio . '';
    echo '</div></label></div>';
}
Alex
  • 16,739
  • 1
  • 28
  • 51
  • Thanks very much! But it thows me the following error: Fatal error: Call to undefined function search_array() in /localhost/control/vta_proc4.php on line 85 – pointup May 08 '15 at 17:08
  • oops sorry my bad. fixed `array_search` :-) – Alex May 08 '15 at 17:10