添加一组数据库中的列数组问题 [英] Issues adding a set of database columns to array

查看:171
本文介绍了添加一组数据库中的列数组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的表从我的数据库查询的用户,然后将每个用户下14名球员。我加了几列到同一个数据库表,我试图让列呼应出来,它抛出一个未定义的索引错误,但我不能完全肯定,我可以在这里中定义它。

这是我试图获得新的列在回声出一部分。我有14名球员,所以这个数组通过他们所有运行为每个用户。我加入了相同数量的'位置',所以这将是PLAYER1位置1,player2位置2,等等。

 为($ playerNum = 1; $ playerNum< = $ totalPlayerNumbers; $ playerNum ++){
    呼应'< TR>< TD>< D​​IV CLASS =draftBorder>' 。 $ playerNum。 '< / DIV>< / TD>';
    的foreach($ userPlayerStore为$ userPlayer){
        呼应'< TD>< D​​IV CLASS =draftBorder>' 。 $ userPlayer ['球员'。 ] $ playerNum。 '< / DIV>< / TD>';

我试图做到这一点,这就是为什么我得到的错误...

 的foreach($ userPlayerStore为$ userPlayer){
    呼应'< TD>< D​​IV CLASS =draftBorder>' 。 $ userPlayer ['球员'。 - 。 位置。 ] $ playerNum。 '< / DIV>< / TD>';

我还能如何格式化这个,这样我就可以去旁边的播放器显示的位置。像这样的:

PLAYER1 - POSITION1

全部code:

 < PHP $ userPlayerStore =阵列(); ?><表类=draft_border_table>
    &所述; TR>
        <第i的Rnd< /第i< PHP//输出用户名作为列标题
$ userResults = mysqli_query($ CON,SELECT * FROM user_players ORDER BY`id`');
而($ userPlayer = mysqli_fetch_array($ userResults)){
    $ userPlayerStore [] = $ userPlayer;
    呼应'百分位>< D​​IV>' 。 $ userPlayer [用户名。 '< / DIV>< /第i';
}?>    < / TR>< PHP//输出每个用户的每排1-14的球员
$ totalPlayerNumbers = 14;
为($ playerNum = 1; $ playerNum&下; = $ totalPlayerNumbers; $ playerNum ++){
呼应'< TR>< TD>< D​​IV CLASS =draftBorder>' 。 $ playerNum。 '< / DIV>< / TD>';
的foreach($ userPlayerStore为$ userPlayer){
     如果(空($ userPlayer ['玩家'。$ playerNum])){
       $ extra_class ='​​emptycell';
    }其他{
       $ extra_class ='​​filledcell';
    }
    呼应'< TD类=draft_table_td>' 。 $ userPlayer ['球员'。 ] $ playerNum。 '< / TD>';
}
呼应'< / TR>';
}?>< /表>


解决方案

在$ userPlayer var为每个记录在数据库中的表与此数组作为列名在每个指标的反映。在code以下应该可以帮助您:

  //输出每个用户的每排1-14的球员
$ totalPlayerNumbers = 14;
为($ playerNum = 1; $ playerNum&下; = $ totalPlayerNumbers; $ playerNum ++){    //开始播放器排
    呼应'< TR>< TD>< D​​IV CLASS =draftBorder>' 。 $ playerNum。 '< / DIV>< / TD>';    的foreach($ userPlayerStore为$ userPlayer){        //获得额外的类名?你打算用这个?
        $ extraClass =空($ userPlayer ['玩家'。$ playerNum])? emptycell':'filledcell';        //输出球员的名字和位置
        $ playerName = $ userPlayer ['球员'。 $ playerNum];
        $ playerPosition = $ userPlayer [位置。 $ playerNum];
        呼应'< TD类=draft_table_td>' 。 $ playerName。 - 。 $ playerPosition。 '< / TD>';
    }    // END玩家排
    呼应'< / TR>';
}

I have the following table that queries users from my database and then adds 14 players under each user. I added a couple of columns to the same database table and I am trying to get the columns to echo out and it is throwing an undefined index error, but I'm not entirely sure where I can define it in here.

This is the part I am trying to get the new columns to echo out at. I have 14 players, so this array runs through them all for each user. I am adding the same number of 'positions' So it will be player1 position1, player2 position2, etc.

for ($playerNum = 1; $playerNum <= $totalPlayerNumbers; $playerNum++) {
    echo '<tr><td><div class="draftBorder">' . $playerNum . '</div></td>';
    foreach ($userPlayerStore as $userPlayer) {
        echo '<td><div class="draftBorder">' . $userPlayer['player' . $playerNum] . '</div></td>';

I tried to do this and this is why I am getting the errors...

 foreach ($userPlayerStore as $userPlayer) {
    echo '<td><div class="draftBorder">' . $userPlayer['player' . ' - ' . 'position' . $playerNum] . '</div></td>';

How else can I format this, so that I can get the position to show up next to the player. Like this:

Player1 - Position1

Full code:

<?php $userPlayerStore = array(); ?>

<table class="draft_border_table">
    <tr>
        <th>Rnd</th>

<?php

// Output usernames as column headings
$userResults = mysqli_query($con, 'SELECT * FROM user_players ORDER BY `id`');
while($userPlayer = mysqli_fetch_array($userResults)) {
    $userPlayerStore[] = $userPlayer;
    echo '<th><div>' . $userPlayer['username'] . '</div></th>';
}

?>

    </tr>

<?php

// Output each user's player 1-14 in each row
$totalPlayerNumbers = 14;
for ($playerNum = 1; $playerNum <= $totalPlayerNumbers; $playerNum++) {
echo '<tr><td><div class="draftBorder">' . $playerNum . '</div></td>';
foreach ($userPlayerStore as $userPlayer) {
     if (empty($userPlayer['player'.$playerNum])){
       $extra_class = 'emptycell';
    }else{
       $extra_class = 'filledcell';
    }
    echo '<td class="draft_table_td">' . $userPlayer['player' . $playerNum] . '</td>';
}
echo '</tr>';
}

?>

</table>

解决方案

The $userPlayer var is a reflection of each record in your database table with each index in this array being the column name. The code below should help you:

// Output each user's player 1-14 in each row
$totalPlayerNumbers = 14;
for ($playerNum = 1; $playerNum <= $totalPlayerNumbers; $playerNum++) {

    // Start the player row
    echo '<tr><td><div class="draftBorder">' . $playerNum . '</div></td>';

    foreach ($userPlayerStore as $userPlayer) {

        // Retrieve extra class name? Did you mean to use this?
        $extraClass = empty($userPlayer['player' . $playerNum]) ? 'emptycell' : 'filledcell';

        // Output player name and position
        $playerName = $userPlayer['player' . $playerNum];
        $playerPosition = $userPlayer['position' . $playerNum];
        echo '<td class="draft_table_td">' . $playerName . ' - ' . $playerPosition . '</td>';
    }

    // End the player row
    echo '</tr>';
}

这篇关于添加一组数据库中的列数组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆