PHP - Ayuda, Crear un calendario de futbol

 
Vista:
sin imagen de perfil

Ayuda, Crear un calendario de futbol

Publicado por marinho (1 intervención) el 18/05/2017 21:01:58
Hola, llevo dias sin avanzar. Con lo que llevo hasta el momento he conseguido que lea en un textarea todos los equipos que tengo en mi tabla "equipos" una vez leido le doy al boton "crear" del formulario y me genera un calendario aleatorio con todas las jornadas y los enfrentamiento de ida y vuelta.
Ahora solo me queda para terminar este archivo es insertar todos los enfrentamientos en mi tabla "partidos", espero conseguir ayuda porque por mi cuenta ya no doy a mas por muchas vueltas e intento que he realizado.

-- codigo --
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
	function main() {
?>
<h1 align="center"> Generador de jornadas </h1>
    <?php
	echo '<table width="30%" border="2" align="center" style="text-align:center;">';
    // Cuantos equipos habra
    if (! isset($_GET['teams']) && ! isset($_GET['names'])) {
        print get_form();
    } else {
        # comprobar cuantos equipos hay
        print show_fixtures(isset($_GET['teams']) ?  nums(intval($_GET['teams'])) : explode("\n", trim($_GET['names'])));
    }
	}
 
	function nums($n) {
    $ns = array();
    for ($i = 1; $i <= $n; $i++) {
        $ns[] = $i;
    }
    return $ns;
	}
 
function show_fixtures($names) {
    $teams = sizeof($names);
 
    print "<h3 align='center'> <b> Calendario de $teams equipos </b> </h3>";
 
    // Si es impar el numero de equipo añadir otro para ser pares
    $ghost = false;
    if ($teams & 2 == 1) {
        $teams++;
        $ghost = true;
    }
 
    // Generar calendario
    $totalRounds = $teams - 1;
    $matchesPerRound = $teams / 2;
    $rounds = array();
    for ($i = 0; $i < $totalRounds; $i++) {
        $rounds[$i] = array();
    }
 
    for ($round = 0; $round < $totalRounds; $round++) {
        for ($match = 0; $match < $matchesPerRound; $match++) {
            $home = ($round + $match) % ($teams - 1);
            $away = ($teams - 1 - $match + $round) % ($teams - 1);
            // El ultimo equipo permanece en su posicion y el resto de equipos rotaran para los enfrentamientos
            if ($match == 0) {
                $away = $teams - 1;
            }
            $rounds[$round][$match] = team_name($home + 1, $names) . " VS " . team_name($away + 1, $names);
        }
    }
	/*"INSERT INTO 'partidos' ('id_partidos', 'id_equipo1', 'resultado_equipo1', 'id_equipo2', 'resultado_equipo1', 'fecha')
	VALUES (' ',$rounds[$round], '0', $rounds[$match], '0', '0000-00-00')";*/
 
    //Intercalando los partidos de casa y fuera
    $interleaved = array();
    for ($i = 0; $i < $totalRounds; $i++) {
        $interleaved[$i] = array();
    }
 
    $evn = 0;
    $odd = ($teams / 2);
    for ($i = 0; $i < sizeof($rounds); $i++) {
        if ($i % 2 == 0) {
            $interleaved[$i] = $rounds[$evn++];
        } else {
            $interleaved[$i] = $rounds[$odd++];
        }
    }
 
    $rounds = $interleaved;
 
    // Si hay equipos impar hacer que se enfrenten
    for ($round = 0; $round < sizeof($rounds); $round++) {
        if ($round % 2 == 1) {
            $rounds[$round][0] = flip($rounds[$round][0]);
        }
    }
 
    // mostrar
	echo "<tr>";
		echo "<td>";
		print "<h2> <b> Primera vuelta </b> </h2>";
		echo "</td>";
	echo "</tr>";
 
	echo "<tr>";
		for ($i = 0; $i < sizeof($rounds); $i++) {
			echo "<td>";
			print "<h3> <b> Jornada " . ($i + 1) . "</b> </h3>\n";
			foreach ($rounds[$i] as $r) {
				print $r . "<br />";
			}
			print "<br />";
			echo "</td>";
	echo "</tr>";
    }
	//
	echo "<tr>";
		echo "<td>";
		print $r;
		echo "</td>";
	echo "</tr>";
	//
	echo "<tr>";
		echo "<td>";
		print "<h2> <b> Segunda vuelta </b> </h2>";
		echo "</td>";
	echo "</tr>";
 
	echo "<tr>";
		$round_counter = sizeof($rounds) + 1;
		for ($i = sizeof($rounds) - 1; $i >= 0; $i--) {
			echo "<td>";
			print "<h3> <b> Jornada " . $round_counter . "</b> </h3>\n";
			$round_counter += 1;
			foreach ($rounds[$i] as $r) {
				print flip($r) . "<br />";
			}
			print "<br />";
			echo "</td>";
	echo "</tr>";
    }
    print "<br />";
 
    if ($ghost) {
        print "Matches against team " . $teams . " are byes.";
    }
}
 
function flip($match) {
    $components = split(' v ', $match);
    return $components[1] . "  " . $components[0];
}
 
function team_name($num, $names) {
    $i = $num - 1;
    if (sizeof($names) > $i && strlen(trim($names[$i])) > 0) {
        return trim($names[$i]);
    } else {
        return $num;
    }
}
echo '</table>';
?>
<html>
<body>
    <form action= <?php $_SERVER['SCRIPT_NAME'] ?>>
        <label for="names">Nombre de los equipos que participan (un equipo por linea)</label>
        <?php
			$db=mysql_connect("localhost","root","") or die("Error en la conexión a MySql");
			$result=mysql_db_query("simulador","SELECT nombre_equipo FROM equipos",$db);
			$filas=mysql_fetch_assoc($result);
        ?>
        <textarea name="names" rows="8" cols="40" readonly><?php do{echo $filas['nombre_equipo'] . "\n";}while ($filas = mysql_fetch_assoc($result));?>
        </textarea>
        <input type="submit" value="Crear Calendario" />
    </form>
</body>
</html>
<?php
main();
 
?>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder