gpt4 book ai didi

php - 按类别对 MySQL 输出进行分组

转载 作者:行者123 更新时间:2023-11-28 23:44:19 25 4
gpt4 key购买 nike

我下面的代码连接到我的数据库并按字母顺序显示我的表格中的图像列表,例如

图片A、图片B、图片C等

相反,我想按字母顺序显示这些图像,并按与每个图像相关的类别进行组织,例如

类别 1图片A,图片B

第 2 类图 C

我还想在每组图像上方动态显示每个类别的标题。

我表中的列是:Link_ID、Link_Image、Link_Text、Link_URL、Link_Category

我在这里发现了一个类似的问题:Output MySQL list of records, grouped by category?但我不确定如何使用本例中给出的解决方案(或显示动态标题)。

    $db_host = 'XXXX';
$db_user = 'XXXX';
$db_pwd = 'XXXX';
$database = 'XXXX';
$table = 'home';

if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");

if (!mysql_select_db($database))
die("Can't select database");

// sending query
$result = mysql_query("SELECT * FROM {$table} ORDER BY Link_Text ASC");
if (!$result) {
die("Query to show fields from table failed");
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta name="robots" content="noindex">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Title</title>
<style type="text/css">
.linkcontent {
margin: auto;
width: 720px;
height: 714px;
}
.linkcontent a img {
float: left;
margin: 2px;
border-top-style: none;
border-right-style: none;
border-bottom-style: none;
border-left-style: none;
}
.linkcontent a img:hover {
float: left;
border: 2px solid #999;
margin: 0px;
}
.linkcontent_title {
font-family: Arial, Helvetica, sans-serif;
font-size: 24px;
font-weight: normal;
color: #999;
text-align:left;
text-decoration: none;
padding-bottom: 5px;
float: left;
height: 24px;
width: 100%;
clear:right;}
</style>
</head>
<body><div class="linkcontent"><div class="linkcontent_title">Category Title</div>
<?php
while($row = mysql_fetch_array($result))
{?>
<a href="<?php echo $row['Link_URL']?>" ><img src="<?php echo $row['Link_Image']?>" alt="<?php echo $row['Link_Text']?>" width="175" height="175" border="0" /></a>
<?php }?>
</div>
</body>
</html>
<?php
mysql_close ();
?>

最佳答案

首先,更改您的查询,以便按 Link_Category 对结果进行排序, 然后通过 Link_Text .

SELECT * FROM {$table} ORDER BY Link_Category ASC, Link_Text ASC

然后当你循环你的结果时,把你的<div class="linkcontent_title">Category Title</div>在你的循环中。更改 Link_Category当类别发生变化时,只需使用一个简单的 php var 来存储当前类别,即。 $currentCategory

<body>
<?php

$currentCategory = ""; //use this to change the Category Title

while($row = mysql_fetch_array($result))
{
if($row['Link_Category'] != $currentCategory) // was wrong on the first draft
{
if($currentCategory != "") //if this is not the 1st, close the last <div>
{ ?>
</div>
<?php } // ends not 1st Category Title if{} ?>
<div class="linkcontent">
<div class="linkcontent_title"><?php echo $row['Link_Category']?></div>
<?php $currentCategory = $row['Link_Category']; //Set the Current Category
} // ends the Category Title if{} ?>
<a href="<?php echo $row['Link_URL']?>" >
<img src="<?php echo $row['Link_Image']?>" alt="<?php echo $row['Link_Text']?>" width="175" height="175" border="0" />
</a>
<?php }?>
</div>
</body>

关于php - 按类别对 MySQL 输出进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33857194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com