所以我有一个包含数据库值的表:
$result=mysql_query("SELECT * FROM database WHERE date = '$period'") or die (mysql_error());
while ($row_result = mysql_fetch_array($result) {
$article = $row_result['article'];
...
//It shows something like this:
id | article | some more data |
4 | 1234 | data |
8 | 654 | data |
9 | 654 | data |
显示了每一行,但现在我要做的是在每篇文章之后显示另一行总结一些值,但如果文章编号相同则显示一行后例如那两行。我现在不知道有多少文章编号相同,但是如果它们在一个表格中相互显示并且它们具有相同的值,那么我想在它们之后显示一行。
首先我做了这样的事情:
$article2 = "";
if ($article2 != $article) {
<td>...</td>
.....
}
$article2 = $article;
但是这对于独特的文章很有用,它在文章后面显示了一个额外的行,但如果我喜欢5行同一篇文章,那么它会显示第一篇文章之后的额外行。像这样:
id | article | some more data |
4 | 1234 | data |
extra | 1234 | sum data |
8 | 654 | data |
extra | 654 | sum data |
9 | 654 | data |
我需要某种逻辑的帮助,这种逻辑理解额外的行只显示在每个不同的文章编号之后。我希望你们明白我要做的事情:)我会感激一些帮助。
答案 0 :(得分:0)
确保$article2="";
在循环之外,如下所示:
$article2="";
while ($row_result = mysql_fetch_array($result) {
$article = $row_result['article'];
//...
if ($article2 != ($article || "")) {
print "<td>...</td>";
print " ..... ";
}
$article2 = $article;
//...
}
并且您还要检查!= ""
的第一个循环。
答案 1 :(得分:0)
!更新了您的评论。
$result=mysql_query("SELECT * FROM database WHERE date = '$period' ORDER BY ABS(machine),shift,id,article ASC");
$currentArticle = 0;
while ($row = mysql_fetch_array($result)) {
if ($row['article']!=$currentArticle) { //check1
if ($currentArticle != 0) { //check2
//show extra row for article > $currentArticle
//your query doesnt contain this article, but I guess that is correct?
$result_kogus = mysql_query("SELECT SUM(amount), SUM(reject), SUM(work) FROM database WHERE date = '$periood' AND machine = '".$row['machine']."' AND order = '".$row['order']."'") or die (mysql_error());
//print row
}
$currentArticle = $row['article'];
}
//show regular row
}
if ($currentArticle != 0) { //check 3 > print summary after last row
$result_kogus = mysql_query("SELECT SUM(amount), SUM(reject), SUM(work) FROM database WHERE date = '$periood' AND machine = '".$row['machine']."' AND order = '".$row['order']."'") or die (mysql_error());
//print row
}
//What would happen on your example data:
//id | article | some more data |
//4 | 1234 | data |
//8 | 654 | data |
//9 | 654 | data |
//round 1, article 1234
//check1 = true (1234!=0)
//check2 = false (0!=0)
//currentArticle is changed to 1234
//regular row printed
//round2, article 654
//check1 = true (654!=1234)
//check2 = true (1234!=0)
//query is executed
//summary is printed for 1234
//currentArticle is changed to 654
//regular row printed
//round3, article 654
//check1 = false (654!=654)
//regular row printed
//loop ends
//check3 = true (654!=0)
//query is executed
//summary is printed for 654