0

私はstackoverflowで同様の質問をしましたが、実際にはどこにも行きませんでした。

このページには、MSSQL サーバーから現在取得している出力が表示されます。

イベントが発生する会場情報 (名前、住所など) のテーブルがあります。これとは別に、スケジュールされている実際のイベントの表があります (イベントは 1 日に複数回、および/または複数日に発生する場合があります)。これらのテーブルを次のクエリで結合します。

<?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

必ずしもそのようになる必要はありません (これらの結果に固有のコードを探しているわけではありませんが、重複した情報を表示しない方法の概念を探しています)。foreach追加のorwhileがそれを行うと想定していますが、 <?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

たぶん、この経験は他の誰かを助けるでしょう。これを機能させるには、ほぼ1年(断続的に)かかりました。これをよりクリーンまたは簡単にするものを自由に投稿してください。

于 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 に答える