以下代碼查詢數據庫,並且在只有一列時工作良好。不過,我想要有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>
'如果($標題= $分組)' - 你可能要使用''==那裏。 – andrewsi
@andrewsi你應該發佈這個答案! –
@andrewsi不要緊,'$ Heading'總是'==''$ prev',看看他上面的代碼...'$ prev = $ Heading;' – cmorrissey