dsoihsnz85757 2015-07-28 09:06
浏览 39
已采纳

PHP echo表及其css

I really need your help. I am trying to echo a table from my database but it first of all comes with my table names from the database which I don't want and also the css is weird. I would like my table to have a different heading in each column then the data displayed below the corresponding column header in a uniform manna.

the php code is here

    <div id="content-1">
        <div>
            <?php 

                $table = 'std_details';
                // sending query for table
                $result = mysql_query("SELECT * FROM {$table}");

                if (!$result) {

                    die ("Query to show fields from table failed");

                }

                $fields_num = mysql_num_fields($result);

                echo "<h1>All Students</h1>";
                echo "<table border='2' class='gridtable'><tr>";

                // printing table headers
                for ($i=0; $i < $fields_num; $i++) { 

                    $field = mysql_fetch_field($result);
                    echo "<td>{$field->name}&nbsp;&nbsp;</td>";

                }

                echo "</tr>
";

                // printing table rows
                while ($row = mysql_fetch_row($result)) {

                    echo "<tr>";

                    // $row is array.... foreach( .. ) puts every element
                    // of $row to $cell variable
                    foreach ($row as $cell) {

                        echo "<td>&nbsp;\t$cell</td>";

                    }
                    echo "</tr>
";
                }

                mysql_free_result($result)
            ?>
        </div>
    </div>

And the table css is

    table.gridtable {
        font-family: verdana,arial,sans-serif;
        font-size:11px;
        color:#333333;
        border-width: 1px;
        border-color: #666666;
        border-collapse: collapse;
    }
    table.gridtable th {
        border-width: 1px;
        padding: 8px;
        border-style: solid;
        border-color: #666666;
        background-color: #dedede;
    }
    table.gridtable td {
        border-width: 1px;
        padding: 8px;
        border-style: solid;
        border-color: #666666;
        background-color: #ffffff;
    }

Thanks guys. I really need help!

enter image description here

  • 写回答

2条回答 默认 最新

  • dthh5038 2015-07-28 10:20
    关注

    I cant understand why you are using mysql instead of mysqli any way this may help you

      <!DOCTYPE html>
        <html>
        <head>
         <style>
          table.gridtable {
            font-family: verdana,arial,sans-serif;
            font-size:11px;
            color:#333333;
            border-width: 1px;
            border-color: #666666;
            border-collapse: collapse;
           }
          table.gridtable th {
             border-width: 1px;
             padding: 8px;
             border-style: solid;
             border-color: #666666;
             background-color: #dedede;
           }
         table.gridtable td {
             border-width: 1px;
             padding: 8px;
             border-style: solid;
             border-color: #666666;
             background-color: #ffffff;
           }
    
        </style>
      <!--- uncomment these line if you are loading css from external file --->
      <!--- <link href="Your css file" rel="stylesheet"> --->
      </head>
    <div id="content-1">
          <div>
               <?php 
    
                $table = 'std_details';
                // sending query for table
                $result = mysql_query("SELECT * FROM {$table}");
    
                    if (!$result) {
    
                      die ("Query to show fields from table failed");
    
                    }
    
                   $fields_num = mysql_num_fields($result);
    
                   echo "<h1>All Students</h1>";
                   echo "<table border='2' class='gridtable'><tr>";
    
                   // printing table headers
                   for ($i=0; $i < $fields_num; $i++) { 
    
                    $field = mysql_fetch_field($result);
                    echo "<td>{$field->name}</td>";
    
                }
    
                echo "</tr>";
    
                // printing table rows
                while ($row = mysql_fetch_row($result)) {
    
                    echo "<tr>";
    
                    // $row is array.... foreach( .. ) puts every element
                    // of $row to $cell variable
                    foreach ($row as $cell) {
    
                        echo "<td>&nbsp;\t$cell</td>";
    
                    }
                    echo "</tr>";
                }
                echo "</table>";
    
                mysql_free_result($result)
            ?>
        </div>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题