0

所以我有一个包含数据库值的表:

$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           |

我需要某种逻辑方面的帮助,以了解额外的行仅显示在每个不同的文章编号之后。我希望你们理解我正在尝试做的事情:) 我将不胜感激。

4

2 回答 2

0

确保它$article2="";在循环之外,如下所示:

$article2="";
while ($row_result = mysql_fetch_array($result) {
  $article = $row_result['article'];
  //...
  if ($article2 != ($article || "")) {
    print "<td>...</td>";
    print "  .....  ";
  }
  $article2 = $article;
  //...
}

并且您还使用!= "".

于 2012-12-03T10:21:55.520 回答
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
于 2012-12-03T10:30:31.393 回答