[英]trying to display all the different username in a table if multiple only display it once
[英]Display state header only once, and still display all addresses in that state
我正在寻找一种显示方式:
<h1>State1</h1>
<h2>City1</h2>
<p>Address1</p>
***if next address is in the same state:*
<h2>City2</h2>
<p>Address2</p>
<hr>
**if next address is NOT in the same state
<h1>State2</h1>
<h2>City3</h2>
<p>Address3</p>
我正在使用:
<?php
$query = "SELECT * FROM places";
$select_all_places = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc()){
$p_state = $row['p_state'];
$p_city = $row['p_city'];
$p_address = $row['p_address'];
?>
<h1><?php echo $p_state ?></h1>
<h2><?php echo $p_city ?></h2>
<p><?php echo $p_address ?></p>
<?php } ?>
在这里看看代码。 我在您的echo语句上添加了一些分号,并将查询变量添加到了mysqli_fetch_assoc方法中。 我还添加了一些评论,以帮助您了解我在这里所做的事情。
<?php
$query = "SELECT * FROM places";
$select_all_places = mysqli_query($connection, $query);
// This will be the state we are currently on. Start it as null.
$current_state = '';
while($row = mysqli_fetch_assoc($select_all_places)){
$p_state = $row['p_state'];
$p_city = $row['p_city'];
$p_address = $row['p_address'];
?>
<!-- Check if variable state is equal to current row's state. -->
<?php if($current_state != $p_state): ?>
<!-- Echo state if they aren't equal -->
<h1><?php echo $p_state; ?></h1>
<!-- Assign new current state -->
<?php $current_state = $p_state; ?>
<?php endif; ?>
<h2><?php echo $p_city; ?></h2>
<p><?php echo $p_address; ?></p>
<?php } ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.