2017-02-26 57 views
1

我正在使用php來查詢sql server數據庫並將結果寫入屏幕。我想用表&我所有的echo語句TR/TD正在創建一個表中顯示的結果,然而,當屏幕上顯示的數據也出現運行在一起,就像下面:PHP表未創建

Employee EmployeeIDPrice1Price2Price3Price4Price5 

等沒有表結構。我應該在這個語法中改變什麼,以便創建一個實際的表來顯示結果?

if ($result = mssql_execute($proc)) 
{ 
    $number_of_rows = mssql_num_rows($result); 
    if($number_of_rows > 0) { 
     echo '<table border="1">'; 
     echo '<tr>'; 
     echo ' <th bgcolor="#FFFFFF" >Employee </th>'; 
     echo ' <th bgcolor="#FFFFFF">EmployeeID </th>'; 
     echo ' <th bgcolor="#FFFFFF">Price1 </th>'; 
     echo ' <th bgcolor="#FFFFFF">Price2 </th>'; 
     echo ' <th bgcolor="#FFFFFF">Price3 </th>'; 
     echo ' <th bgcolor="#FFFFFF">Price4 </th>'; 
     echo ' <th bgcolor="#FFFFFF">Price5 </th>'; 
     echo ' <th bgcolor="#FFFFFF">Price6 </th>'; 
     echo ' <th bgcolor="#FFFFFF">Price7</th>'; 
     echo ' <th bgcolor="#FFFFFF">Price8</th>'; 
     echo ' <th bgcolor="#FFFFFF">Price9</th>'; 
     echo ' <th bgcolor="#FFFFFF">Price10</th>'; 
     echo ' <th bgcolor="#FFFFFF">Price11</th>'; 
     echo '</tr>'; 

    while ($Row = mssql_fetch_assoc($result)) 
    { 
     echo '<tr><td>'.$Row['Employee'] . 
     '</td><td>'.$Row['EmployeeID'] . 
     '</td><td>'."$".round($Row['Price1']) . 
     '</td><td>'."$".round($Row['Price2']) . 
     '</td><td>'."$".round($Row['Price3']) . 
     '</td><td>'."$".round($Row['Price4']) . 
     '</td><td>'."$".round($Row['Price5']) . 
     '</td><td>'."$".round($Row['Price6']) . 
     '</td><td>'."$".round($Row['Price7']) . 
     '</td><td>'."$".round($Row['Price8']) . 
     '</td><td>'."$".round($Row['Price9']) . 
     '</td><td>'."$".round($Row['Price10']) . 
     '</td><td>'."$".round($Row['Price11']) . 
     '</td></tr>'; 
    } 
     echo '</table>'; 
    } 

編輯
這是我的輸出樣子,甚至在CELLPADDING加入後=「5」 ......沒有一個明確定義的表像我使用HTML的話tabletdtr當預期 - 意味着所有的數據只是在一起流血,而不是像我想的那樣定義表格。

Picture

回答

-1

這是不正確的結構
必須是這樣的:

<table style="width:100%"> 
    <tr> 
    <th>Firstname</th> 
    <th>Lastname</th> 
    <th>Age</th> 
    </tr> 
    <tr> 
    <td>Jill</td> 
    <td>Smith</td> 
    <td>50</td> 
    </tr> 
    <tr> 
    <td>Eve</td> 
    <td>Jackson</td> 
    <td>94</td> 
    </tr> 
</table> 

https://www.w3schools.com/html/html_tables.asp

+0

從SQL Server甚至以爲我返回結果我不強迫使用echo語句? – BellHopByDayAmetuerCoderByNigh

+0

如果我不使用echo語句,當我嘗試訪問該頁面時,出現500錯誤。 – BellHopByDayAmetuerCoderByNigh

-1

echo '<table border="1" cellpadding="1" style="width: 100%;">';

增加的cellpadding的要求。

但我建議你閱讀css作爲未來的改進。

此外,該行有語法錯誤:'</td><td>."$".round($Row['Price6']) .

它應該是:'</td><td>'."$".round($Row['Price6']) . - 你解決了這個問題,因爲我貼!

甚至更​​好:'</td><td>$'.round($Row['Price6']) .

+0

只是爲了澄清,cellpadding =「1」會給你一個像素的細胞周圍的空間。 cellpadding =「5」會給你5個像素等。這個答案有幫助,或者你還沒有看到它顯示你想要的? – craigmayhew

+0

仍然不是我以後的輸出。它只是一起跑,就像書中的文字一樣。我在一個乾淨有序的表格之後,像下面提供的w3schools鏈接。 – BellHopByDayAmetuerCoderByNigh

+1

即使cellpadding =「5」?你能給你實際的html輸出嗎? – craigmayhew