0

是否可以使用一个数组来保存mysql查询的结果并通过增加数组索引计数器来显示它?

我有这段代码,它允许输入从 6 到 11 的数字,并生成 2 个表,如果可能的话,将数字平分。它会生成递增的团队名称(例如团队 1、团队 2、...)。我尝试使用循环来显示我的数据库中的真实团队名称,但它也会循环生成表并创建这么多表。请帮助。

<?php

        $count = count($result);
        $team = $count;
        $teamctr = 1;

        if ($team > 5 && $team <= 11){
            $table = 2;
            $tblctr = 1;
            $r1ctr = 0;
            $r2ctr = 0;
            if ($team == 6){
                $row1 = 3;
                $row2 = 3;
            }
            else if ($team == 7){
                $row1 = 3;
                $row2 = 4;
            }
            else if ($team == 8){
                $row1 = 4;
                $row2 = 4;
            }
            else if ($team == 9){
                $row1 = 4;
                $row2 = 5;
            }
            else if ($team == 10){
                $row1 = 5;
                $row2 = 5;
            }
            else if ($team == 11){
                $row1 = 5;
                $row2 = 6;
            }
            while($tblctr <= $table){$i = 0;
                echo "Number of Teams: ".$team; ?><br><?php
                echo "Group Into: ".$table; ?><br><?php

                ?>
                <table border="1" align="center" width="30%">
                    <tr>
                        <th width="80%" align="center">Group &nbsp <?php echo $tblctr; ?></th>
                        <th width="10%" align="center">W</th>
                        <th width="10%" align="center">L</th>
                    </tr>
                    <?php 
                    if($tblctr = 1){
                        while($r1ctr < $row1){
                        ?>
                        <tr>
                            <td>Team &nbsp <?php echo $teamctr ?></td>
                            <td><input type="text" name="score" maxlength="6" size="6" /></td>
                            <td><input type="text" name="score" maxlength="6" size="6" /></td>
                        <?php
                        $r1ctr++;
                        $teamctr++;
                    }
                    echo ""; ?><br><?php
                    }
                    if($tblctr = 2){
                        ?>
                <table border="1" align="center" width="30%">
                    <tr>
                        <th width="80%" align="center">Group &nbsp <?php echo $tblctr; ?></th>
                        <th width="10%" align="center">W</th>
                        <th width="10%" align="center">L</th>
                    </tr>
                    <?php
                        while($r2ctr < $row2){
                        ?>
                        <tr>
                            <td>Team &nbsp <?php echo $teamctr ?></td>
                            <td><input type="text" name="score" maxlength="6" size="6" /></td>
                            <td><input type="text" name="score" maxlength="6" size="6" /></td>
                        <?php
                        $r2ctr++;
                        $teamctr++;
                    }
                    echo ""; ?><br><?php
                    }
                    else{
                        echo "Done IF";
                    }

                    $tblctr++;
            }
        }

如何使用此查询显示数据库中的团队名称?

$query = "SELECT * From tbl_teams";
        $result = mysql_query($query);
        if($result) {

    $i = 0;

    while ($row = mysql_fetch_array($result)) {
        $id[$i] = $row['team_name'];   
        echo $id[$i];
    }
}
4

1 回答 1

0

在您的数据库查询循环中,尝试更改为

$team_names = array();
while ($row = mysql_fetch_array($result)) {
    $team_names[] = $row['team_name'];
}

然后在您的表格代码中,尝试将其更改为

$count = count($team_names);
$team = $count;
$teamctr = 0;

<td>Team &nbsp <?php echo $team_names[$teamctr]; ?></td>
于 2013-08-27T16:40:32.590 回答