error_reporting(E_ALL);
echo "<pre>";
// DISTRIBUTE TEAMS INTO CONTESTS
$fixtures = mysqli_query($link, "SELECT teamname FROM tourn_teams WHERE groupname='Group 1'");
$teams = array();
// THE TEAMS
while($row = mysqli_fetch_assoc($fixtures))
{
$teams[] = $row['teamname'];
}
// HOW MANY WEEKS
$weeks = 3;
// MAKE ENOUGH ARRAY ELEMENTS FOR THE DISTRIBUTION
$array = array_merge($teams, $teams);
// POPULATE THE MATCHES ARRAY
$matches = array();
while ($weeks)
{
foreach ($teams as $ptr => $team)
{
// FIND THE INDEX INTO THE DISTRIBUTION ARRAY
$linkt = $ptr + $weeks;
// SELECT THE HOME AND AWAY TEAMS
$home = $team;
$away = $array[$linkt];
$matches[$team][$weeks] = array('home' => $home, 'away' => $away);
}
// NEXT WEEK
$weeks--;
}
// SORT THE MATCHES SENSIBLY SO WEEK ONE COMES FIRST
foreach ($matches as $team => $contests)
{
ksort($contests);
$matches[$team] = $contests;
}
// ACTIVATE THIS TO SEE WHAT THE $matches ARRAY LOOKS LIKE
// print_r($matches);
// CREATE THE TABLE OF MATCHUPS
$out = NULL;
$out .= "<table>";
$out .= PHP_EOL;
// CREATE THE HEADERS FOR EACH WEEK
$weeknums = end($matches);
$out .= "<tr>";
$out .= '<th> Team </th>';
$out .= '<th> v </th>';
$out .= "<th> Team </th>";
$out .= '</tr>';
$out .= PHP_EOL;
// CREATE THE MATRIX OF MATCHUPS
foreach ($matches as $team => $contests)
{
$out .= "<form class='form-horizontal' action='".$_SERVER['PHP_SELF']."'d method='post'><tr><td><input type='text' name='teamone' value='$team' readonly></td>";
$out .= "<td> <b>v</b></td>";
foreach ($contests as $week => $matchup)
{
print_r($matchup);
$out .= "<td> <input type='text' name='teamtwo' value='{$matchup["away"]}' readonly> </td>";
}
$out .= "</tr>";
$out .= PHP_EOL;
}
$out .= "<input class='btn btn-primary' type='submit' name='submit'></form></table>";
$out .= PHP_EOL;
foreach ($matches as $team => $contests)
{
foreach ($contests as $week => $matchup)
{
if(is_array($matchup)){
foreach($matchup as $key => $value){
$home = $matchup['home'];
$away = $matchup[away];
$sql = mysqli_query($link, "INSERT INTO tourn_fixtures(teamone, teamtwo) values ('$home', '$away')");
}
}
}
}
echo "</pre>";
echo $out;
这就是我的代码。当我尝试在//创建MATCHUPS矩阵之后运行sql查询时,它可以正常工作并执行我想要的操作,但是它将2行相同的行插入到数据库中。
Database
我一直在玩耍,找不到重复的内容。我只想要一排而不是两排相同。
我也注意到了,稍后它会生成两个相同的灯具...
Duplicate
这是我得到的数组输出;
Array
(
[home] => Committee All-Stars
[away] => Vets
)
Array
(
[home] => Committee All-Stars
[away] => Lightning
)
Array
(
[home] => Committee All-Stars
[away] => Bolt
)
Array
(
[home] => Vets
[away] => Lightning
)
Array
(
[home] => Vets
[away] => Bolt
)
Array
(
[home] => Vets
[away] => Firsts
)
Array
(
[home] => Lightning
[away] => Bolt
)
Array
(
[home] => Lightning
[away] => Firsts
)
Array
(
[home] => Lightning
[away] => Committee All-Stars
)
Array
(
[home] => Bolt
[away] => Firsts
)
Array
(
[home] => Bolt
[away] => Committee All-Stars
)
Array
(
[home] => Bolt
[away] => Vets
)
Array
(
[home] => Firsts
[away] => Committee All-Stars
)
Array
(
[home] => Firsts
[away] => Vets
)
Array
(
[home] => Firsts
[away] => Lightning
)
我只希望每个团队互相比赛一次。
最佳答案
您的问题出在最后一个foreach
上。 $matchup
是一个类似数组
Array
(
[home] => All-Stars
[away] => Vets
)
并且您正在迭代其值,因此您要为每个对局进行两次插入。您实际上不需要在那里的
foreach
,只需使用foreach ($contests as $week => $matchup) {
$home = $matchup['home'];
$away = $matchup['away'];
$sql = mysqli_query($link, "INSERT INTO tourn_fixtures(teamone, teamtwo) values ('$home', '$away')");
}
请注意,您还需要在
away
周围加上引号作为$matchup
的索引。这是您的代码演示,显示了每个
INSERT
的两个$matchup
查询的生成:Demo on 3v4l.org