2017-03-15 76 views
0

我已經創建了一個由MySQL表自動填充的表,它只是稍微工作。它加載一個結果,但沒有更多。我需要它在mysql表中顯示所有結果。由MySQL填充的PHP/HTML表只顯示一個結果

下面是我的查詢+陣列

$future = mysql_query("SELECT * FROM Future"); 
    $future_row = mysql_fetch_array($future); 

這是我的表碼

<div class="tg-wrap"><table class="tg" style="undefined;table-layout: fixed; width: 1000px;> 

      <tr class="header"> 
       <th class="tg-ls31" colspan="2">Device</th> 
       <th class="tg-ls31" colspan="2">Booked By</th> 
       <th class="tg-ls31" colspan="2">Date Out</th> 
       <th class="tg-ls31" colspan="2">Date Back</th> 
      </tr> 
      <?php 
       { 
        echo "<tr>"; 
        echo "<td class='tg-baqh' colspan='2'>".$future_row[Device]."</td>"; 
        echo "<td class='tg-baqh' colspan='2'>".$future_row[Name]."</td>"; 
        echo "<td class='tg-baqh' colspan='2'>".$future_row[Out]."</td>"; 
        echo "<td class='tg-baqh' colspan='2'>".$future_row[In]."</td>"; 
        echo "</tr>"; 
       } 

      ?> 
     </table></div> 

如果你能提供一個修復,這樣我可以在這個表中顯示多個結果,我d最感激。

EDIT 1:

我已經更新了代碼,除去所述第一陣列和創建的while循環

<?php while($future_row = mysql_fetch_array($future)) 
       { 
        echo "<tr>"; 
        echo "<td class='tg-baqh' colspan='2'>".$future_row[Device]."</td>"; 
        echo "<td class='tg-baqh' colspan='2'>".$future_row[Name]."</td>"; 
        echo "<td class='tg-baqh' colspan='2'>".$future_row[Out]."</td>"; 
        echo "<td class='tg-baqh' colspan='2'>".$future_row[In]."</td>"; 
        echo "</tr>"; 
       } 

      ?> 

我曾經也爲表的圖像的請求,這是我提供了下面

Image of the MySQL DB Table

編輯2:

我被要求提供完整的頁面代碼,我將在下面提供。

<head> 
<?php 
$hostname = "localhost"; 
$username = "root"; 
$password = "gunner98"; 

//connection to the database 
$dbhandle = mysql_connect($hostname, $username, $password) 
    or die("Unable to connect to MySQL"); 

//select a database to work with 
$selected = mysql_select_db("laptop_pool",$dbhandle) 
    or die("Could not select database"); 

//execute the SQL query and return records 
$P1 = mysql_query("SELECT * FROM P1"); 
$P1_row = mysql_fetch_array($P1); 

$P2 = mysql_query("SELECT * FROM P2"); 
$P2_row = mysql_fetch_array($P2); 

$P3 = mysql_query("SELECT * FROM P3"); 
$P3_row = mysql_fetch_array($P3); 

$P4 = mysql_query("SELECT * FROM P4"); 
$P4_row = mysql_fetch_array($P4); 

$P5 = mysql_query("SELECT * FROM P5"); 
$P5_row = mysql_fetch_array($P5); 

$P6 = mysql_query("SELECT * FROM P6"); 
$P6_row = mysql_fetch_array($P6); 

$P7 = mysql_query("SELECT * FROM P7"); 
$P7_row = mysql_fetch_array($P7); 

$future = mysql_query("SELECT * FROM Future"); 
$future_row4 = mysql_fetch_array($future); 


//close the connection 
mysql_close($dbhandle); 
?> 

<title>&nbsp;&nbsp;Moat Laptop Pool Table&nbsp;&nbsp;</title> 

</head> 

<body style="background-height: 100%;background-width: 100%;background: #141E30;background: -webkit-linear-gradient(to left, #141E30 , #243B55);background: linear-gradient(to left, #141E30 , #243B55);"> 

<div id="top" style="position: fixed;top: 0;left: 0;z-index: 999;width: 100%;height: 25px;"> 

<div id="alert_box" style="height: 100%; width: 100%; color: white; background-color: rgba(0, 0, 0, 0.5);"> 

<div id="Site_Alert" style="font-weight: bold; display: inline;width: 5%;float: left;"> 
&nbsp;Site Alert: 
</div> 

<div id="scroll_alert" style="display: inline; float: right; width:95%;"> 
<marquee behavior=scroll direction="left" scrollamount="8" onmouseover="this.stop();" onmouseout="this.start();">This is in development!</marquee> 
</div> 

</div> 

<div id="logo" style="font-family: Tw Cen MT; font-weight: Bold; position: fixed; color: white; left: 650px;top: 35px; font-size: 80px;text-shadow: 3px 3px #c7c7c7;"> 
Moat Laptop Pool Table 
</div> 

<div id="content_box" style="position: fixed; left: 450px;top: 135px; width:60%; height: 70%; border-radius: 3px; 

"> 

<style type="text/css"> 
.tg {border-collapse:collapse;border-spacing:0;border-color:#ccc;margin:0px auto;} 
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#fff;} 
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#f0f0f0;} 
.tg .tg-baqh{text-align:center;vertical-align:top} 
.tg .tg-ls31{font-weight:bold;background-color:#333333;color:#f0f0f0;text-align:center;vertical-align:top} 
th.tg-sort-header::-moz-selection { background:transparent; }th.tg-sort-header::selection  { background:transparent; }th.tg-sort-header { cursor:pointer; }table th.tg-sort-header:after { content:''; float:right; margin-top:7px; border-width:0 4px 4px; border-style:solid; border-color:#404040 transparent; visibility:hidden; }table th.tg-sort-header:hover:after { visibility:visible; }table th.tg-sort-desc:after,table th.tg-sort-asc:after,table th.tg-sort-asc:hover:after { visibility:visible; opacity:0.4; }table th.tg-sort-desc:after { border-bottom:none; border-width:4px 4px 0; }@media screen and (max-width: 767px) {.tg {width: auto !important;}.tg col {width: auto !important;}.tg-wrap {overflow-x: auto;-webkit-overflow-scrolling: touch;margin: auto 0px;}}</style> 
<div class="tg-wrap"><table id="tg-4b5Id" class="tg" style="undefined;table-layout: fixed; width: 1000px; "> 
<colgroup> 
<col style="width: 29px"> 
<col style="width: 51px"> 
<col style="width: 35px"> 
<col style="width: 53px"> 
<col style="width: 28px"> 
<col style="width: 77px"> 
<col style="width: 58px"> 
<col style="width: 94px"> 
<col style="width: 44px"> 
<col style="width: 133px"> 
<col style="width: 37px"> 
<col style="width: 91px"> 
</colgroup> 
    <tr> 
    <th class="tg-ls31" colspan="2">Device</th> 
    <th class="tg-ls31" colspan="2">Out or In</th> 
    <th class="tg-ls31" colspan="2">Out on</th> 
    <th class="tg-ls31" colspan="2">In Posession Of</th> 
    <th class="tg-ls31" colspan="2">Department</th> 
    <th class="tg-ls31" colspan="2">Due Back</th> 
    </tr> 
    <tr> 
    <td class="tg-baqh" colspan="2">P1</td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P1_row['status']; 
?> </td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P1_row['left']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P1_row['posession']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P1_row['department']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P1_row['returns']; 
?></td> 
    </tr> 
    <tr> 
    <td class="tg-baqh" colspan="2">P2</td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P2_row['status']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P2_row['left']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P2_row['posession']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P2_row['department']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P2_row['returns']; 
?></td> 
    </tr> 
    <tr> 
    <td class="tg-baqh" colspan="2">P3</td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P3_row['status']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P3_row['left']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P3_row['posession']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P3_row['department']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P3_row['returns']; 
?></td> 
    </tr> 
    <tr> 
    <td class="tg-baqh" colspan="2">P4</td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P4_row['status']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P4_row['left']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P4_row['posession']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P4_row['department']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P4_row['returns']; 
?></td> 
    </tr> 
    <tr> 
    <td class="tg-baqh" colspan="2">P5</td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P5_row['status']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P5_row['left']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P5_row['posession']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P5_row['department']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P5_row['returns']; 
?></td> 
    </tr> 
    <tr> 
    <td class="tg-baqh" colspan="2">P6</td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P6_row['status']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P6_row['left']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P6_row['posession']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P6_row['department']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P6_row['returns']; 
?></td> 
    </tr> 
    <tr> 
    <td class="tg-baqh" colspan="2">P7</td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P7_row['status']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P7_row['left']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P7_row['posession']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P7_row['department']; 
?></td> 
    <td class="tg-baqh" colspan="2"><?php 
echo $P7_row['returns']; 
?></td> 
    </tr> 
</table></div> 
<div id="future" style="font-family: Tw Cen MT; font-weight: Bold; position: fixed; color: white; left: 750px; font-size: 80px;text-shadow: 3px 3px #c7c7c7;"> 
Future Bookings 
</div> 
<br /> 
<br /> 
<br /> 
<br /> 
<br /> 
<div class="tg-wrap"><table class="tg" style="undefined;table-layout: fixed; width: 1000px;> 

      <tr class="header"> 
       <th class="tg-ls31" colspan="2">Device</th> 
       <th class="tg-ls31" colspan="2">Booked By</th> 
       <th class="tg-ls31" colspan="2">Date Out</th> 
       <th class="tg-ls31" colspan="2">Date Back</th> 
      </tr> 
      <?php while($future_row = mysql_fetch_array($future)) 
       { 
        echo "<tr>"; 
        echo "<td class='tg-baqh' colspan='2'>".$future_row[Device]."</td>"; 
        echo "<td class='tg-baqh' colspan='2'>".$future_row[Name]."</td>"; 
        echo "<td class='tg-baqh' colspan='2'>".$future_row[Out]."</td>"; 
        echo "<td class='tg-baqh' colspan='2'>".$future_row[In]."</td>"; 
        echo "</tr>"; 
       } 

      ?> 
     </table></div> 
<script type="text/javascript" charset="utf-8">var TgTableSort=window.TgTableSort||function(n,t){"use strict";function r(n,t){for(var e=[],o=n.childNodes,i=0;i<o.length;++i){var u=o[i];if("."==t.substring(0,1)){var a=t.substring(1);f(u,a)&&e.push(u)}else u.nodeName.toLowerCase()==t&&e.push(u);var c=r(u,t);e=e.concat(c)}return e}function e(n,t){var e=[],o=r(n,"tr");return o.forEach(function(n){var o=r(n,"td");t>=0&&t<o.length&&e.push(o[t])}),e}function o(n){return n.textContent||n.innerText||""}function i(n){return n.innerHTML||""}function u(n,t){var r=e(n,t);return r.map(o)}function a(n,t){var r=e(n,t);return r.map(i)}function c(n){var t=n.className||"";return t.match(/\S+/g)||[]}function f(n,t){return-1!=c(n).indexOf(t)}function s(n,t){f(n,t)||(n.className+=" "+t)}function d(n,t){if(f(n,t)){var r=c(n),e=r.indexOf(t);r.splice(e,1),n.className=r.join(" ")}}function v(n){d(n,L),d(n,E)}function l(n,t,e){r(n,"."+E).map(v),r(n,"."+L).map(v),e==T?s(t,E):s(t,L)}function g(n){return function(t,r){var e=n*t.str.localeCompare(r.str);return 0==e&&(e=t.index-r.index),e}}function h(n){return function(t,r){var e=+t.str,o=+r.str;return e==o?t.index-r.index:n*(e-o)}}function m(n,t,r){var e=u(n,t),o=e.map(function(n,t){return{str:n,index:t}}),i=e&&-1==e.map(isNaN).indexOf(!0),a=i?h(r):g(r);return o.sort(a),o.map(function(n){return n.index})}function p(n,t,r,o){for(var i=f(o,E)?N:T,u=m(n,r,i),c=0;t>c;++c){var s=e(n,c),d=a(n,c);s.forEach(function(n,t){n.innerHTML=d[u[t]]})}l(n,o,i)}function x(n,t){var r=t.length;t.forEach(function(t,e){t.addEventListener("click",function(){p(n,r,e,t)}),s(t,"tg-sort-header")})}var T=1,N=-1,E="tg-sort-asc",L="tg-sort-desc";return function(t){var e=n.getElementById(t),o=r(e,"tr"),i=o.length>0?r(o[0],"td"):[];0==i.length&&(i=r(o[0],"th"));for(var u=1;u<o.length;++u){var a=r(o[u],"td");if(a.length!=i.length)return}x(e,i)}}(document);document.addEventListener("DOMContentLoaded",function(n){TgTableSort("tg-4b5Id")});</script> 
</div> 




</body> 

不是目前最乾淨的代碼,但我想在清理它之前得到它的功能。

+0

'$ future_row4'!='$ future_row'。你複製/粘貼? –

+0

@JayBlanchard不,因爲我在其他地方引用它,我添加了4以確保它沒有衝突,它是創建該循環。但是,我剛剛添加了另一個條目,顯示出來了。表面上看,第一排沒有被拿到桌子上。所有其他的行,爲了解決這個問題,我剛剛添加了一個空白行,而且現在似乎都在工作。 –

+0

我不明白,因爲您沒有使用'$ future_row4'來輔助表格,並且我沒有在代碼中的其他任何地方看到它。你回去引用標識符嗎? –

回答

2

首先,一些警告:

stop using mysql_* functionsThese extensions已在PHP 7中刪除。瞭解有關PDOMySQLiprepared對帳單,並考慮使用PDO,it's really pretty easy


你通過你的結果必須循環來填充表:

$future = mysql_query("SELECT * FROM Future"); 

現在,在表中,循環(確保引用數組標識符):

<div class="tg-wrap"><table class="tg" style="undefined;table-layout: fixed; width: 1000px;> 

      <tr class="header"> 
       <th class="tg-ls31" colspan="2">Device</th> 
       <th class="tg-ls31" colspan="2">Booked By</th> 
       <th class="tg-ls31" colspan="2">Date Out</th> 
       <th class="tg-ls31" colspan="2">Date Back</th> 
      </tr> 
      <?php 
       while($future_row = mysql_fetch_array($future)) { 
        echo "<tr>"; 
        echo "<td class='tg-baqh' colspan='2'>".$future_row['Device']."</td>"; 
        echo "<td class='tg-baqh' colspan='2'>".$future_row['Name']."</td>"; 
        echo "<td class='tg-baqh' colspan='2'>".$future_row['Out']."</td>"; 
        echo "<td class='tg-baqh' colspan='2'>".$future_row['In']."</td>"; 
        echo "</tr>"; 
       } 

      ?> 
     </table></div> 

編輯 -

現在,我可以看到修改後的完整代碼,我看到你在這裏分配數據的第一行:

$future_row4 = mysql_fetch_array($future); 

所以,當你這樣做以後while循環:

while($future_row = mysql_fetch_array($future)) {... 

你只會得到表中剩餘的行。刪除(或註釋掉)第一個賦值,你的表將包含所有的行。

+0

嗨,謝謝你的回答。我的服務器當前運行PHP 5而不是7,所以它們仍然應該有效地運行。我已經包含了這個循環,它只填充一個字段,我需要它包含mysql表中的所有數據,而不僅僅是一行。 –

+0

你可以將你的MySQL表格配置添加到原始文章中嗎? –

+0

也可以包括你的更新代碼在問題 –

0

使用while循環。

<?php 
     while($future_row = mysql_fetch_array($future)) 
       { 
+0

我已經包括了while循環,但它沒有解決這個問題。 –