如何在HTML中显示与PHP行中相同值的行?

huangapple go评论85阅读模式
英文:

How to display the php row with the same value as one in HTML?

问题

从数据库中获取多个具有相同值的数据。如何构建可以作为子类别工作的数据以合并所有具有相同名称的行?

网站应该只显示一个 "Pampanga"。这应该作为一个子类别工作。

以下是此特定页面的代码:

<section class="page-section bg-dark" id="home">
	<div class="container">
		<h2 class="text-center">旅游套餐</h2>
		<div class="d-flex w-100 justify-content-center">
			<hr class="border-warning" style="border:3px solid" width="15%">
		</div>
		<div class="d-flex w-100">
			<?php
			$packages = $conn->query("SELECT * FROM `packages` order by rand() ");
				while($row = $packages->fetch_assoc() ):
					$cover='';
					if(is_dir(base_app.'uploads/package_'.$row['id'])){
						$img = scandir(base_app.'uploads/package_'.$row['id']);
						$k = array_search('.',$img);
						if($k !== false)
							unset($img[$k]);
						$k = array_search('..',$img);
						if($k !== false)
							unset($img[$k]);
						$cover = isset($img[2]) ? 'uploads/package_'.$row['id'].'/'.$img[2] : ""
					}
					$row['description'] = strip_tags(stripslashes(html_entity_decode($row['description'])));
			?>
			<div class="card w-100 rounded-0">
				<img class="card-img-top" src="<?php echo validate_image($cover) ?>" alt="<?php echo $row['title'] ?>" height="200rem" style="object-fit:cover">
				<div class="card-body">
				<h5 class="card-title truncate-1"><?php echo $row['title'] ?></h5>
				<p class="card-text truncate"><?php echo $row['description'] ?></p>
				<div class="w-100 d-flex justify-content-end">
					<a href="./?page=packages&id=<?php echo md5($row['id']) ?>" class="btn btn-sm btn-flat btn-warning">查看套餐 <i class="fa fa-arrow-right"></i></a>
				</div>
				</div>
			</div>
			<?php endwhile; ?>
		</div>
		<div class="d-flex w-100 justify-content-end">
			<a href="./?page=packages" class="btn btn-flat btn-warning mr-4">探索套餐 <i class="fa fa-arrow-right"></i></a>
		</div>
	</div>
</section>
英文:

Multiple data is fetched from the database with the same value. How do I construct this that will work as a sub category. How can I combine all the row with the same name into one?

如何在HTML中显示与PHP行中相同值的行?

如何在HTML中显示与PHP行中相同值的行?

The site should display only one pampanga. This should work as a sub category.

Here is the code for this specific page.

<section class="page-section bg-dark" id="home">
<div class="container">
<h2 class="text-center">Tour Packages</h2>
<div class="d-flex w-100 justify-content-center">
<hr class="border-warning" style="border:3px solid" width="15%">
</div>
<div class="d-flex w-100">
<?php
$packages = $conn->query("SELECT * FROM `packages` order by rand() ");
while($row = $packages->fetch_assoc() ):
$cover='';
if(is_dir(base_app.'uploads/package_'.$row['id'])){
$img = scandir(base_app.'uploads/package_'.$row['id']);
$k = array_search('.',$img);
if($k !== false)
unset($img[$k]);
$k = array_search('..',$img);
if($k !== false)
unset($img[$k]);
$cover = isset($img[2]) ? 'uploads/package_'.$row['id'].'/'.$img[2] : "";
}
$row['description'] = strip_tags(stripslashes(html_entity_decode($row['description'])));
?>
<div class="card w-100 rounded-0">
<img class="card-img-top" src="<?php echo validate_image($cover) ?>" alt="<?php echo $row['title'] ?>" height="200rem" style="object-fit:cover">
<div class="card-body">
<h5 class="card-title truncate-1"><?php echo $row['title'] ?></h5>
<p class="card-text truncate"><?php echo $row['description'] ?></p>
<div class="w-100 d-flex justify-content-end">
<a href="./?page=packages&id=<?php echo md5($row['id']) ?>" class="btn btn-sm btn-flat btn-warning">View Package <i class="fa fa-arrow-right"></i></a>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
<div class="d-flex w-100 justify-content-end">
<a href="./?page=packages" class="btn btn-flat btn-warning mr-4">Explore Package <i class="fa fa-arrow-right"></i></a>
</div>
</div>
</section>

答案1

得分: 2

如果您只想获取每个旅游地点的一个实例,您可以在请求中使用GROUP BY语句。

SELECT tour_location FROM `your_table_name` GROUP BY tour_location;

更多信息请参考 https://sql.sh/cours/group-by

另一个解决方法可以是使用SQL的DISTINCT。

SELECT DISTINCT tour_location FROM `your_table_name`
英文:

If you want only one instance of each tour location you car use a group by statement in your request.

SELECT tour_location FROM `your_table_name` GROUP BY tour_location;

See more on https://sql.sh/cours/group-by

Another solution could be the use of sql DISTINCT.

SELECT DISTINCT tour_location FROM `your_table_name`

huangapple
  • 本文由 发表于 2023年4月4日 14:49:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定