+ Reply to Thread
Results 1 to 3 of 3

Thread: sorting mysql data with the same fields

  1. #1
    etomak26 is offline x10Hosting Member etomak26 is an unknown quantity at this point
    Join Date
    Sep 2007
    Posts
    10

    sorting mysql data with the same fields

    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>";
    
    ?>

    Last edited by etomak26; 09-08-2008 at 07:53 PM. Reason: added code tag

  2. #2
    VPmase's Avatar
    VPmase is offline x10 Elder VPmase is an unknown quantity at this point
    Join Date
    Nov 2007
    Location
    Dixon, IL, USA
    Posts
    914

    Re: sorting mysql data with the same fields

    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.

  3. #3
    etomak26 is offline x10Hosting Member etomak26 is an unknown quantity at this point
    Join Date
    Sep 2007
    Posts
    10

    Thumbs up Re: sorting mysql data with the same fields

    that didn't work. but i already work on my codes and make it to work. thanks for your effort. i really appreciate it.

    Topic Solved. Please close this thread.
    Last edited by etomak26; 09-08-2008 at 08:04 PM. Reason: requesting to close this thread. topic solved.

+ Reply to Thread

Similar Threads

  1. [PHP] MySQL and PHP
    By Bryon in forum Tutorials
    Replies: 43
    Last Post: 03-24-2011, 07:27 AM
  2. New Site-Suggestions?
    By mnoutside in forum Review My Site
    Replies: 9
    Last Post: 08-27-2008, 07:01 AM
  3. MySQL Data BAck up needed
    By gomcodoctor in forum Free Hosting
    Replies: 2
    Last Post: 08-14-2008, 05:05 PM
  4. Replies: 2
    Last Post: 11-20-2007, 11:15 PM
  5. Have a problem with my forum
    By tikloos in forum Scripts & 3rd Party Apps
    Replies: 43
    Last Post: 01-19-2006, 01:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers