2012-08-23 40 views
0

我從不同的站點功能基於PHP的查詢以生成表:沒有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 noob,並不真正知道我要修改的地方/內容。任何幫助表示讚賞,歡呼!

+1

請不要使用'mysql_ *'函數編寫新代碼。他們不再維護,社區已經開始[棄用程序](http://goo.gl/KJveJ)。請參閱* [紅盒子](http://goo.gl/GPmFd)*?相反,您應該瞭解[準備好的語句](http://goo.gl/vn8zQ)並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli的)。如果你不能決定哪些,[這篇文章](http://goo.gl/3gqF9)會幫助你。如果你選擇PDO,[這裏是很好的教程](http://goo.gl/vFWnC)。 –

+0

@Truth已經說過了,請使用一些標準和經過良好測試的framworks /庫。它們可能已經被許多人在不同的條件和環境中進行過測試。 – FirmView

回答

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_*功能,這是相當老(thesedays)討厭。您可能需要考慮轉而使用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; 

} 
+0

對不起,沒有很好地解釋我自己,我確實需要標題行,只需要第一列標題爲空。 –

+0

@ChrisSpalton我不知道我明白,你想在標題行中顯示什麼? – Fluffeh

+0

只需要第一列('A'如果你喜歡)在標題行中沒有任何值,列B,C,D等,需要在那裏正常的標題。 –

相關問題