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
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>";
}
?>
Kind regards,
AutoPilot