sorting mysql data with the same fields

etomak26

New Member
Messages
10
Reaction score
0
Points
0
hello guys, just want to ask some newbie questions. i have a table named "students" in my database, the field names are "Lastname", "Firstname" and "Highschool". i want to view all records and sort my query by highschool field. i want to show that query results in a html table using php. how can i sort by showing all schools in the database and under each schools, i want all students' names that are in the same school. the highschool name is the header of the table and the content of that header will be the names of students under the name of the highschool. in the image that i provided, there are two tables of the school "Pasig Catholic College" i want that to become as one table and the names under that school is in the "Pasig Catholic College" table.

please help me with my codes.

here are the codes i use:

Code:
<?php

        $query = "SELECT * FROM students ORDER BY highschool";
        $result = mysql_query($query);
        $numrows = mysql_num_rows($result);


        while($row = mysql_fetch_array($result)) {
        echo "<table border='1'>";
        echo "<tr>";
        echo "<th align='left'>" . $row['highschool'] . "</th>";
        echo "</tr>";
        echo "<tr>";
        echo "<td width='450'>" . $row['lastname'] . ", " . $row['firstname'] . "</td>";
        echo "</tr>";
        echo "<p>&nbsp</p>";
        }
echo "</table>";

?>


sample.jpg
 
Last edited:

VPmase

New Member
Messages
914
Reaction score
1
Points
0
Try this:

Code:
<?php

$query = "SELECT * FROM students ORDER BY highschool";
$result = mysql_query($query);
$numrows = mysql_num_rows($result);

$n = 0;
while($row = mysql_fetch_array($result)) {
if($n>0 && $row['highschool'] == $hs[$n-1]){
$n -= 1;
}
$hs[$n] = $row['highschool'];
$lastname[$hs[$n]][] = $row['lastname'];
$firstname[$hs[$n]][] = $row['firstname'];
$numstudents[$hs[$n]][] = sizeof($firstname[$hs[$n]]);
$n++;
}
for($a=0;$a<$n;$a++){
echo "<table border=\"1\">
<tr>
<th align=\"left\">
" . $hs[$a] . "
</th>
</tr>
<tr>
<td width=\"450px\">
";
for($b=0;$b<$numstudents[$hs[$a]];$b++){
echo $lastname[$hs[$a]][$b] . ", " . $firstname[$hs[$a]][$b] . "
</td>
</tr>
<p>&nbsp</p>
</table>";
}
}
?>

I believe that should work but tell me if it doesn't.
 

etomak26

New Member
Messages
10
Reaction score
0
Points
0
that didn't work. but i already work on my codes and make it to work. thanks for your effort. i really appreciate it. :biggrin:

Topic Solved. Please close this thread.
 
Last edited:
Top