Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按类别分组MySQL输出_Php_Mysql - Fatal编程技术网

Php 按类别分组MySQL输出

Php 按类别分组MySQL输出,php,mysql,Php,Mysql,下面的代码连接到我的数据库,并按字母顺序显示我表格中的图像列表,例如: 图像A、图像B、图像C等 相反,我希望按字母顺序显示这些图像,并按与每个图像相关的类别进行组织,例如 第一类 图A、图B 第2类 图C 我还想在每组图像上方动态显示每个类别的标题 我的表格中的列有:链接ID、链接图像、链接文本、链接URL、链接类别 我在这里发现了一个类似的问题:但我不确定如何使用本例中给出的解决方案或显示动态标题 $db_host = 'XXXX'; $db_user = 'XXXX'; $db_p

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

图像A、图像B、图像C等

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

第一类 图A、图B

第2类 图C

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

我的表格中的列有:链接ID、链接图像、链接文本、链接URL、链接类别

我在这里发现了一个类似的问题:但我不确定如何使用本例中给出的解决方案或显示动态标题

    $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_类别,只需使用存储当前类别的简单php变量,即$currentCategory


最简单的解决方案:使用两个查询,一个用于获取所有类别,然后在循环中创建第二个查询,用于获取给定类别的所有数据。为每个类别重复创建第二个查询。@maxhb这是一个错误的做法。这可以通过一个查询和一个循环来完成。没有理由这么做。@Sean你介意提供一个例子来说明如何做吗?这非常有效,除了每个图像都重复类别标题这一事实之外。是否可以只为每个类别显示一次?抱歉,我有两个错误。1我有if$row['Link_Text']!=$如果$row['Link\U Category'],则应为当前类别$currentCategory 2忘记添加$currentCategory=$row['Link_Category'];这会将当前类别重置为当前行的类别,这将阻止它对每个图像重复。
SELECT * FROM {$table} ORDER BY Link_Category ASC, Link_Text ASC
<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>