2

我正在一个网站上显示待售域名,这些域名已全部归类为大约 35 个主要类别之一。每个待售域名也属于该主要类别的子类别。每个主要类别有 1 到 10 个子类别。

目前,该站点仅列出所选主要类别中数据库中的所有域名。(它实际上显示了所选主类别的第一个子类别中的所有名称,然后是该主类别中的所有其余子类别域名)。

数据库中只有一张表。它有 4 列,大约 10,000 行长,大约有 35 个主要类别和每个主要类别中的几个子类别。除了id(我认为)之外,它都是文本。

这是表格的一部分:

Id   MainCat  SubCat           DomainName
1    Art      Art Companies    ArtCompanies.com
2    Beauty   Beuauty Care     CareBeauty.com
3    Art      Art Galleries    ArtInAmerica.com
4    Art      Art Companies    ArtInChicago.com
5    Art      Art Galleries    ArtMuseumsInLA.com
6    Beauty   Beauty Products  BeautyIdeas.com
7    Art      Artists          ArtMuseumsNY.com
8    Business Trademarks       TrademarksUSA.com

以下是 php/mysql 代码的简化版本,该代码目前检索所选主类别中的所有域名以及该主类别中的第一个子类别。这是“键盘”图像下方文本顶部的代码:

$res_sub = $mysqliConnection->query("SELECT * FROM {$table} WHERE MainCategory = '$cellMainCat' AND SubCategory = '$cellSubCat' ORDER BY SubCategory ASC") or die(mysqli_error());
echo "You are viewing <div class='catFormat'>$cellSubCat</div> in <div class='catFormat'>$cellMainCat</div>";

/* determine number of rows in result set */
$total_number_of_rows_returned = $res_sub->num_rows;
$number_of_rows_remaining = 0;

while ($number_of_rows_remaining < ($total_number_of_rows_returned/2) && (boolean) ($row = mysqli_fetch_object($res_sub))) 
{
$number_of_rows_remaining++;  // increment 
    echo $row->DomainName;
}
/* now doing to do the RIGHT SIDE MIDDLE div and put the right side results in.  We don't need to do the count half thing cause there should only be half left */

while ($row = mysqli_fetch_object($res_sub)) 
{
echo $row->DomainName;
    echo "<br>";
}

这是一个指向该网站的链接,就像现在一样:http:
//bestmarketingnames.com/Tbanneredit_v40.php

我的问题: 我想修改网站,以便当用户单击按钮选择主类别时,页面显示该主类别中的所有域名,但按子类别细分。

因此,例如,如果该人单击“艺术”的主类别按钮,页面将显示:

You have chosen the Main Category Art which has 8 Subcategories.  

Subcategory 1 of 8 is:  Art Galleries.  The domain names for sale for 
this subcategory are:
             myartgalleries.com
             bigartgalleries.com
             superartgaleries.com

Subcategory 2 of 8 is:  Art Companies.  The domain names for sale for 
this subcategory are:
            bigartcompanies.com
            superartcompanies.com

       [etc. - end example]

我想知道的是使用什么代码来循环遍历子类别并一次显示一个子类别,如上例所示。如果我可以在不查询每个子类别的情况下逃脱,那将是很棒的,但我对 php/mysql 的了解还不够,不知道这是否可能。我对数组等没问题。

任何帮助,将不胜感激。

谢谢。

4

2 回答 2

0
SELECT name,category,subcategory FROM table WHERE category ='art' GROUP BY subcategory 

..应该这样做。

于 2013-05-31T07:56:06.650 回答
0

您可以使用GROUP_CONCAT()一次性抓取所有以逗号分隔并按子类别分组的域名。

SELECT subcat,
       GROUP_CONCAT(domainname) domainname
  FROM domainnames
 WHERE maincat = 'Art'
 GROUP BY subcat

输出:

|        SUBCAT |                          DOMAINNAME |
-------------------------------------------------------
| Art Companies |   ArtCompanies.com,ArtInChicago.com |
| Art Galleries | ArtInAmerica.com,ArtMuseumsInLA.com |
|       Artists |                    ArtMuseumsNY.com |

这是SQLFiddle演示

并进行所有操作以像这样在 php 中构建 UI

$maincat = "Art";

$db  = new mysqli('localhost', 'user', 'password', 'dbname') or die('Cannot connect');
$sql = "SELECT subcat,
               GROUP_CONCAT(domainname) domainname
          FROM domainnames
         WHERE maincat = ?
         GROUP BY subcat";  

$query = $db->prepare($sql) or die('Unable to prepare: ' . $db->error);
$query->bind_param("s", $maincat);
$query->execute();
$query->bind_result($s, $d);

$subcategories = array();
while ($query->fetch()) {
    $subcategories[$s] = explode(',', $d);
}
$db->close();

$sub_count = count($subcategories);
echo "<p>You have chosen the Main Category $maincat which has $sub_count Subcategories.</p>";

$i = 1;
foreach ($subcategories as $sub_name => $domainnames) {
    echo "<p>Subcategory $i of $sub_count is: $sub_name.<br>";
    echo "The domain names for sale for this subcategory are:</p>";
    echo "<ul>";
    foreach ($domainnames as $domain_name) {
        echo "<li>$domain_name</li>";
    }
    echo "</ul>";
    $i++;
}
于 2013-05-31T21:28:05.650 回答