按类别

时间:2015-11-22 16:31:31

标签: php mysql

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

图像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 ();
?>

1 个答案:

答案 0 :(得分:1)

首先,更改您的查询,以便按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即ie。 $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>