2014-01-29 83 views
1

以下代碼查詢數據庫,並且在只有一列時工作良好。不過,我想要有3列數據。代碼正確地生成一個標題並且擴展了頁面的寬度,但是當試圖循環記錄時,它將重複列數。我非常接近,但需要一些指導。while循環按列數重複記錄

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

'如果($標題= $分組)' - 你可能要使用''==那裏。 – andrewsi

+0

@andrewsi你應該發佈這個答案! –

+0

@andrewsi不要緊,'$ Heading'總是'==''$ prev',看看他上面的代碼...'$ prev = $ Heading;' – cmorrissey

回答

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

試試這個, 按照我的問題的理解,這種解決方案會奏效。

+0

這只是創建一列數據,我試圖得到3列。我正在使用localhost,但如果沒有簡單的解決方案,我會將其移至服務器以供查看。 – Brian

+0

您是在尋找具有相同數據的3行或具有不同數據的3行,此解決方案將創建具有不同數據的3行 –

+0

我再次檢查並且您提供的代碼創建了一個長列,右邊有很多空白區域。 – Brian