gpt4 book ai didi

php - 如果条件存在则打印数组

转载 作者:行者123 更新时间:2023-11-30 06:45:19 24 4
gpt4 key购买 nike

我正在通过 php 打印棒球队阵容。我想为缺失的 Player 6(或任何缺失的位置)打印一个占位符

因此,如果玩家 1 -> 玩家 5 可以打印,则没有玩家 #6 打印占位符,玩家 7 -> 玩家 9 可以打印。我试图简化代码。我尝试过以各种方式解决这个问题,但我一直卡住了。

CODE:

$rot = array();
$pos = array();
$jn = array();

$x = 1;
// loads up the arrays from the db
while ( $rot[$x], $pos[$x], $jn[$x])= $r->fetch_row() ) {
$x++;
}

// counts the actual number of players in linuep
// used for validation and error display
$num_players = mysqli_num_rows($r);

// controls the lineup position
for ($i = 1; $i <= 15; $i++){
if($rot[$i] == $i) {
//prints player
$lineup .= "<div data-fp='" . $pos[$i] . "'>" .$jn[$i]. "</div>";
} else {
// prints place holder
$text = "This Position needs to be filled before the next game.";
$lineup .= "<div id='pid' data-rel='".$text."' data-fp='' data-pid='' data-jn='' title=''>x</div>";
}
}

我也试过这个来遍历数组 rot[] 以找到匹配的位置并打印该行,但它实际上重复打印了 holder。

// controls the lineup position
for ($x = 1; $x <= 15; $x++){

for ($i = 1; $i <= ($num_players+1); $i++) {
if ($x == $i) {
//prints player
$lineup .= "<div data-fp='" . $pos[$i] . "'>" .$jn[$i]. "</div>";
} else {
// prints place holder
$text = "This Position needs to be filled before the next game.";
$lineup .= "<div id='pid' data-rel='".$text."' data-fp='' data-pid='' data-jn='' title=''>x</div>";
}
}
}

最佳答案

关于:

# index all players by position while taking them from the database

$players = array();
while ( $row = $r->fetch_row() ) {
list($rot, $pos, $jn) = $row;
$players[$pos] = compact(array('rot', $pos, $jn);
}

...

# line-up players
for ($pos = 1; $pos <= 15; $pos++)
{
$playerExists = isset($players[$pos]);

if ($playerExists)
{
# do this ...
}
else
{
# do that ...
}
}

关于php - 如果条件存在则打印数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7449287/

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