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

时间:2014-01-29 21:38:56

标签: php mysql loops

以下代码查询数据库,并且只有一列时效果很好。但是,我希望有3列数据。代码正确生成标题并扩展页面的宽度,但在尝试循环记录时,它将按列数复制。我非常接近,但需要一些指导。

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

2 个答案:

答案 0 :(得分:0)

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

试试这个, 根据我对问题的理解,这个解决方案将起作用。

答案 1 :(得分:0)

问题是Heading作为一个列必须对于几个结果是相同的,并且您必须单独检查每个标题,拉出记录,并将它们显示在3列中。

这是代码:

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