php生成表的第1列没有标题

时间:2012-08-23 11:21:11

标签: php html-table

我从不同的站点获得了一个函数来生成基于PHP查询的表:

function SQLResultTable($Query)
{
    $host = "localhost";
$user = "root";
$pass = "";
$db = "Quality_Monitoring";
    $link = mysql_connect($host, $user, $pass) or die('Could not connect: ' . mysql_error());      //build MySQL Link
    mysql_select_db($db) or die('Could not select database');        //select database
    $Table = "";  //initialize table variable

    $Table.= "<table id='Table1' border='1' style=\"border-collapse: collapse; text-align: center; font-size: 10px; cellspacing: 5px; \">"; //Open HTML Table

    $Result = mysql_query($Query); //Execute the query
    if(mysql_error())
    {
        $Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>";
    }
    else
    {
        //Header Row with Field Names
        $NumFields = mysql_num_fields($Result);
        $Table.= "<tr style=\"background-color: #000066; text-align: center; color: #FFFFFF;\">";
        for ($i=0; $i < $NumFields; $i++)
        {     
            $Table.= "<th>" . mysql_field_name($Result, $i) . "</th>"; 
        }
        $Table.= "</tr>";

        //Loop thru results
        $RowCt = 0; //Row Counter
        while($Row = mysql_fetch_assoc($Result))
        {
            //Alternate colors for rows
            if($RowCt++ % 2 == 0) $Style = "background-color: #CCCCCC;";
            else $Style = "background-color: #FFFFFF;";

            $Table.= "<tr style=\"$Style\">";
            //Loop thru each field
            foreach($Row as $field => $value)
            {
                $Table.= "<td>$value</td>";
            }
            $Table.= "</tr>";
        }
       // $Table.= "<tr style=\"background-color: #000066; color: #FFFFFF;\"><td colspan='$NumFields'>Query Returned " . mysql_num_rows($Result) . " records</td></tr>";
    }
    $Table.= "</table>";

    return $Table;

}

我需要做的是没有第一列的标题,但保留列,其中包含记录。任何人都可以帮我解决这个问题吗?对于PHP而言,总的菜鸟并不知道我想要修改的位置/内容。任何帮助赞赏,欢呼!

1 个答案:

答案 0 :(得分:0)

删除此部分代码:

    //Header Row with Field Names
    $NumFields = mysql_num_fields($Result);
    $Table.= "<tr style=\"background-color: #000066; text-align: center; color: #FFFFFF;\">";
    for ($i=0; $i < $NumFields; $i++)
    {     
        $Table.= "<th>" . mysql_field_name($Result, $i) . "</th>"; 
    }
    $Table.= "</tr>";

所以你的功能将是:

function SQLResultTable($Query)
{
    $host = "localhost";
$user = "root";
$pass = "";
$db = "Quality_Monitoring";
    $link = mysql_connect($host, $user, $pass) or die('Could not connect: ' . mysql_error());      //build MySQL Link
    mysql_select_db($db) or die('Could not select database');        //select database
    $Table = "";  //initialize table variable

    $Table.= "<table id='Table1' border='1' style=\"border-collapse: collapse; text-align: center; font-size: 10px; cellspacing: 5px; \">"; //Open HTML Table

    $Result = mysql_query($Query); //Execute the query
    if(mysql_error())
    {
    $Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>";
    }
    else
    {
    //Loop thru results
    $RowCt = 0; //Row Counter
    while($Row = mysql_fetch_assoc($Result))
    {
        //Alternate colors for rows
        if($RowCt++ % 2 == 0) $Style = "background-color: #CCCCCC;";
        else $Style = "background-color: #FFFFFF;";

        $Table.= "<tr style=\"$Style\">";
        //Loop thru each field
        foreach($Row as $field => $value)
        {
            $Table.= "<td>$value</td>";
        }
        $Table.= "</tr>";
    }
       // $Table.= "<tr style=\"background-color: #000066; color: #FFFFFF;\"><td colspan='$NumFields'>Query Returned " . mysql_num_rows($Result) . " records</td></tr>";
    }
    $Table.= "</table>";

    return $Table;

}

但是在那个注释中,这个函数使用了mysql_*函数,这些函数相当陈旧,并且(次要)令人讨厌。您可能需要考虑转而使用PDO

编辑:试试这个:

function SQLResultTable($Query)
{
    $host = "localhost";
$user = "root";
$pass = "";
$db = "Quality_Monitoring";
    $link = mysql_connect($host, $user, $pass) or die('Could not connect: ' . mysql_error());      //build MySQL Link
    mysql_select_db($db) or die('Could not select database');        //select database
    $Table = "";  //initialize table variable

    $Table.= "<table id='Table1' border='1' style=\"border-collapse: collapse; text-align: center; font-size: 10px; cellspacing: 5px; \">"; //Open HTML Table

    $Result = mysql_query($Query); //Execute the query
    if(mysql_error())
    {
        $Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>";
    }
    else
    {
        //Header Row with Field Names
        $NumFields = mysql_num_fields($Result);
        $Table.= "<tr style=\"background-color: #000066; text-align: center; color: #FFFFFF;\">";
        for ($i=0; $i < $NumFields; $i++)
        {     
            $Table.= "<th>" . mysql_field_name($Result, $i) . "</th>"; 
        }
        $Table.= "</tr>";

        //Loop thru results
        $RowCt = 0; //Row Counter
        while($Row = mysql_fetch_assoc($Result))
        {
            //Alternate colors for rows
            if($RowCt!=0)
        {
            if($RowCt % 2 == 0) $Style = "background-color: #CCCCCC;";
            else $Style = "background-color: #FFFFFF;";

            $Table.= "<tr style=\"$Style\">";
            //Loop thru each field
            foreach($Row as $field => $value)
            {
                $Table.= "<td>$value</td>";
            }
            $Table.= "</tr>";
        }
    $RowCt++;
        }
       // $Table.= "<tr style=\"background-color: #000066; color: #FFFFFF;\"><td colspan='$NumFields'>Query Returned " . mysql_num_rows($Result) . " records</td></tr>";
    }
    $Table.= "</table>";

    return $Table;

}