Py学习  »  DATABASE

MYSQL复制数组值

Louie Heaton • 5 年前 • 1740 次点击  
    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;

这是我的密码。当我尝试在//CREATE the matchip OF matchips之后运行sql查询时,它可以正常工作并执行我想要的操作,但它将同一行中的两行插入到数据库中。

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
)

我只想让每支球队打一次。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/52257
 
1740 次点击  
文章 [ 1 ]  |  最新文章 5 年前
lesliehuang93
Reply   •   1 楼
lesliehuang93    5 年前

我把我的网络项目从另一台笔记本电脑转移到另一台。安装了所需的所有jar文件。但是超级大乐透当我运行这个项目时,这个错误就出现了。

C:\Users\user\Desktop\Inventory\Inventory\nbproject\build-impl.xml:1013: Warning: Could not find file G:\Downloads 幸运时时彩 from Chrome\commons-fileupload-1.4-src.zip to copy. BUILD FAILED (total time: 0 seconds)十一运夺金

Nick
Reply   •   2 楼
Nick    5 年前

你的问题是最后一个 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 作为 $配对 .

下面是你的代码演示 INSERT 对每个 $配对 : Demo on 3v4l.org