gpt4 book ai didi

php - 是否可以在php中不循环地显示mysql查询的结果?

转载 作者:行者123 更新时间:2023-11-30 01:21:35 27 4
gpt4 key购买 nike

是否可以使用一个数组来保存 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];
}
}

最佳答案

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

$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>

关于php - 是否可以在php中不循环地显示mysql查询的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18470214/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com