将Mysql的输出格式化为表格&新线

时间:2016-08-17 04:48:28

标签: php html mysql

基本上我希望输出与thead匹配并对齐。我尝试了一些方法,只创建了更多错误。我对编码很陌生,我感谢所有那些以前来过的人并提出问题,因为答案让我走到了这一步。

示例:http://nightsins.net/nhlrealgm/test/ 输入任何有效的nhl播放器的姓氏,即Crosby

桌面下方的输出我无法弄清楚如何正确格式化并加入表头。第二个问题是,你输入“Cros”,它将在同一行返回2个结果。下班后会吗?下面是我使用的代码减去任何标记格式。谢谢,

<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
   <tr>
        <th>Player</th>
        <th>POS</th>
        <th>16-17 Fpts</th>
        <th>16-17 Fpts/G</th>
        <th>15-16 Fpts</th>
        <th>15-16 Fpts/G</th>
        <th>14-15 Fpts</th>
        <th>14-15 Fpts/G</th>
        <th>13-14 Fpts</th>
        <th>13-14 Fpts/G</th>
    </tr>
</thead>
</table>

<?php 
    $i=1;
    while($results = mysql_fetch_assoc($raw_results)){
?>
<tbody>
    <tr>
      <td><?php echo $results['Name']; ?></td>
      <td><?php echo $results['Pos']; ?></td>
      <td><?php echo $results['FPts16']; ?></td>
      <td><?php echo $results['FPG16']; ?></td>
      <td><?php echo $results['FPts15']; ?></td>
      <td><?php echo $results['FPG15']; ?></td>
      <td><?php echo $results['FPts14']; ?></td>
      <td><?php echo $results['FPG14']; ?></td>
      <td><?php echo $results['FPts13']; ?></td>
      <td><?php echo $results['FPG13']; ?></td>
    </tr>
<?php
$i++;
    }
?>
  </tbody>

<?php

    }
    else
    {
        echo "Player not found.";
    }

}
else
{ 
    echo "Minimum length is ".$min_length;
}
?>
</body>
</html>

3 个答案:

答案 0 :(得分:0)

Your just adding <tbody> in ur while loop. remove and close u r table after tbody   

<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
    <thead>
       <tr>
            <th>Player</th>
            <th>POS</th>
            <th>16-17 Fpts</th>
            <th>16-17 Fpts/G</th>
            <th>15-16 Fpts</th>
            <th>15-16 Fpts/G</th>
            <th>14-15 Fpts</th>
            <th>14-15 Fpts/G</th>
            <th>13-14 Fpts</th>
            <th>13-14 Fpts/G</th>
        </tr>
    </thead>
    <tbody>

    <?php 
        $i=1;
        while($results = mysql_fetch_assoc($raw_results))
{
    ?>

        <tr>
          <td><?php echo $results['Name']; ?></td>
          <td><?php echo $results['Pos']; ?></td>
          <td><?php echo $results['FPts16']; ?></td>
          <td><?php echo $results['FPG16']; ?></td>
          <td><?php echo $results['FPts15']; ?></td>
          <td><?php echo $results['FPG15']; ?></td>
          <td><?php echo $results['FPts14']; ?></td>
          <td><?php echo $results['FPG14']; ?></td>
          <td><?php echo $results['FPts13']; ?></td>
          <td><?php echo $results['FPG13']; ?></td>
        </tr>
    <?php
    $i++;
        }
    ?>
      </tbody>
</table>
    <?php

        }
        else
        {
            echo "Player not found.";
        }

    }
    else
    { 
        echo "Minimum length is ".$min_length;
    }
    ?>
    </body>
    </html>

答案 1 :(得分:0)

    Use the code as follows. You just need to put the tbody outside the while loop.



 <table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
    <thead>
       <tr>
            <th>Player</th>
            <th>POS</th>
            <th>16-17 Fpts</th>
            <th>16-17 Fpts/G</th>
            <th>15-16 Fpts</th>
            <th>15-16 Fpts/G</th>
            <th>14-15 Fpts</th>
            <th>14-15 Fpts/G</th>
            <th>13-14 Fpts</th>
            <th>13-14 Fpts/G</th>
        </tr>
</thead>
    <tbody>    
    <?php 
        $i=1;
        while($results = mysql_fetch_assoc($raw_results)){
    ?>
        <tr>
          <td><?php echo $results['Name']; ?></td>
          <td><?php echo $results['Pos']; ?></td>
          <td><?php echo $results['FPts16']; ?></td>
          <td><?php echo $results['FPG16']; ?></td>
          <td><?php echo $results['FPts15']; ?></td>
          <td><?php echo $results['FPG15']; ?></td>
          <td><?php echo $results['FPts14']; ?></td>
          <td><?php echo $results['FPG14']; ?></td>
          <td><?php echo $results['FPts13']; ?></td>
          <td><?php echo $results['FPG13']; ?></td>
        </tr>
    <?php
    $i++;
        }
    ?>
      </tbody>

    <?php

        }
        else
        {
            echo "<tr><td colspan=10>Player not found.</td></tr>";
        }

    }
    else
    { 
        echo "Minimum length is ".$min_length;
    }
    ?>
    </body>
    </html>

答案 2 :(得分:0)

好像你还没有提供完整的代码,因为括号没有正确地结束if语句..但是我跟随你的代码并基于它我发现你已经</table></thead>之后结束了不应该是这样。

相反,您应该在</tbody>之后关闭,而且您还没有向我们展示if语句的开始位置..我希望它在<table>标记之前开始。

所以我提出了以下代码,并希望这是有道理的。

<?php

if(2 == 2) // your main if starts which will check your length 
{
    if(1 == 1)
    { // this if statement is to find a player
?>
            <table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
            <thead>
               <tr>
                    <th>Player</th>
                    <th>POS</th>
                    <th>16-17 Fpts</th>
                    <th>16-17 Fpts/G</th>
                    <th>15-16 Fpts</th>
                    <th>15-16 Fpts/G</th>
                    <th>14-15 Fpts</th>
                    <th>14-15 Fpts/G</th>
                    <th>13-14 Fpts</th>
                    <th>13-14 Fpts/G</th>
                </tr>
            </thead>


            <?php
                $i=1;
                while($results = mysql_fetch_assoc($raw_results)){
            ?>
            <tbody>
                <tr>
                  <td><?php echo $results['Name']; ?></td>
                  <td><?php echo $results['Pos']; ?></td>
                  <td><?php echo $results['FPts16']; ?></td>
                  <td><?php echo $results['FPG16']; ?></td>
                  <td><?php echo $results['FPts15']; ?></td>
                  <td><?php echo $results['FPG15']; ?></td>
                  <td><?php echo $results['FPts14']; ?></td>
                  <td><?php echo $results['FPG14']; ?></td>
                  <td><?php echo $results['FPts13']; ?></td>
                  <td><?php echo $results['FPG13']; ?></td>


                </tr>
            <?php
            $i++;
                }
            ?>
              </tbody>
              </table>
<?php

    } // if statement to find a player ends 
    else
    {
        echo "Player not found.";
    }

} // if statement to find a length ends 
else
{
    echo "Minimum length is ".$min_length;
}
?>

</body>
</html>