代码放置在PHP中

时间:2017-07-10 17:09:12

标签: php

I'm trying to get $adminbutton and $admibutton2 on the same row

目前,我的表重复了我不希望它做的团队。我已将$ adminbutton2移动到表格中所需的colume

<table style ="background: #5b9bc6;
margin: auto;"
>
<tr>
<th>rank</th>
<th>team</th>
<th>win count</th>
<?php IF(isadmin())
{
    echo ("<th>Add wins</th>");
    echo ("<th>Revert wins</th>");
} ?>
</tr>
<?php
$sql="SELECT `id`, `teamname`, `wincount`,`AdminU` FROM `teamranks` ORDER BY 
`wincount` DESC";
if(!$result = $db->query($sql)){
    die('There was an error running the query [' . $db->error . ']');
}
$rank = 1;
while($row = $result->fetch_assoc()){
    $adminbutton ="";
    $teamid= $row['id'];
    if (isadmin()){
        $adminbutton="<button onclick=\"addwin($teamid)\" 
type=\"button\">Add win</button>";
    }
    echo '<tr><td>'.$rank."</td><td>". $row['teamname']."</td><td 
id=\"wincount-$teamid\">".$row ['wincount'] . "</td><td>$adminbutton</td>
<td></td></tr>";
    $rank++;
}
?>
<?php
$sql="SELECT `id`, `teamname`, `wincount` FROM `teamranks` ORDER BY 
 `wincount` DESC";
if(!$result = $db->query($sql)){
    die('There was an error running the query [' . $db->error . ']');
}
$rank = 1;
while($row = $result->fetch_assoc()){
    $adminbutton2 ="";
    $teamid= $row['id'];
    if (isadmin()){
        $adminbutton2="<button onclick=\"revertwin($teamid)\" 
type=\"button\">revert win</button>";
    }
    echo '<tr><td>'.$rank."</td><td>". $row['teamname']."</td><td 
id=\"wincount-$teamid\">".$row ['wincount'] . "</td><td></td>
<td>$adminbutton2</td><td></td>/tr>";
    $rank++;
}
?>

</table>

它基本上重复了球队的排名以及他们的胜利数量。所有代码都可以使用,但它不显示我想要的位置。我知道我可能在那里有很多不必要的代码,但是如果你有人可以帮我弄清楚要保留什么,放在正确的地方以及删除的内容会很棒!

1 个答案:

答案 0 :(得分:1)

while($row = $result->fetch_assoc()){
    $adminbutton= "";
    $adminbutton2= "";
    $teamid = $row['id'];
    if(isadmin()){
        $adminbutton = "<button onclick=\"addwin($teamid)\" type=\"button\">Add win</button>";
        $adminbutton2 = "<button onclick=\"revertwin($teamid)\" type=\"button\">Revert win</button>";
    }
    echo "<tr><td>".$rank."</td><td>".$row['teamname']."</td><td id=\"wincount-$teamid\">".$row['wincount'] . "<td><td>$adminbutton</td><td>$adminbutton2</td><td></td></tr>";
    $rank++;
}

与@Martin建议一样,您的代码存在一些问题,您需要阅读或练习更多内容。 Youtube,Udemy和其他网站上有一些很棒的教程。

根据我从屏幕截图中可以理解的内容,您看起来想要在每行$ adminbutton和$ adminbutton2中使用两个按钮。看起来您复制了查询并更改了按钮。

<强>解

  • 使用上面的代码
  • 替换代码行 77 - 84
  • 删除代码行 85 - 99