2016-05-29 127 views
2

我如何在PHP中的数据表中显示选择语句结果。我已经使用下面的代码来显示来自Mysql表的记录但不工作。我怎样才能使用Datatables为Php While循环

我都试过,但没有运气

Select语句

$sql = "SELECT demand.itemid,demand.qty, MIITEM.descr,MIITEM.descr,supplier.suplId,supplier.suplProdCode,supplier.itemId,MIILOC.qStk,MIILOC.qWIP,MIILOC.qRes, MIILOC.qOrd 
FROM MIITEM 
LEFT JOIN demand 
ON MIITEM.itemId=demand.itemId 
LEFT OUTER JOIN supplier 
ON MIITEM.itemId = supplier.itemId 
LEFT OUTER JOIN MIILOC 
ON MIITEM.itemId = MIILOC.itemId 
WHERE MIITEM.itemId=demand.itemId AND supplier.itemId=demand.itemId"; 

$result = $conn->query($sql); 

显示记录

echo"<table id='example' class='display' cellspacing='0' width='100%'> 
    <thead> 
     <tr style='background:#ccc;'> 
<th STYLE='WIDTH:50px; padding:7px'>ID</th> 
<th STYLE='WIDTH:250px; padding:7px'>Description</th> 

<th STYLE='WIDTH:100px; padding:7px'>Supplier#</th> 
<th STYLE='WIDTH:200px; padding:7px'>Supplier </th> 
<th STYLE='WIDTH:100px; padding:7px'>ON WO</th> 
<th STYLE='WIDTH:100px; padding:7px'>Stock</th> 
<th STYLE='WIDTH:100px; padding:7px'>WIP</th> 
<th STYLE='WIDTH:100px; padding:7px'>Reserve</th> 
<th STYLE='WIDTH:100px; padding:7px'>On Order</th> 

</tr> </thead></table>"; 
if ($result->num_rows > 0) { 

    while($row = $result->fetch_assoc()) { 
     echo"<table id='example' class='display' cellspacing='0' width='100%'><tbody> 
<tr> 
<th STYLE='WIDTH:50px; padding:7px'>"; echo$row["itemid"];echo"</th> 
<th STYLE='WIDTH:250px; padding:7px'>"; echo$row["descr"];echo"</th> 
<th STYLE='WIDTH:100px; padding:7px'>"; echo$row["suplId"];echo"</th> 
<th STYLE='WIDTH:200px; padding:7px'>"; echo$row["suplProdCode"];echo" </th>"; 
echo"<th STYLE='WIDTH:100px; padding:7px'>";echo$row["qty"];echo"</th>"; 
echo"<th STYLE='WIDTH:100px; padding:7px'>";echo$row["qStk"];echo"</th>"; 
echo"<th STYLE='WIDTH:100px; padding:7px'>";echo$row["qWIP"];echo"</th>"; 

echo"<th STYLE='WIDTH:100px; padding:7px'>";echo$row["qRes"];echo"</th>"; 

echo"<th STYLE='WIDTH:100px; padding:7px'>";echo$row["qOrd"];echo"</th>"; 


echo"</tr></tbody></table>"; 

这里是Javascript代码,并从​​CSS文件,但没有发生变化。

<script type="text/javascript" src="//code.jquery.com/jquery-1.12.3.min.js"></script> 
<script type="text/javascript" src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script> 
<script type="text/javascript" src="https://cdn.datatables.net/select/1.2.0/js/dataTables.select.min.js"></script> 


    <script type="text/javascript"> 
$(document).ready(function() { 
    $('#example').DataTable({ 
     select: true 
    }); 
}); 

    </script> 
+0

您的查询在哪里?在php myadmin中执行查询吗? – paranoid

+0

@paranoid我在我的问题开始处添加了查询 – Kin

+0

将这段代码复制到php myadmin中,你有结果吗? – paranoid

回答

0

请试试这个代码

<?php 
//you dont need where condition Left join already filter data over all of your tables demand,supplier, MIILOC 
$sql = "SELECT demand.itemid,demand.qty, MIITEM.descr,MIITEM.descr,supplier.suplId,supplier.suplProdCode,supplier.itemId,MIILOC.qStk,MIILOC.qWIP,MIILOC.qRes, MIILOC.qOrd 
FROM MIITEM 
LEFT JOIN demand 
ON MIITEM.itemId=demand.itemId 
LEFT OUTER JOIN supplier 
ON MIITEM.itemId = supplier.itemId 
LEFT OUTER JOIN MIILOC 
ON MIITEM.itemId = MIILOC.itemId"; 

$result = $conn->query($sql); 
if (!$result) { 
printf("Errormessage: %s\n", $mysqli->error); 
die; 
} 

$table = "<table id='example' class='display' cellspacing='0' width='100%'> 
<thead> 
    <tr style='background:#ccc;'> 
    <th STYLE='WIDTH:50px; padding:7px'>ID</th> 
    <th STYLE='WIDTH:250px; padding:7px'>Description</th> 

    <th STYLE='WIDTH:100px; padding:7px'>Supplier#</th> 
    <th STYLE='WIDTH:200px; padding:7px'>Supplier </th> 
    <th STYLE='WIDTH:100px; padding:7px'>ON WO</th> 
    <th STYLE='WIDTH:100px; padding:7px'>Stock</th> 
    <th STYLE='WIDTH:100px; padding:7px'>WIP</th> 
    <th STYLE='WIDTH:100px; padding:7px'>Reserve</th> 
    <th STYLE='WIDTH:100px; padding:7px'>On Order</th> 
</tr> </thead>"; 

if ($result->num_rows > 0) { 
    $table .= "<tbody>"; 
    while($row = $result->fetch_assoc()) { 
     $table .= " 
     <tr> 
     <th STYLE='WIDTH:50px; padding:7px'>{$row["itemid"]}</th> 
     <th STYLE='WIDTH:250px; padding:7px'>{$row["descr"]}</th> 
     <th STYLE='WIDTH:100px; padding:7px'>{$row["suplId"]}</th> 
     <th STYLE='WIDTH:200px; padding:7px'>{$row["suplProdCode"]}</th> 
     <th STYLE='WIDTH:100px; padding:7px'>{$row["qty"]}</th> 
     <th STYLE='WIDTH:100px; padding:7px'>{$row["qStk"]}</th> 
     <th STYLE='WIDTH:100px; padding:7px'>{$row["qWIP"]}</th> 
     <th STYLE='WIDTH:100px; padding:7px'>{$row["qRes"]}</th> 
     <th STYLE='WIDTH:100px; padding:7px'>{$row["qOrd"]}</th> 
    </tr>"; 
} 
$table .= "</tbody>" 
} 
$table .= "</table>"; 

echo $table; 

?> 
<script type="text/javascript" src="//code.jquery.com/jquery-1.12.3.min.js"></script> 
<script type="text/javascript" src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script> 
<script type="text/javascript" src="https://cdn.datatables.net/select/1.2.0/js/dataTables.select.min.js"></script> 


<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#example').DataTable({ 
      select: true 
     }); 
    }); 

</script> 
+0

我也回答了;-)虽然我把整个表打印在'if'中,并且把主体中的'th'改成了'td'。不错的做法,加入一个字符串变量,然后在最后做一个'echo'。 – annoyingmouse

+0

谢谢,它现在是一种习惯。 – chudasamachirag

1

您的标记将是很奇怪,就好像你在插入重复表,用相同的ID,每个$row。根本没有任何东西进入用户界面?使用在线HTML有效性检查器检查页面的来源以及标记的有效性总是值得的。从我了解你的需求我已经创建this snippet

if ($result->num_rows > 0) { 
    echo " 
     <style> 
      th, td { 
       width: 100px; 
       padding: 7px; 
      } 
      .fifty { 
       width: 50px; 
      } 
      .twohundred { 
       width: 200px; 
      } 
      .twohundredfifty { 
       width: 250px; 
      } 
     </style> 
     <table id='example' class='display' cellspacing='0' width='100%'> 
      <thead> 
       <tr style='background:#ccc;'> 
        <th class='fifty'>ID</th> 
        <th class='twohundredfifty'>Description</th> 
        <th>Supplier#</th> 
        <th class='twohundred'>Supplier </th> 
        <th>ON WO</th> 
        <th>Stock</th> 
        <th>WIP</th> 
        <th>Reserve</th> 
        <th>On Order</th> 
       </tr> 
      </thead> 
      <tbody> 
    "; 
    while($row = $result->fetch_assoc()) { 
     echo "<tr>"; 
     echo " <td class='fifty'>".$row["itemid"]."</td>"; 
     echo " <td class='twohundredfifty'>".$row["descr"]."</td>"; 
     echo " <td>".$row["suplId"]."</td>"; 
     echo " <td class='twohundred'>".$row["suplProdCode"]."</td>"; 
     echo " <td>".$row["qty"]."</td>"; 
     echo " <td>".$row["qStk"]."</td>"; 
     echo " <td>".$row["qWIP"]."</td>"; 
     echo " <td>".$row["qRes"]."</td>"; 
     echo " <td>".$row["qOrd"]."</td>"; 
     echo "</tr>"; 
    } 
    echo " 
      </tbody> 
     </table> 
    "; 
} 

它也总是使用缩进,这样就可以保证标记的流量是正确的,检查你的代码一个很好的主意。

希望有所帮助。