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.
以下代码查询数据库,并且只有一列时效果很好。但是,我希望有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 个解决方案
#1
0
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.
问题是,作为列的标题必须与多个结果相同,并且您必须单独检查每个标题,拉出记录,并将它们显示在3列中。
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>
#2
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>
Try this, as per my understanding of question, this solution will work.
试试这个,根据我对问题的理解,这个解决方案会起作用。
#1
0
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.
问题是,作为列的标题必须与多个结果相同,并且您必须单独检查每个标题,拉出记录,并将它们显示在3列中。
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>
#2
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>
Try this, as per my understanding of question, this solution will work.
试试这个,根据我对问题的理解,这个解决方案会起作用。