while循环重复记录按列数记录

The following code queries a database and works great when there is only one column. However, I would like to have 3 columns of data. The code generates a heading correctly and extends the width of the page, but when trying to loop the records, it will duplicate by the number of columns. I'm very close, but need some guidance.

    <div class="directory">
    <div class="group">

    <?php

    // new variables
    $prev ='';      

    //Start a while loop to process all the rows
    while ($row = mysql_fetch_assoc ($result_set))
    {

        $Heading = $row['Heading'];
        $Name = $row['Name'];
        $Address =  $row['Address'];
        $City =  $row['City'];
        $Phone =  $row['Phone'];

        // Unique header code
        if($Heading != $prev) 
        {
    ?>
            <div class="row" style="background-color:#666" >
                <div class="col-md-12"><p><?php echo $Heading; ?></p></div>
            </div>
    <?php
        }
        $prev = $Heading;
    ?>
            <div class="row">
    <?php
                if($Heading = $prev)
                {
    ?>
                <div class="col-md-4">
                    <p><strong><?php echo $Name; ?></strong><br><?php echo $Address; ?><br><?php echo $City; ?><br><?php echo $Phone; ?></p>
                </div>
                <div class="col-md-4">
                    <p><strong><?php echo $Name; ?></strong><br><?php echo $Address; ?><br><?php echo $City; ?><br><?php echo $Phone; ?></p>
                </div>
                <div class="col-md-4">
                    <p><strong><?php echo $Name; ?></strong><br><?php echo $Address; ?><br><?php echo $City; ?><br><?php echo $Phone; ?></p>
                </div>
    <?php
                }
    ?>
            </div>
    <?php
    } //END WHILE
    ?>

    </div>
</div>

The problem is that Heading as a column would have to be the same for several results, and you have to go through each heading individually, pull the records, and display them in 3 columns.

This is the code:

<div class="directory">
    <?php 

    // retrieve all the Headings
    $q = mysql_query("SELECT DISTINCT `Heading` FROM contacts WHERE category = 'church'"); 

    // if there are any headings at all, then let's go through each one
    if(mysql_num_rows($q)>0):
        while ($group = mysql_fetch_assoc($q)):



    // now retrieve churches within that group
    $result_set = mysql_query("SELECT * FROM contacts WHERE `Heading` = '".mysql_real_escape_string($group['Heading'])."' AND category = 'church' ");

    //Start another (nested) loop to process all the rows
    $count = mysql_num_rows($result_set);

    // skip this group if it doesn't contain any records
    if($count<1) continue;

    // display the heading
    ?>
    <div class="group">


        <div class="row" style="background-color:#666" >
            <div class="col-md-12"><p><?php echo $group['Heading']; ?></p></div>
        </div>
        <div class="row">
            <?php

            // we need a counter to know how many we've displayed
            $i = 0;
            while ($row = mysql_fetch_assoc ($result_set)) :
                $i++; // increment every time

                # the function extract() extracts an array as variables
                # so you don't have to assign values manually
                extract ($row);


                # display the row
                ?>
                <div class="col-md-4">
                    <p>
                        <strong><?php echo $Name; ?></strong><br />
                        <?php echo $Address; ?><br />
                        <?php echo $City; ?><br />
                        <?php echo $Phone; ?>
                    </p>
                </div>

                <?php
                # if we displayed 3 results and there are more results to be shown, then close that div (row) and start a new one
                if($i%3==0 && $i<$count-1):
                ?>
                </div>
                <div class="row">
                <?php endif; ?>

            <?php endwhile; //END WHILE ?>
        </div>
    </div>

    <?php 
    endwhile; // end group loops
endif; // end if there are groups 

 ?>

</div>
     <div class="directory">
        <div class="group">

        <?php

        // new variables
        $prev ='';      

        //Start a while loop to process all the rows
        while ($row = mysql_fetch_assoc ($result_set))
        {

            $Heading = $row['Heading'];
            $Name = $row['Name'];
            $Address =  $row['Address'];
            $City =  $row['City'];
            $Phone =  $row['Phone'];

            // Unique header code
            if($Heading != $prev) 
            {
        ?>
                <div class="row" style="background-color:#666" >
                    <div class="col-md-12"><p><?php echo $Heading; ?></p></div>
                </div>
        <?php
            }?>
       <div class="row">
                    <div class="col-md-4">
                        <p><strong><?php echo $Name; ?></strong><br><?php echo $Address; ?><br><?php echo $City; ?><br><?php echo $Phone; ?></p>
                    </div>
                </div>
        <?php
       $prev = $Heading;
 } //END WHILE
        ?>

        </div>
    </div>

Try this, as per my understanding of question, this solution will work.