doujiaoang69440 2014-02-11 21:15
浏览 17
已采纳

由于某种原因,我的PHP代码不起作用

So my code is supposed to print out all of the users of my game in ASC (ascending) order by ID. It does this fine, but for some reason it won't print their names correctly...

Here is my code:

<table width="90%" border="1" style="border-collapse:collapse;border:4px solid #9f0606;">
  <tr height="41">
    <td align="center" colspan="2" style="border-collapse:collapse;border:4px solid #9f0606;"><strong>User List</strong></td>
  </tr>
<?
$presult = mysql_query("SELECT * FROM users ORDER BY id ASC") or die(mysql_error());
$b = 0;                    
while($row = mysql_fetch_array($presult)) {
?>
  <tr height="31">
    <td width="80%" style="border-collapse:collapse;border:4px solid #9f0606;" align="left">
      <?php
$b = $b + 1;
if($row['account_type'] == 1){
echo "<p><a style=\"color:black;text-decoration:none;\" href=\"view_profile.php?id=". $row['id'] ."\">".$row['username']."&nbsp;[".$row['id']."]</a></p>";
?>
     </td>
   <td width="20%" style="border-collapse:collapse;border:4px solid #9f0606;" align="left">
<?
echo "<p>Level:&nbsp;".$row['level']."</p>";
?>
  </td>
</tr>
<?
}elseif($row['account_type'] == 2){
?>
echo "<p><a style=\"color:#CC0000;text-decoration:none;\" href=\"view_profile.php?id=". $row['id'] ."\">".$row['username']."&nbsp;[".$row['id']."]</a></p>";
?>
  </td>
 <td width="20%" style="border-collapse:collapse;border:4px solid #9f0606;" align="left">
<?
echo "<p>Level:&nbsp;".$row['level']."</p>";
?>
</td>
 </tr>
<?
}else{
?>
echo "<p><a style=\"color:black;text-decoration:none;\" href=\"view_profile.php?id=". $row['id'] ."\">".$row['username']."&nbsp;[".$row['id']."]</a></p>";
?>
 </td>
<td width="20%" style="border-collapse:collapse;border:4px solid #9f0606;" align="left">
<?
echo "<p>Level:&nbsp;".$row['level']."</p>";
    ?>
    </td>
  </tr>
<?
}
}
?>
</table>

Sorry that it's not very clean or organized, having to space everything 4 times gets annoying so I let it look a bit sloppy.

Here's a screenshot of what the code looks like for me in-browser:

(I cant post images because I don't have 10 reputation yet apparently, but basically the screenshot shows the username/id part looking like this:

echo "

`this row in blue`".$row['username']." [".$row['id']."]

";?>

and the level part looking exactly how I want it, like this:

Level: 12)

As you can see, the "level" part of the loop (printing the user's level) works fine. If you can find the error in the rest of my code, that would be awesome. Oh and by the way, where it says ($row['account_type'] == 1), none of the users are account_type == 1, the first one is account_type == 6, and the second one is account_type == 2. They all print exactly the same. Thanks!

Image here:

enter image description here

  • 写回答

1条回答 默认 最新

  • dqnqpqv3841 2014-02-11 21:20
    关注

    you are closing the php ?> too soon in multiple places:

    <table width="90%" border="1" style="border-collapse:collapse;border:4px solid #9f0606;">
      <tr height="41">
        <td align="center" colspan="2" style="border-collapse:collapse;border:4px solid #9f0606;"><strong>User List</strong></td>
      </tr>
    <?
    $presult = mysql_query("SELECT * FROM users ORDER BY id ASC") or die(mysql_error());
    $b = 0;                    
    while($row = mysql_fetch_array($presult)) {
    ?>
      <tr height="31">
        <td width="80%" style="border-collapse:collapse;border:4px solid #9f0606;" align="left">
          <?php
    $b = $b + 1;
    if($row['account_type'] == 1){
    echo "<p><a style=\"color:black;text-decoration:none;\" href=\"view_profile.php?id=". $row['id'] ."\">".$row['username']."&nbsp;[".$row['id']."]</a></p>";
    ?>
         </td>
       <td width="20%" style="border-collapse:collapse;border:4px solid #9f0606;" align="left">
    <?
    echo "<p>Level:&nbsp;".$row['level']."</p>";
    ?>
      </td>
    </tr>
    <?
    }elseif($row['account_type'] == 2){
    ?>
    

    *** HERE FOR EXAMPLE *******

    echo "<p><a style=\"color:#CC0000;text-decoration:none;\" href=\"view_profile.php?id=". $row['id'] ."\">".$row['username']."&nbsp;[".$row['id']."]</a></p>";
    ?>
      </td>
     <td width="20%" style="border-collapse:collapse;border:4px solid #9f0606;" align="left">
    <?
    echo "<p>Level:&nbsp;".$row['level']."</p>";
    ?>
    </td>
     </tr>
    <?
    }else{
    ?>
    

    ****** AND HERE *******

    echo "<p><a style=\"color:black;text-decoration:none;\" href=\"view_profile.php?id=". $row['id'] ."\">".$row['username']."&nbsp;[".$row['id']."]</a></p>";
    ?>
     </td>
    <td width="20%" style="border-collapse:collapse;border:4px solid #9f0606;" align="left">
    <?
    echo "<p>Level:&nbsp;".$row['level']."</p>";
        ?>
        </td>
      </tr>
    <?
    }
    }
    ?>
    </table>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP