Removing Duplicate Data From SQL Query Output For Display On A Web Page [migrated]
- by doubleJ
I had asked a similar question on stackoverflow but didn't really get anywhere.
This page shows the output that I'm currently getting from my MSSQL server.
I have a table of venue information (name, address, etc...) that our events happen on. Separately, I have a table of the actual events that are scheduled (an event may happen multiple times in one day and/or over multiple days). I join those tables with this query:
<?php
try {
$dbh = new PDO("sqlsrv:Server=localhost;Database=Sermons", "", "");
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT TOP (100) PERCENT dbo.TblSermon.Day, dbo.TblSermon.Date, dbo.TblSermon.Time, dbo.TblSermon.Speaker, dbo.TblSermon.Series, dbo.TblSermon.Sarasota, dbo.TblSermon.NonFlc, dbo.TblJoinSermonLocation.MeetingName, dbo.TblLocation.Location, dbo.TblLocation.Pastors, dbo.TblLocation.Address, dbo.TblLocation.City, dbo.TblLocation.State, dbo.TblLocation.Zip, dbo.TblLocation.Country, dbo.TblLocation.Phone, dbo.TblLocation.Email, dbo.TblLocation.WebAddress
FROM dbo.TblLocation RIGHT OUTER JOIN dbo.TblJoinSermonLocation ON dbo.TblLocation.ID = dbo.TblJoinSermonLocation.Location RIGHT OUTER JOIN dbo.TblSermon ON dbo.TblJoinSermonLocation.Sermon = dbo.TblSermon.ID
WHERE (dbo.TblSermon.Date >= { fn NOW() })
ORDER BY dbo.TblSermon.Date, dbo.TblSermon.Time";
$stmt = $dbh->prepare($sql);
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach ($stmt as $row) {
echo "<pre>";
print_r($row);
echo "</pre>";
}
unset($row);
$dbh = null;
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>
So, as it loops through the query results, it creates an array for each record and ends up like this:
Array
(
[Day] => Tuesday
[Date] => 2012-10-30 00:00:00.000
[Time] => 07:00 PM
[Speaker] => Keith Moore
[Location] => The Ark Church
[Pastors] => Alan & Joy Clayton
[Address] => 450 Humble Tank Rd.
[City] => Conroe
[State] => TX
[Zip] => 77305.0
[Phone] => (936) 756-1988
[Email] => [email protected]
[WebAddress] => http://www.thearkchurch.org
)
Array
(
[Day] => Wednesday
[Date] => 2012-10-31 00:00:00.000
[Time] => 07:00 PM
[Speaker] => Keith Moore
[Location] => The Ark Church
[Pastors] => Alan & Joy Clayton
[Address] => 450 Humble Tank Rd.
[City] => Conroe
[State] => TX
[Zip] => 77305.0
[Phone] => (936) 756-1988
[Email] => [email protected]
[WebAddress] => http://www.thearkchurch.org
)
Array
(
[Day] => Tuesday
[Date] => 2012-11-06 00:00:00.000
[Time] => 07:00 PM
[Speaker] => Keith Moore
[Location] => Fellowship Of Faith Christian Center
[Pastors] => Michael & Joan Kalstrup
[Address] => 18999 Hwy. 59
[City] => Oakland
[State] => IA
[Zip] => 51560.0
[Phone] => (712) 482-3455
[Email] => [email protected]
[WebAddress] => http://www.fellowshipoffaith.cc
)
Array
(
[Day] => Wednesday
[Date] => 2012-11-14 00:00:00.000
[Time] => 07:00 PM
[Speaker] => Keith Moore
[Location] => Faith Family Church
[Pastors] => Michael & Barbara Cameneti
[Address] => 8200 Freedom Ave NW
[City] => Canton
[State] => OH
[Zip] => 44720.0
[Phone] => (330) 492-0925
[Email] =>
[WebAddress] => http://www.myfaithfamily.com
)
As you can see, The Ark Church and its associated contact information is duplicated, so when I work with those arrays and output them to the page, I see a bunch of duplicate content.
I'd like to remove the duplicate information so that I get results similar to this:
The Ark Church
Alan & Joy Clayton
450 Humble Tank Rd.
Conroe, TX 77305
(936) 756-1988
[email protected]
http://www.thearkchurch.org
Meetings:
Tuesday, 2012-10-30 07:00 PM
Wednesday, 2012-10-31 07:00 PM
Fellowship Of Faith Christian Center
Michael & Joan Kalstrup
18999 Hwy. 59
Oakland, IA 51560
(712) 482-3455
[email protected]
http://www.fellowshipoffaith.cc
Meetings:
Tuesday, 2012-11-06 07:00 PM
Faith Family Church
Michael & Barbara Cameneti
8200 Freedom Ave NW
Canton, OH 44720
(330) 492-0925
http://www.myfaithfamily.com
Meetings:
Wednesday, 2012-11-14 07:00 PM
It doesn't necessarily have to end up like that (I'm not looking for code specific for these results, but a concept of how to not show the duplicated information). I'm assuming that an additional foreach or while will do it, but I haven't figured out any logic that says <?php if ($location == $previouslocation) echo ""; ?>.