if ($puntos > (32) ) {
echo $puntos . '<br />';
$puntos--;$puntos--;$puntos--;$puntos--;$puntos--;
}
else if ($puntos >16)
{echo $puntos . '<br />';
$puntos--;$puntos--;
}
else if ($puntos > 0)
{echo $puntos . '<br />';
$puntos--;
}
else {
echo $puntos . '<br />';
}
Funciona, que es lo importante. Para restar X, no necesitas repetir $puntos-- X veces, sino que puedes hacer esto: $puntos -= X;.
Primero, en el formulario se pueden tomar los datos de los tres juegos a la vez o tiene que hacerse uno por vez. Yo he creado de prueba un formulario que permite meter varios puntajes y aunque digo que cada puntaje vaya a una tabla, todos llegan a la primera tabla asi que se pisan y solo se queda grabado el ultimo puntaje.
Vas muy rápido y te falta conocimiento :P. Eso se puede hacer enviando arreglos a PHP.
Lee esto primero, entiéndelo y luego intenta implementarlo.
Segunda, y ¿cuantas tablas tendria que crear para 3 juegos? ¿4 o 6? Si fueran 4, seria la tabla jugador y la las tablas puntos1, puntos2 y puntos3. Si fueran 6, jugador1, puntos1, jugador2, puntos2, jugador3, puntos 3.
Necesitarías una tabla de juegos relacionada de igual forma que las últimas dos, pero esto implicaría la modificación de las tablas anteriores (por eso es que se debe planificar la creación de una base de datos). Las tablas serían estas:
DROP TABLE IF EXISTS puntuacion; # Esto eliminará la tabla de puntuaciones y los datos en ella.
DROP TABLE IF EXISTS jugador;
CREATE TABLE jugador (
jugador_id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
nombre_jugador VARCHAR(3) NOT NULL
) ENGINE=InnoDB CHARSET=utf8;
CREATE TABLE jugador_juego (
jugador_id INT(11) UNSIGNED NOT NULL,
juego_id INT(11) UNSIGNED NOT NULL,
puntos INT(11) UNSIGNED NOT NULL
) ENGINE=InnoDB CHARSET=utf8;
CREATE TABLE juego (
juego_id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
nombre_juego VARCHAR(200) NOT NULL UNIQUE
) ENGINE=InnoDB CHARSET=utf8;
ALTER TABLE jugador_juego ADD CONSTRAINT pk_jugador_juego PRIMARY KEY (jugador_id, juego_id);
ALTER TABLE jugador_juego ADD CONSTRAINT fk_jugjue_ref_jugador FOREIGN KEY (jugador_id) REFERENCES jugador (jugador_id) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE jugador_juego ADD CONSTRAINT fk_jugjue_ref_juego FOREIGN KEY (juego_id) REFERENCES juego (juego_id) ON DELETE CASCADE ON UPDATE CASCADE;
Nuevamente, ejecuta todo en ese exacto orden. Ahora el código PHP (
el anterior ya no funciona):
index.php<html>
<body>
<?php
require_once( 'funciones.php' );
$con = mysql_connect( 'localhost', 'root', 'mpccnFER21#02#2005' );
mysql_select_db( 'test', $con );
?>
<form action="add_user.php" method="post">
<p><label>Nombre: </label><input type="text" name="nombre" /></p>
<p><label>Juego: </label><?php get_game_list( $con ); ?></p>
<p><label>Puntos: </label><input type="text" name="puntos" /></p>
<input type="submit" />
</form>
</body>
<?php
require_once( 'funciones.php' );
?>
<h4>Jugadores y sus Puntajes</h4>
<table>
<thead>
<tr>
<th>Jugador</th>
<th>Juego</th>
<th>Puntos</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM jugador LEFT JOIN jugador_juego USING (jugador_id) LEFT JOIN juego USING (juego_id) ORDER BY puntos DESC";
$result = mysql_query( $query, $con );
$rows = mysql_num_rows( $result );
if( $rows >= 1 ) :
while( $row = mysql_fetch_assoc( $result ) ) :
?>
<tr>
<td><?php echo $row['nombre_jugador']; ?></td>
<td><?php echo $row['nombre_juego']; ?></td>
<td><?php echo $row['puntos']; ?></td>
</tr>
<?php
endwhile;
else :
?>
<tr>
<td colspan="2">No hay jugadores registrados aún.</td>
</tr>
<?php
endif;
?>
</tbody>
</table>
</html>
add_game.php -> Para añadir los juegos.
<html>
<head>
<title>Añadir Juego</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p><label for="juego">Nombre del juego: </label><input type="text" name="juego" /></p>
<input type="submit" />
</form>
<?php
require_once( 'funciones.php' );
$con = mysql_connect( 'localhost', 'root', 'mpccnFER21#02#2005' );
mysql_select_db( 'test', $con );
if( isset( $_POST['juego'] ) && ! empty( $_POST['juego'] ) ) {
$return = add_game( $_POST['juego'], $con );
if( is_numeric( $return ) )
echo "<p>Juego Guardado.</p>";
else
echo mysql_error( $con );
}
?>
<div id="resultados">
<?php get_game_list( $con, false ); ?>
</div>
</body>
</html>
add_user.php -> Para añadir los usuarios, los puntos y el juego seleccionado.
<?php
if( isset( $_POST['nombre'] ) && ! empty( $_POST['nombre'] ) &&
isset( $_POST['puntos'] ) && ! empty( $_POST['puntos'] ) ) {
$con = mysql_connect( 'localhost', 'root', 'mpccnFER21#02#2005' );
mysql_select_db( 'test', $con );
require_once( 'funciones.php' );
$user = add_player( $_POST['nombre'], $con );
echo add_points_to_player( $user, $_POST['puntos'], $_POST['juego'], $con );
echo "<br />Errores: " . mysql_error( $con );
mysql_close( $con );
} else {
echo 'Necesito el nombre y el puntaje :P';
}
echo "<a href='index.php'>Volver</a>";
?>
funciones.php -> El archivo que hace todo XD.
<?php
// Este código debes ponerlo en un archivo e incluirlo en todos los que manipulen a los jugadores.
function add_player( $nombre, $db_con ) {
$id = get_player_id( $nombre, $db_con );
if( ! is_numeric( $id ) ) { // El usuario no existe
$nombre = filter_var( $nombre, FILTER_SANITIZE_STRING );
$insert = "INSERT INTO jugador SET nombre_jugador = '$nombre'";
mysql_query( $insert, $db_con );
if( mysql_affected_rows( $db_con ) == 1 )
return get_player_id( $nombre, $db_con );
} else {
return $id;
}
return null;
}
function add_points_to_player( $jugador_id, $puntos, $juego, $db_con ) {
if( ( is_numeric( $jugador_id ) && $jugador_id > 0 ) &&
( is_numeric( $juego ) && $juego > 0 ) ) {
$jugador_id = filter_var( $jugador_id, FILTER_SANITIZE_NUMBER_INT );
if( ! is_numeric( $puntos ) )
$puntos = 1;
else
$puntos = filter_var( $puntos, FILTER_SANITIZE_NUMBER_INT );
$juego = filter_var( $juego, FILTER_SANITIZE_NUMBER_INT );
$insert = "INSERT INTO jugador_juego SET puntos = $puntos, jugador_id = $jugador_id, juego_id = $juego ON DUPLICATE KEY UPDATE puntos = $puntos";
if( mysql_query( $insert, $db_con ) === TRUE )
return check_affected( $db_con );
else
return 'No pude guardar los puntos: ' . mysql_error( $db_con );
} else {
return 'Los parámetros "jugador_id" y "juego" deben ser números enteros mayores a 0';
}
}
// $jugador puede ser el id o el nombre del jugador:
function get_player_data( $jugador, $db_con ) {
$query = "SELECT * FROM jugador LEFT JOIN puntuacion USING(jugador_id) WHERE ";
if( is_numeric( $jugador ) ) {
$jugador = filter_var( $jugador, FILTER_SANITIZE_NUMBER_INT );
$query .= "jugador_id = $jugador";
} elseif( is_string( $jugador ) ) {
filter_var( $jugador, FILTER_SANITIZE_STRING );
$query .= "nombre_jugador = '$jugador'";
}
$result = mysql_query( $query, $db_con );
$rows = mysql_num_rows( $result );
if( $rows == 1 )
return mysql_fetch_array( $result, MYSQL_ASSOC );
else
return 'No hubieron resultados';
}
function get_player_id( $nombre_jugador, $db_con ) {
$nombre_jugador = filter_var( $nombre_jugador, FILTER_SANITIZE_STRING );
$query = "SELECT jugador_id FROM jugador WHERE nombre_jugador = '$nombre_jugador'";
$result = mysql_query( $query, $db_con );
$rows = mysql_num_rows( $result );
if( $rows == 1 ) {
$id = mysql_fetch_array( $result, MYSQL_ASSOC );
return (int) $id['jugador_id'];
} else {
return 'El jugador no existe';
}
}
function add_game( $nombre, $db_con ) {
$id = get_game_id( $nombre, $db_con );
if( ! is_numeric( $id ) ) {
$nombre = filter_var( $nombre, FILTER_SANITIZE_STRING );
$insert = "INSERT INTO juego SET nombre_juego = '$nombre'";
mysql_query( $insert, $db_con );
if( mysql_affected_rows( $db_con ) == 1 )
return get_game_id( $nombre, $db_con );
} else {
return $id;
}
return null;
}
function get_game_id( $nombre_juego, $db_con ) {
$nombre_juego = filter_var( $nombre_juego, FILTER_SANITIZE_STRING );
$query = "SELECT juego_id FROM juego WHERE nombre_juego = '$nombre_juego'";
$result = mysql_query( $query, $db_con );
$rows = mysql_num_rows( $result );
if( $rows == 1 ) {
$id = mysql_fetch_array( $result, MYSQL_ASSOC );
return (int) $id['juego_id'];
} else {
return 'El juego no existe';
}
}
function get_game_list( $db_con, $select = true ) {
$out = '';
if( $select )
$out = '<select name="juego">';
else
$out = '<ul>';
$query = "SELECT * FROM juego";
$result = mysql_query( $query, $db_con );
if( mysql_num_rows( $result ) > 0 ) {
while( $row = mysql_fetch_assoc( $result ) ) {
if( $select )
$out .= "<option value='$row[juego_id]'>$row[nombre_juego]</option>";
else
$out .= "<li>$row[nombre_juego]</li>";
}
if( $select )
$out .= '</select>';
else
$out .= '</ul>';
echo $out;
} else {
echo 'No hay juegos registrados aún';
}
}
function check_affected( $db_con ) {
$affected = mysql_affected_rows( $db_con );
if( $affected == 1 )
return 'Una fila afectada';
elseif( $affected > 1 )
return 'Se ingresaron más de una fila';
elseif( $affected == 0 )
return 'No se modificaron filas';
elseif( $affected < 0 )
return 'Ocurrió un error' . mysql_error( $db_con );
}
?>
A ver como te va. Yo lo probé y funciona :P.