0

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.

Myt
  • 204
  • 6
  • 16
  • 2
    Cant you group it by article id 1234,654 ? – v0d1ch Dec 03 '12 at 10:11
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). We can't see where `$period` comes from, but you may also be **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Dec 03 '12 at 10:12

2 Answers2

0

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 != "".

Christoffer Bubach
  • 1,676
  • 3
  • 17
  • 45
0

!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
Hugo Delsing
  • 13,803
  • 5
  • 45
  • 72
  • I can't order them by article, because there are other fields that the data needs to be ordered by. $result=mysql_query("SELECT * FROM database WHERE date = '$period' ORDER BY ABS(machine),shift,id ASC"); I left that ordering out before, sorry for that.. I did a new query inside the while loop: $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()); But here ordering by article does not help anymore. Any suggestions ? – Myt Dec 03 '12 at 11:06
  • Well, then the logic still applies. But then it means "show this row before starting a new article". EG: it will show 3 times article x, then the summary row for x, then two times article y, then the summary row for article y, then article x again and another summary row for article x. But there is always a change that by ordering the other col, its automatically ordered correctly for article to. Or use two orders. ORDER BY ABS(machine), article – Hugo Delsing Dec 03 '12 at 11:39
  • Hmm..can't still get this to work the way I want it to, when I added your logic then it showed the summary row after every article. Still appreciate your help though :) – Myt Dec 03 '12 at 12:28
  • Thank you, I will give it a go tomorrow ;). – Myt Dec 03 '12 at 14:37