SQL PHP HTML ASP JavaScript articles and free scripts to download
 
 

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

Further readings
PHP MySql Query
PHP MySql Data Display
Displaying records without repetitions of same field data
PHP Paging or breaking records per page
PHP MySQL Connection
Checking MySQL by phpinfo


Sections
PHP
JavaScript
ASP
HTML
SQL
Photoshop
Articles SEO
PHP Tutorials
PHP Monthly Planner
PHP Introduction
Loops & structure
Array
Date & Time
Functions
Form Handling
File Handling
Math Functions
String Functions
GD Functions
Comment Posting
Content Management
PHP & Ajax
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.