Page 1 of 1

PHP Output Format

Posted: Tue Mar 29, 2011 1:47 pm
by AutoPilot
Hi All,

I need some help in grouping data in PHP. I have a MySQL query that executes and returns the names of libraries and services under those libraries. However, when I echo the data to my PHP page I get the following output which is displayed like:

ColHeader1 | ColHeader2
------------------------------------
Library1 | Service1
------------------------------------
Library1 | Service2
------------------------------------
Library1 | Service3
------------------------------------
Library1 | Service4
------------------------------------
Library1 | Service5
------------------------------------
Library2 | Service1
------------------------------------
Library2 | Service2
------------------------------------
Library2 | Service3
------------------------------------
Library2 | Service4
------------------------------------
Library2 | Service5
------------------------------------

Imagine that's a table 8O. I want to display the out in the following format:

ColHeader1 | ColHeader2
------------------------------------
Library1 | Service1
| Service2
| Service3
| Service4
| Service5
------------------------------------
Library2 | Service1
| Service2
| Service3
| Service4
| Service5

The PHP code that i'm using to display the data is as follows:

Code: Select all

		<?php
		
			mysql_connect("localhost", "root", "password") or die(mysql_error());
			mysql_select_db("libraryservices") or die(mysql_error());

			$uLoc = $_POST['Location'];
			$result = mysql_query("SELECT Name, ServName FROM (Library LEFT JOIN AvailableServices ON Library.LID = AvailableServices.LID) LEFT JOIN ServiceList ON AvailableServices.SLID = ServiceList.SLID WHERE Library.AID = $uLoc");						
			while($data = mysql_fetch_array($result))
			{
    			
    			echo "<tr>";
    			echo "<td bgcolor=\"F3F3F3\">".$data['Name']."</td>";
    			echo "<td bgcolor=\"F3F3F3\">".$data['ServName']."</td>";
    			echo "</tr>";
    			
			}
			
		?>

Any suggestions? Please help, i'm at a complete loss.

Kind regards,


AutoPilot

Re: PHP Output Format

Posted: Wed Mar 30, 2011 2:40 am
by divedj
Try to add GROUP BY and ORDER BY to your sql query like:

Code: Select all

$result = mysql_query("SELECT Name, ServName FROM (Library LEFT JOIN AvailableServices ON Library.LID = AvailableServices.LID) LEFT JOIN ServiceList ON AvailableServices.SLID = ServiceList.SLID WHERE Library.AID = $uLoc GROUP BY Name ORDER BY ServName");