#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0

    Avoiding repetition in query results


    I have a mysql query whose results I display on a php page.

    The value in the row ['gr'] is the same in every line. So, what I would like to do is to avoid repeating it in every line. At the moment the query results are displayed in the following way

    gr, fc, hr, dr, tc
    gr, fc, hr, dr, tc

    etc

    What I would like to do is to display the results the following way:

    gr (once)

    fc, hr, dr, tc (for every line )


    This is the current code:

    while($row = mysql_fetch_array ($result))
    {
    echo'
    <tr>
    <td align="left">
    '. $row['gr'] .'</td>
    <td align="left">
    '. $row['fc'] .'</td>
    <td align="left">
    '. $row['hr'] .'</td>
    <td align="left">
    '. $row['dr'] .'</td>
    <td align="left">
    '. $row['tc'] .'</td>


    </tr>';



    Thanks in advance for your help
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,911
    Rep Power
    1045
    Hi,

    I think we need a FAQ entry for this question ...

    In the query, order the result set by the "gr" column. Create a variable for the last "gr" value outside of the row loop. In the loop, compare the current "gr" value with the last one, and if they're equal, leave the cell empty (or display whatever you want).

    Comments on this post

    • requinix agrees : nudge nudge
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0
    Thanks for the quick reply.

    I understand what you are saying but am not sure how to put it in practice.


    Ok so I should change the order by to the gr value.

    And then something like this (which doesn't work )

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

    if ($row['gr'] !=$group){
    $group=$row['gr'];

    echo'
    <tr>
    <td align="left">
    '. $row['gr'] .'</td>


    <td align="left">
    '. $row['fc'] .'</td>
    <td align="left">
    '. $row['hr'] .'</td>
    <td align="left">
    '. $row['dr'] .'</td>
    <td align="left">
    '. $row['tc'] .'</td>


    </tr>';
    }

    else {
    echo'<td align="left">
    '. $row['fc'] .'</td>
    <td align="left">
    '. $row['hr'] .'</td>
    <td align="left">
    '. $row['dr'] .'</td>
    <td align="left">
    '. $row['tc'] .'</td>
    </tr>';
    }

    echo '</table>';
    mysql_free_result($result);
    }
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,911
    Rep Power
    1045
    Sorry, but I don't know what "It doesn't work" means. You'll have to be more specific.

    I can barely read the code, because you're not using code tags. But the loop looks messed up. Why is there a mysql_free_result()?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    22
    Rep Power
    0
    It's ok I've worked it out. Thanks anyway
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    37
    Rep Power
    1
    In your query, instead of
    Code:
    SELECT colname FROM table
    Use,
    Code:
    SELECT DISTINCT colname FROM table
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,911
    Rep Power
    1045
    Originally Posted by The Alchemist
    In your query, instead of ...
    Um, did you actually read the question? He has five(!) columns. The value of one(!) of those columns is always the same, but the other values are different. Now he wants to display all rows, but he wants to replace the repeated value with something different.

    What you query does is display the unique combinations of those five columns. Nobody said anything about that.

IMN logo majestic logo threadwatch logo seochat tools logo