Avoiding repetitions of same field data

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 .

$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

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 !!!

Post your comments , suggestion , error , requirements etc here .




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