0

在stackoverflow上问过一个类似的问题,但并没有真正得到任何地方。

此页面显示了我当前从我的 MSSQL 服务器获得的输出。

我有一张我们的活动发生的场地信息表(名称、地址等)。另外,我有一个计划的实际事件表(一个事件可能在一天内和/或多天发生多次)。我用这个查询加入这些表:

<?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();
}
?>

因此,当它遍历查询结果时,它会为每条记录创建一个数组,最终结果如下:

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] => info@thearkchurch.com
    [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] => info@thearkchurch.com
    [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] => ffcc@frontiernet.net
    [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
)

如您所见,方舟教堂及其关联的联系信息是重复的,因此当我使用这些数组并将它们输出到页面时,我会看到一堆重复的内容。

我想删除重复的信息,以便获得类似于以下的结果:

The Ark Church
Alan & Joy Clayton
450 Humble Tank Rd.
Conroe, TX  77305
(936) 756-1988
info@thearkchurch.com
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
ffcc@frontiernet.net
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

它不一定要以那样结束(我不是在寻找特定于这些结果的代码,而是如何不显示重复信息的概念)。我假设一个额外的foreachorwhile会做,但我还没有想出任何逻辑说<?php if ($location == $previouslocation) echo ""; ?>.

4

2 回答 2

0

哇...

我终于完成了我想要的(这让我很头疼)。我敢肯定还有其他方法(可能还有更好的方法)可以做到这一点。不过,这实际上以我想要的方式给了我结果。

PHP代码...

<!doctype html>
<html>
<head>
<title>Events | Faith Life Church - Branson, MO</title>
</head>
<body>
<h1>Events <abbr title="And">&amp;</abbr> Meetings</h1>
<?php
// get events from a database
try {
    // MSSQL has the ability to use Windows Authentication by not passing a username or password
    $dbh = new PDO("sqlsrv:Server=localhost;Database=Sermons", "", "");
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // get events from the main database
    $getoriginalevents = "SELECT TOP (100) PERCENT dbo.TblSermon.Day, dbo.TblSermon.Date, dbo.TblSermon.Time, dbo.TblSermon.Speaker, dbo.TblSermon.Series, dbo.TblSermon.Sermon, 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() }) AND (dbo.TblSermon.Notes NOT LIKE '%Not to be put on our Website%' OR dbo.TblSermon.Notes IS NULL)
        ORDER BY dbo.TblSermon.Date, dbo.TblSermon.Time";
    // prepared statements are good for security and speed
    $originalevents = $dbh->prepare($getoriginalevents);
    $originalevents->execute();
    // create an array using names instead of keys ($originalevent["Location"] instead of $originalevent[1])
    $originalevents->setFetchMode(PDO::FETCH_ASSOC);

    // delete the data from the temporary events table (we don't want duplicate events)
    $dbh->query("DELETE FROM dbo.Events");

    // insert events into a temporary events table (the original database doesn't contain all the information so PHP creates some)
    // this will be executed within foreach ()
    $putprocessedevent = $dbh->prepare("INSERT INTO dbo.Events (Title, Speaker, Venue, Pastors, Street, City, State, Zip, Map, Phone, Email, Website, Date) VALUES (:title, :speaker, :venue, :pastors, :street, :city, :state, :zip, :map, :phone, :email, :website, :date)");

    // for every record that the initial query produces...
    foreach ($originalevents as $originalevent) {
        // redefine the variables
        $title = $originalevent["Location"];
        if ($originalevent["MeetingName"]) $title = $originalevent["MeetingName"];
        if ($originalevent["Sermon"]) $title = $originalevent["Sermon"];
        if ($originalevent["Series"]) $title = $originalevent["Series"];
        $speaker = $originalevent["Speaker"];
        // if the record ISN'T from Sarasota or the road...
        if ($originalevent["Sarasota"] == 0 && $originalevent["NonFlc"] == 0) {
            $venue = "Faith Life Church";
            $pastors = "Keith & Phyllis Moore";
            $street = "3701 State Highway 76";
            $city = "Branson";
            $state = "MO";
            $zip = "65616";
            $map = "http://maps.google.com/maps?q=Faith+Life+Church+Branson+MO+65616";
            $phone = "+1-417-334-9233";
            $email = "general@moorelife.org";
            $website = "http://www.flcbranson.org";
        }
        // if the record IS from Sarasota...
        if ($originalevent["Sarasota"] == 1) {
            $venue = "Faith Life Church";
            $pastors = "Keith & Phyllis Moore";
            $street = "6980 Professional Parkway East";
            $city = "Sarasota";
            $state = "FL";
            $zip = "34240";
            $map = "http://maps.google.com/maps?q=Faith+Life+Church+Sarasota+FL+34240";
            $phone = "+1-941-388-6961";
            $email = "general@moorelife.org";
            $website = "http://www.flcsarasota.org";
        }
        // if the record IS from the road...
        if ($originalevent["NonFlc"] == 1) {
            $venue = $originalevent["Location"];
            if ($originalevent["Pastors"]) $pastors = $originalevent["Pastors"];
            $street = $originalevent["Address"];
            $city = $originalevent["City"];
            $state = $originalevent["State"];
            $zip = substr($originalevent["Zip"], 0, -2);
            $map = "http://maps.google.com/maps?q=" . str_replace(" ", "+", $originalevent["Location"]) . "+" . str_replace(" ", "+", $originalevent["City"]) . "+" . $originalevent["State"] . "+" . substr($originalevent["Zip"], 0 ,-2);
            if ($originalevent["Phone"]) $phone = "+1-" . substr($originalevent["Phone"], 1, 3) . "-" . substr($originalevent["Phone"], -8);
            if ($originalevent["Email"]) $email = $originalevent["Email"];
            if ($originalevent["WebAddress"]) $website = $originalevent["WebAddress"];
        }

        $date = date("Y-m-d", strtotime($originalevent["Date"]));
        if ($originalevent["Time"]) $date = date("c", strtotime(substr($originalevent["Date"], 0, -13) . $originalevent["Time"]));

        // execute the actual insertion of new events
        $putprocessedevent->execute(array(":title" => $title, ":speaker" => $speaker, ":venue" => $venue, ":pastors" => $pastors, ":street" => $street, ":city" => $city, ":state" => $state, ":zip" => $zip, ":map" => $map, ":phone" => $phone, ":email" => $email, ":website" => $website, ":date" => $date));
    }
    // kill the last record of the array
    unset($originalevent);

    // get events from the temporary events database
    $getprocessedtitles = "SELECT DISTINCT Title, MIN(EventsID) FROM dbo.Events GROUP BY Title ORDER BY MIN(EventsID)";
    $processedtitles = $dbh->prepare($getprocessedtitles);
    $processedtitles->execute();
    // create an array using names instead of keys ($originalevent["Location"] instead of $originalevent[1])
    $processedtitles->setFetchMode(PDO::FETCH_ASSOC);

    // for every record that the new query produces...
    foreach ($processedtitles as $processedtitle) {
        $eventtitle = $processedtitle["Title"];
?>
<h2><?php echo $eventtitle; ?></h2>
<dl>
    <dt>Speakers</dt>
<?php
        // get dates from the temporary events database
        $getprocessedspeakers = "SELECT DISTINCT Speaker, MIN(EventsID) FROM dbo.Events WHERE Title LIKE '%" . $eventtitle . "%'GROUP BY Speaker ORDER BY MIN(EventsID)";
        $processedspeakers = $dbh->prepare($getprocessedspeakers);
        $processedspeakers->execute();
        // create an array using names instead of keys ($originalevent["Location"] instead of $originalevent[1])
        $processedspeakers->setFetchMode(PDO::FETCH_ASSOC);
        foreach ($processedspeakers as $processedspeaker) {
            $eventspeaker = $processedspeaker["Speaker"];
?>
    <dd><?php echo $eventspeaker; ?></dd>
<?php
        }
?>
</dl>
<h3>Venue Information</h3>
<dl>
<?php
        // get dates from the temporary events database
        $getprocessedinfo = "SELECT Venue, Pastors, Street, City, State, Zip, Map, Phone, Email, Website FROM dbo.Events WHERE Title LIKE '%" . $eventtitle . "%'";
        $processedinfo = $dbh->prepare($getprocessedinfo);
        $processedinfo->execute();
        // create an array using names instead of keys ($originalevent["Location"] instead of $originalevent[1])
        // I used $processedinfo = $processedinfo->fetch(PDO::FETCH_ASSOC); instead of $processedtitles->setFetchMode(PDO::FETCH_ASSOC); so that I wouldn't have to do a separate array (print_r($processedtitles); shows the actual query and not the results of the query)
        $processedinfo = $processedinfo->fetch(PDO::FETCH_ASSOC);
        $eventvenue = $processedinfo["Venue"];
        $eventpastors = $processedinfo["Pastors"];
        $eventstreet = $processedinfo["Street"];
        $eventcity = $processedinfo["City"];
        $eventstate = $processedinfo["State"];
        $eventzip = $processedinfo["Zip"];
        $eventmap = $processedinfo["Map"];
        $eventphone = $processedinfo["Phone"];
        $eventemail = $processedinfo["Email"];
        $eventwebsite = $processedinfo["Website"];
?>
    <dt>Venue</dt>
    <dd><?php echo $eventvenue; ?></dd>
    <dt>Pastors</dt>
    <dd><?php echo $eventpastors; ?></dd>
    <dt>Street</dt>
    <dd><?php echo $eventstreet; ?></dd>
    <dt>City</dt>
    <dd><?php echo $eventcity; ?></dd>
    <dt>State</dt>
    <dd><?php echo $eventstate; ?></dd>
    <dt>Zip</dt>
    <dd><?php echo $eventzip; ?></dd>
    <dt>Map</dt>
    <dd><?php echo $eventmap; ?></dd>
    <dt>Phone</dt>
    <dd><?php echo $eventphone; ?></dd>
    <dt>Email</dt>
    <dd><?php echo $eventemail; ?></dd>
    <dt>Website</dt>
    <dd><?php echo $eventwebsite; ?></dd>
</dl>
<h3>Event Dates</h3>
<ol>
<?php
        // get dates from the temporary events database
        $getprocesseddates = "SELECT Date FROM dbo.Events WHERE Title LIKE '%" . $eventtitle . "%'";
        $processeddates = $dbh->prepare($getprocesseddates);
        $processeddates->execute();
        // create an array using names instead of keys ($originalevent["Location"] instead of $originalevent[1])
        $processeddates->setFetchMode(PDO::FETCH_ASSOC);
        // for every record that the new query produces...
        foreach ($processeddates as $processeddate) {
            $eventdate = $processeddate["Date"];
?>
    <li><?php echo $eventdate; ?></li>
<?php
        }
        // kill the last record of the array
        unset($processeddate);
?>
</ol>
<?php
    }
    // kill the last record of the array
    unset($processedtitle);

    // kill the database connection
    $dbh = null;
}
catch(PDOException $e) {
    echo $e->getMessage();
}
?>
</body>
</html>

结果呈现的 HTML...

Events & Meetings

KCM Branson Victory Campaign 2013
Speakers
    Kenneth Copeland
    Gloria Copeland
Venue Information
Venue
    Faith Life Church
Pastors
    Keith & Phyllis Moore
Street
    3701 State Highway 76
City
    Branson
State
    MO
Zip
    65616
Map
    http://maps.google.com/maps?q=Faith+Life+Church+Branson+MO+65616
Phone
    +1-417-334-9233
Email
    general@moorelife.org
Website
    http://www.flcbranson.org
Event Dates
    1. 2013-03-07T19:00:00-06:00
    2. 2013-03-08T14:00:00-06:00
    3. 2013-03-08T19:00:00-06:00
    4. 2013-03-08T09:00:00-06:00
    5. 2013-03-09T09:30:00-06:00

Victory Family Church
Speakers
    Keith Moore 

Venue Information
Venue
    Victory Family Church
Pastors
    John & Michelle Nuzzo
Street
    21150 Route 19
City
    Cranberry Township
State
    PA
Zip
    16066
Map
    http://maps.google.com/maps?q=Victory+Family+Church+Cranberry+Township+PA+16066
Phone
    +1-724-453-6200
Email
    general@moorelife.org
Website
    http://www.lifeatvictory.com

Event Dates
    1. 2013-08-19T19:00:00-05:00
    2. 2013-08-20T19:00:00-05:00
    3. 2013-08-21T19:00:00-05:00

也许这种经历会对其他人有所帮助。我花了将近一年的时间(断断续续地)让它发挥作用。随意张贴可以使这更清洁或更容易的东西。

于 2013-02-19T22:41:57.287 回答
0

您可能会想要使用 GROUP BY ... HAVING 语句。

尝试添加类似

GROUP BY dbo.TblSermon.Date HAVING MIN(dbo.TblSermon.Date)
于 2012-11-02T02:30:51.413 回答