Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm revisiting this as I'm working on another page that I want to use the same concept. 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 a query (as seen below).

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

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
)

What I'm wanting to do is combine those arrays, in some fashion, so that the venue information isn't repeated every time, but each date/time is show.

I tried typing it out in array style, but couldn't figure out the appropriate multi-dimensionality for it. I'm just pasting how I would like it to display, since that is how it will end up.

The Ark Church
    Contact:
        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
    Contact:
        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
    Contact:
        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, but it should give a good idea of what I'm looking for.

I don't necessarily have to create a new array. I just want to not show the same information over and over and over.

I was thinking that I could just do some form of compare, in foreach() that says something along the lines of "if Location is the same as previous Location", but I haven't figured out how to do it (is there a way to cache a previous variable with doing $location1 = [Location], $location2 = [Location], etc...?

One thing to note...

These examples don't have different speakers, but sometimes there are. I would like to be able to access the speaker, somehow. I assume that I would want it bound to the actual event.

JJ

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
124 views
Welcome To Ask or Share your Answers For Others

1 Answer

One technique: if you have a loop like this:

while ($row = $query->fetch(PDO::FETCH_NUM) ) {
    list($time,$place,$etc) = $row;
    // display formatted data
}

try this instead:

$row = $query->fetch();
do {
    list($time,$place,$etc) = $row;
    $row = $query->fetch(PDO::FETCH_NUM);
    if ($time != $row[0] && $place != $row[1] && $etc != $row[2]) {
        // display formatted data
    }
} while ($row);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...