Yoshioka
Yoshioka

Reputation: 815

How to make a new <tr> if id in the next record is different

Let's say I have this result of my query:

id_unsur | nama_subunsur
---------+--------------
       1 | subunsur1
       1 | subunsur1
       2 | subunsur3

I want to fetch my result into a table, and if id is the same as the id in the next result I don't want to echo a new <tr>, and if it's different then echo new <tr>. What I want is:

1 | subunsur1
2 | subunsur3

I want to do that in:

while ($data = mysql_fetch_array($query)) {
    // I want to do that here...
}

There is no problem in my query, I'm not using group by because the other field result is different. So how to do that? Any help will be appreciated.

Upvotes: 0

Views: 66

Answers (2)

Ibrahim Abou Khalil
Ibrahim Abou Khalil

Reputation: 322

To avoid Duplicated rows use DISTINCT clause in a select statment.

SELECT DISTINCT columns FROM table_name WHERE where_conditions;

Upvotes: 1

Shubham
Shubham

Reputation: 1288

Create an empty array outside while loop.

Whenever printing any id push its id in array.

before printing check if that id is present in that array.

Pseudocode

if(id present in array){
  dont print
}else {
  print tr
  push it to array
}

Upvotes: 1

Related Questions