わお...
私は最終的に私が望んでいたことを達成しました(そしてそれはお尻の痛みでした). それを行う他の方法(そしておそらくより良い方法)があると確信しています。ただし、これにより、実際には、私が望む方法で結果が得られます。
PHPコード...
<!doctype html>
<html>
<head>
<title>Events | Faith Life Church - Branson, MO</title>
</head>
<body>
<h1>Events <abbr title="And">&</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年(断続的に)かかりました。これをよりクリーンまたは簡単にするものを自由に投稿してください。