我如何将这个SQLDatabase数据回显到一个程式化的表格?

时间:2012-02-11 16:48:21

标签: php mysql database html-table echo

我正在尝试将SQL数据库中的数据回显到一个看起来有点像样的表中。我已经可以将数据正确地回显到5个独立的基本表中,但是当我无法弄清楚如何设置它而不会完全弄乱它。这是我已有的代码:

    // OUTPUTS RESULTS //

$resultfirst = mysql_query("SELECT * FROM Students WHERE FirstPeriod='$_POST[firstperiod]'");
$resultsecond = mysql_query("SELECT * FROM Students WHERE SecondPeriod='$_POST[secondperiod]'");
$resultthird = mysql_query("SELECT * FROM Students WHERE ThirdPeriod='$_POST[thirdperiod]'");
$resultfourth = mysql_query("SELECT * FROM Students WHERE FourthPeriod='$_POST[fourthperiod]'");
$resultfifth = mysql_query("SELECT * FROM Students WHERE FifthPeriod='$_POST[fifthperiod]'");


// 1st PERIOD

echo "<table border='1' bgcolor='#3b5998' style='float:left; margin:20'>
<tr>
<th>First Period</th>
</tr>";

while($row = mysql_fetch_array($resultfirst))
  {
  echo "<tr>";
  echo "<td>" . $row['StudentName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";



// 2nd PERIOD

echo "<table border='1' bgcolor='#3b5998' style='float:left; margin:20'>
<tr>
<th>Second Period</th>
</tr>";

while($row = mysql_fetch_array($resultsecond))
  {
  echo "<tr>";
  echo "<td>" . $row['StudentName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";



// 3rd PERIOD

echo "<table border='1' bgcolor='#3b5998' style='float:left; margin:20'>
<tr>
<th>Third Period</th>
</tr>";

while($row = mysql_fetch_array($resultthird))
  {
  echo "<tr>";
  echo "<td>" . $row['StudentName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";



// 4th PERIOD

echo "<table border='1' bgcolor='#3b5998' style='float:left; margin:20'>
<tr>
<th>Fourth Period</th>
</tr>";

while($row = mysql_fetch_array($resultfourth))
  {
  echo "<tr>";
  echo "<td>" . $row['StudentName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";




// 5th PERIOD

echo "<table border='1' bgcolor='#3b5998' style='float:left; margin:20'>
<tr>
<th>Fifth Period</th>
</tr>";

while($row = mysql_fetch_array($resultfifth))
  {
  echo "<tr>";
  echo "<td>" . $row['StudentName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

如果上面的代码不清楚 - 我的目标是比较存储在数据库中的第一期教师,并输出共享同一时期/教师的学生姓名。 这段代码工作正常。但桌子看起来很平淡。我想将数据回显到这样的表中。

<table width="368" border="0" cellspacing="0" cellpadding="2" align="center" height="100">
    <tr valign="middle"> 
      <td bgcolor="#000066" width="120" height="20"> 
        <div align="center"><font color="#FFFFFF"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Web 
          Services</font></b></font> </div>
      </td>
      <td width="4" height="20"></td>
      <td bgcolor="#990000" width="120" height="20"> 
        <div align="center"><font color="#FFFFFF"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Web 
          Products</font></b></font></div>
      </td>
      <td width="4" height="20"></td>
      <td bgcolor="#C89800" width="120" height="20"> 
        <div align="center"><font color="#FFFFFF"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Web 
          Resources</font></b></font> </div>
      </td>
    </tr>
    <tr valign="top"> 
      <td bgcolor="#000066" width="120"> 
        <table width="100%" border="0" cellspacing="0" cellpadding="3" height="80">
          <tr bgcolor="#FFFFFF" valign="top"> 
            <td> 
              <p><font face="Verdana, Arial, Helvetica, sans-serif" size="1">Professional 
                and cost effective web design, development and promotion services 
                </font></p>
            </td>
          </tr>
        </table>
      </td>
      <td width="4"></td>
      <td bgcolor="#990000" width="120"> 
        <table width="100%" border="0" cellspacing="0" cellpadding="3" height="80">
          <tr bgcolor="#FFFFFF" valign="top"> 
            <td> 
              <p><font face="Verdana, Arial, Helvetica, sans-serif" size="1">Interactive 
                Web Products - Flash Survey, poll, Guest book, Instant Quote

                </font></p>
            </td>
          </tr>
        </table>
      </td>
      <td width="4"></td>
      <td bgcolor="#C89800" width="120"> 
        <table width="100%" border="0" cellspacing="0" cellpadding="3" height="80">
          <tr bgcolor="#FFFFFF" valign="top"> 
            <td> 
              <p><font face="Verdana, Arial, Helvetica, sans-serif" size="1">Free 
                web resources - articles, tutorials, tips and tricks. 
               </font></p>
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </table>

如果有人能提供任何形式的任何建议 - 我将不胜感激。

1 个答案:

答案 0 :(得分:0)

我看到一些可以改进的方面:

  • 正如其他人所说,不要使用表来管理布局。但是,表格完全可以显示表格数据
  • 您真的想使用CSS来设置页面样式,控制样式更容易,并且标记更容易阅读。在CSS Tables
  • 上查看这个w3schools的artcile
  • 您可以将多个查询合并为一个以获得更好的效果(但这似乎超出了您的问题的范围)。

以下是您的表格的示例标记和CSS:http://jsfiddle.net/vxzJV/