简体   繁体   中英

For some reason my PHP code isn't working

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:

在此输入图像描述

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM