Avoiding repetitions of same field data

Current approach with PDO

Sort the result set by the grouping field, remember the previous value, and print the name only when it changes. This is a presentation rule, so all database rows are still available to the page.

<?php
$stmt = $pdo->query(
    'SELECT name, subject, mark FROM student_mark ORDER BY name, subject'
);

$previousName = null;
foreach ($stmt as $row) {
    $name = $row['name'] === $previousName ? '—' : $row['name'];

    echo '<tr>';
    echo '<td>' . htmlspecialchars($name, ENT_QUOTES, 'UTF-8') . '</td>';
    echo '<td>' . htmlspecialchars($row['subject'], ENT_QUOTES, 'UTF-8') . '</td>';
    echo '<td>' . (int) $row['mark'] . '</td>';
    echo '</tr>';

    $previousName = $row['name'];
}
?>
Legacy example below: the older sample uses the removed mysql_query()/mysql_fetch_array() API. Keep it only for understanding older code; use PDO or MySQLi for current PHP.
There can be a requirement where we have to limit display the value of a filed once only in a group while displaying all records. We can display the filed value if it changes only. This way we can display a more user friendly way to our visitors. A group by query will display the records in groups but the value of the field will be displayed in every record. Here we will try to avoid repetition of same field data

Let us take the example of a student table where we have stored all the marks of the students. While displaying we will show all the mark of a student in one group but we don't want the student name should be displayed ( or repeat ) along with every records of different subject. It should display once only. After finishing one student the next group will show another group with student name display only for the first record.

As you can see here we are only managing the display part through PHP but collecting the records are same only.

Please read the basic of how records are displayed in php mysql

We will use one variable to store the name inside the while loop. In next looping cycle we will check whether the next name is same as the name stored in variable. If it is same then we will display some empty space or dashes, else (a new name has come) let us display that name. Here is the display part .
Legacy code: the following block uses the removed mysql_* API and is retained only to explain the original technique.
$q=mysql_query("select * from student_mark order  by  name ");

$name="";

echo "<table width='200' border='0' cellspacing='1' cellpadding='0'>";

while($nt=mysql_fetch_array($q)){



if($name<>$nt['name']){

echo "<tr ><td> $nt[name]</td><td>$nt[subject]</td><td> $nt[mark] </td></tr>";

}else{

echo "<tr ><td> ------</td><td>$nt[subject]</td><td> $nt[mark] </td></tr>";

}

$name=$nt['name'];

}

echo "</table>";
Download the full script for displaying records
Query with Error message


Subscribe to our YouTube Channel here



plus2net.com







Suranga Bandara

14-08-2015

$sql = mysql_query("SELECT DISTINCT table1.id, table2.id, table2.name
FROM table1, table2 WHERE id=id GROUP BY name");

Use `DISTINCT` and `GROUP BY name`.. If not every time repeat data.
Cheers !!!




PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer