简体   繁体   中英

php and html table logic

So I have an table that has values from database:

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

Each row is shown, but now what I would like to do is show another row after each article that sums up some values, but if the article numbers are the same then show one row after for example those two. I don't now how many article numbers are the same, but if they are shown after one another in a table and they have the same value then I would like to show a row after them.

First I did something like this:

 $article2 = "";
 if ($article2 != $article) {
    <td>...</td>
       .....  
 }
 $article2 = $article;

But this is useful for unique article, it shows an extra row after an article, but if I have like 5 rows with the same article it shows the extra row after the first article. Like so:

    id  |  article |  some more data |
    4   |  1234    |  data           |
extra   |  1234    |  sum data       |
    8   |  654     |  data           |
extra   |  654     |  sum data       |
    9   |  654     |  data           |

I need help with some kind of logic that understands that the extra row is only shown after each different article number. I hope you guys understand what I am trying to do :) I would appreciate some help.

Make sure that $article2=""; is outside the loop, like this:

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

And that you also check for first loop with != "" .

!Updated with your comments.

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM