[英]Is it possible to display the result of a mysql query without looping in php?
是否可以使用保存mysql查询结果并通过增加数组索引计数器显示它的数组?
我有这段代码,允许输入从6到11的数字,并生成2个表(如果可能)将数字均分,它会生成递增的团队名称(例如team 1,team2,...)。 我尝试使用循环显示数据库中的真实团队名称,但它还会循环生成表并创建许多表。请帮助。
<?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   <?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   <?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   <?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   <?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   <?php echo $team_names[$teamctr]; ?></td>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.