我想在一个变量中创建一个表(稍后将其发送给js),然后显示它。但我的回声结果只是表头('Produkty','Ilość','Cena')。你能告诉我我犯错误的地方或纠正我吗?
<?php
header('Access-Control-Allow-Origin: *');
include "database.php";
$dane = array();
$tabela='<table class="table table-striped">
<thead>
<tr>
<th>Produkt</th>
<th>Ilość</th>
<th>Cena</th>
</tr>
</thead>
<tbody>';
$dane=array();
$sql_main="SELECT Products.`Name`,Orders_NEW.`Amount`,((Products.`Price`)*(Orders_NEW.`Amount`)) as 'PRICE' FROM `Orders_NEW` inner join `Products` on Orders_NEW.`Product`=Products.`ID` AND `Order_ID`=669";
$dane = $db->query($sql_main);
foreach($dane as $row)
{
$tabela.="<tr><td>".$row['Name']."</td><td>".$row['Amount']."</td><td>".$row['Price']."</td></tr>";
}
$sql_second="SELECT SUM((Products.`Price`)*(Orders_NEW.`Amount`)) as 'SUMA' FROM `Orders_NEW` inner join `Products` on Orders_NEW.`Product`=Products.`ID` AND `Order_ID`=669";
$dane_second= array();
$dane_second= $db -> query($sql_second);
foreach($dane_second as $row)
{
$tabela.='<thead>
<tr>
<th>Łącznie</th>
<th></th>
<th>'.$row["SUMA"].'</th>
</tr>
</thead>
</table>';
}
echo($tabela);
?>
已编辑:将foreach更改为while($ row = $ dane-&gt; fetch_assoc()) 现在我的结果是:
ProduktIlośćCena
Łącznie
似乎像$ row ['Name']等变量就是这里的问题
答案 0 :(得分:0)
我不确定你是否有某种类型的自定义类与database.php,但如果$ db只包含mysqli对象,那么你需要执行以下操作:
$dane = $db->query($sql_main);
while($row = $dane->fetch_assoc()) {
$tabela.="<tr><td>".$row['Name']."</td><td>".$row['Amount']."</td><td>".$row['Price']."</td></tr>";
}
第二个查询也是如此(假设你的SQL是正确的并且顺便返回结果)。
答案 1 :(得分:0)
您的代码中有错误:
id_clien | zipcode | timestamp | trancheUC | MISS
--------------------------------------------------|-------- |------
amar | 11111 | 2016-09-28 20:05:03.001 | TB | 0
akbar | 11111 | 2016-09-28 20:05:03.001 | M | 1
antony | 11111 | 2016-09-28 20:07:03.001 | TM | 0
amar | 11111 | 2016-09-28 20:08:03.001 | TB | 1
将关闭表移出foreach。