0

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に関しては完全に初心者で、どこを/何を修正しようとしているのか本当にわかりません。どんな助けでも大歓迎です、乾杯!

4

1 に答える 1

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;

}
于 2012-08-23T11:22:39.210 に答える