简体   繁体   English

我如何php将此SQLDatabase数据回显到样式表中?

[英]How do I php echo this SQLDatabase data into a stylized table?

I'm trying to echo data from an SQLdatabase into a table that is somewhat decent-looking. 我正在尝试将SQL数据库中的数据回显到看起来不错的表中。 I can already echo the data properly into 5 separate basic tables, but when I can't figure out how to style it without it completely messing up. 我已经可以将数据正确地反射到5个单独的基本表中,但是当我想不出如何完全不弄乱样式的方式来设计它时。 Here is the code I already have: 这是我已经拥有的代码:

    // 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>";

If the code above is unclear- my goal is to compare the first period teachers in which are stored in the database, and output the students names that share the same period/teacher. 如果上面的代码不清楚,我的目标是比较存储在数据库中的第一期教师,并输出共享同一时期/教师的学生姓名。 This code works fine. 此代码可以正常工作。 But the tables look very bland. 但是桌子看起来很平淡。 I would like to echo the data into a table such as this. 我想将数据回显到这样的表中。

<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>

If anyone can offer any suggestions of any sort- I would greatly appreciate it. 如果有人可以提供任何建议,我将不胜感激。

I see a few areas that can be improved: 我看到一些可以改进的地方:

  • As others have said, don't use tables to manage layout. 正如其他人所说,不要使用表格来管理布局。 However, tables are perfectly acceptable for displaying tabular data 但是,表对于显示表格数据是完全可以接受的
  • You really want to use CSS to style your page, it's a lot easier to control your style and makes the markup easier to read. 您确实想使用CSS来设置页面样式,控制样式容易得多,并使标记更易于阅读。 See this w3schools' artcile on CSS Tables 请参阅CSS表上的w3schools文章
  • You can combine your multiple queries into one for better performance (though, that seems to be outside the scope of your question) 您可以将多个查询合并为一个,以提高性能(尽管这似乎不在您的问题范围内)

Here is an example markup and CSS for your table: http://jsfiddle.net/vxzJV/ 这是表的示例标记和CSS: http : //jsfiddle.net/vxzJV/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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