Jump to content

Recommended Posts

I know there's a way to do this but don't know how...

 

I want to be able to pull all the data out in groups.

example

 

ART -

FIRST LAST

FIRST LAST

 

BAND -

FIRST LAST

FIRST LAST

FIRST LAST

 

This is what I have right now. "tgroup" is the name of the groups.

 

<?php
			// Connect to the database.
			require_once ('mysql_connect.php');

			// Make the query.
			$query = "SELECT user_id, first_name, last_name FROM users WHERE webaccess='2' ORDER BY tgroup, last_name ASC";

			// Run the query.
			$result = @mysql_query ($query);

			// If the query ran w/o error, print out the results.
			if ($result)
			{
				// Table header.
				echo '<h3><span style="color: #af410d;">Current Faculty</span></h3>
				<table>';

				// Fetch and print all the records.
				while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
				{
					echo '
					<tr>
					<td width="20px"></td>
					<td>- <a href="teacherpages.php?id=' . $row['user_id'] . '">' . $row['first_name'] . ' ' . $row['last_name'] . '</a><br></td>
					</tr>';
				}

				// Close the table.
				echo '</table>';

				// Free up the resources.
				mysql_free_result ($result);

			}
			else
			{
				// If the query did not run successfully, print out an error message.
				echo 'No Teachers in the system';
			}

			// Close the database connection.
			mysql_close();
			?>

 

Thanks for the help

Link to comment
https://forums.phpfreaks.com/topic/135295-solved-grouping/
Share on other sites

try

<?php

            // Connect to the database.
            require_once ('mysql_connect.php');

            // Make the query.
            $query = "SELECT user_id, first_name, last_name, tgroup FROM users WHERE webaccess='2' ORDER BY tgroup, last_name ASC";
            
            // Run the query.
            $result = @mysql_query ($query);
            
            // If the query ran w/o error, print out the results.
            if ($result)
            {
               // Table header.
               echo '<h3><span style="color: #af410d;">Current Faculty</span></h3>
               <table>';
               $last_group = '';
               // Fetch and print all the records.
               while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
               {
               	if ($row['tgroupd'] != $last_group){
               		echo '
                  <tr>
                  <td width="20px"></td>
                  <td>'.$row['tgroup'] . ' -</td>
                  </tr>';
               		$last_group = $row['tgroup'];
               	}
                echo '
                <tr>
                <td width="20px"></td>
                <td>- <a href="teacherpages.php?id=' . $row['user_id'] . '">' . $row['first_name'] . ' ' . $row['last_name'] . '</a><br></td>
                </tr>';
                  
               }
                              
               // Close the table.
               echo '</table>';
               
               // Free up the resources.
               mysql_free_result ($result);
               
            }
            else
            {
               // If the query did not run successfully, print out an error message.
               echo 'No Teachers in the system';
            }
            
            // Close the database connection.
            mysql_close();
            ?>
?>

Link to comment
https://forums.phpfreaks.com/topic/135295-solved-grouping/#findComment-704809
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.