Hello,
I have the following PHP code:

$query = mysql_query("SELECT
societati.denumire_soc,
incasari.Data_Facturii,
incasari.Nr_Factura,
incasari.ARTICOLNUME,
incasari.Valoare_factura,
incasari.suma_achitata,
incasari.modalitate_plata,
incasari.data_achitat
FROM
incasari
Inner Join users ON users.id_vip = incasari.inspectorid
Inner Join societati ON societati.id_vip = incasari.PartID
WHERE incasari.inspectorid = $vipid
ORDER BY societati.denumire_soc ASC, incasari.Data_Facturii ASC");


$htmloutput = '';



$htmloutput .= '<br/>


<div id="wrapper3" align="center">

<center><h2>Situatie autorizatii societati comerciale</h2></center>

<table width="100%" border="0" cellspacing="0 cellpadding="5">
    <tr>
       <td class="colhead">Denumire societate</td>
       <td class="colhead">Data Factura</td>
       <td class="colhead">Nr Factura</td>
       <td class="colhead">Articol</td>
       <td class="colhead">Valoare Factura</td>
       <td class="colhead">Suma Achitata</td>
       <td class="colhead">Modalitate Plata</td>
       <td class="colhead">Data Plata</td>

</tr>';


$societate = '';


while ($row = mysql_fetch_array($query))
{



$htmloutput .=  
   '<tr>

<td class="societati">'.$row["denumire_soc"].'</td>
<td class="societati">'.$row["Data_Facturii"].'</td>
<td class="societati">'.$row["Nr_Factura"].'</td>
<td class="societati">'.$row["ARTICOLNUME"].'</td>
<td class="societati">'.$row["Valoare_factura"].'</td>
<td class="societati">'.$row["suma_achitata"].'</td>
<td class="societati">'.$row["modalitate_plata"].'</td>
<td class="societati">'.$row["data_achitat"].'</td>

</tr>';



}



$htmloutput .='</table>
</div>';

this outputs:

SOCIETY1    2010-02-18    ADA140436    Dr aut ambiental (unitati alimentatie publica) an 2010    3094    3094    OP    2010-02-03
SOCIETY1    2010-02-18    ADA140436    Dr aut ambiental (turism) an 2010    3094    3094    OP    2010-02-03

As you can see the rows are duplicated by fourth field.

I want to display it like this:

SOCIETY1    2010-02-18    ADA140436    Dr aut ambiental (unitati alimentatie publica) an 2010    3094    3094    OP    2010-02-03
                                       Dr aut ambiental (turism)

    This is a common question, and here's how I usually do it:

    1. ORDER BY the field that you want to group the output by. For example, do you want to display the full row whenever the Nr_Factura changes? If so, I would add that column onto the end of your ORDER BY list.

    2. Initialize a temporary variable outside of the loop that retrieves results. You should give it a value that you don't expect to occur in the result set (I often use NULL).

    3. Inside the loop, check to see if the current row's Nr_Factura value is equal to the value stored in the temporary variable.

      If it is equal, then the row you're currently processing is within the same "group" as the previous row (meaning you shouldn't output the "header" information that you only want to appear with the first result in the group).

      If it is not equal, then the row you're currently processing is the beginning of a different group (e.g. the previous row processed was the last in the previous group). As such, you would want to output the header information here.

    4. At the end of each loop iteration, update the temporary variable with the value of the data that you're grouping on (e.g. Nr_Factura).

    Note that if you want to detect a change in any of the first three fields (rather than just Nr_Factura or any other single column), one solution would be to concatenate the data from all three fields and store that value into the temporary variable (don't forget to do the same concatenation when checking the previous value as well!).

      @,

      Thank you for your reply.
      That helped me a lot!

      Now what i want to do is determine the number of incasari.ARTICOLNUME for each Nr_Factura, so that I can rowspan the other <td>'s with that value.

        mannerheim;10984333 wrote:

        (...) determine the number (...) so that I can rowspan the other <td>'s with that value.

        Do exactly what you do now, but hold off on concatenating this string to $htmloutput a little longer, to see how many rows you get. I'd construct an array that looks like

        $rows = array(); #to hold row data
        $rows[0] = array of all data from first row
        
        all other rows added
        $rows[>0] = array of everything _except_
        			$row["denumire_soc"]
        			$row["Data_Facturii"]
        			$row["Nr_Factura"]
        

        When you notice that nr_factura changes, you have count($rows) to tell you what rowspan to use. Moreover, you can array_shift($rows) to get the first row and build the string for this first row, inserting rowspan as needed.

        After that you can foreach over the rest of the rows to replace each row array with a string by using implode('</td><td>'), and from this point you can do the same for the entire set of rows with implode('</tr><tr>').

          Write a Reply...