简体   繁体   English

查询以显示类别和子类别

[英]Query to Show Categories and Subcategories

I want to show CATEGORIES, and then show SUBCATEGORIES on mouse over. 我想显示“类别”,然后在鼠标悬停时显示“ SUBCATEGORIES”。 I have 2 tables: 我有2张桌子:

create table categorias (
`idCategoria` int NOT NULL PRIMARY KEY,
`nomCategoria` varchar (55) NOT NULL,
`desCategoria` varchar (255),
`tipoCategoria` int (2),
`fotoCategoria` varchar (55),
`metaTiCategoria` varchar (255),
`metaDesCategoria` varchar (255),
`keywordsCategoria` varchar(100)
) 

and

CREATE TABLE IF NOT EXISTS `subcategoria` (
  `idSubcate` int(3) NOT NULL AUTO_INCREMENT,
  `nomSubcate` varchar(55) NOT NULL,
  `fotoSubcate` varchar(255) DEFAULT NULL,
  `metaTiSubcate` varchar(255) DEFAULT NULL,
  `metaDesSubcate` varchar(255) DEFAULT NULL,
  `keywordsSubcate` varchar(255) DEFAULT NULL,
  `tipoSubcate` int(1) DEFAULT NULL,
  `cateSubcate` int(3) DEFAULT NULL,
  PRIMARY KEY (`idSubcate`))

The PHP code: PHP代码:

 <ul class="grid cs-style-4">
    <?
      $cate = 'communit_new_site_2016.categorias';
      $qry = ("SELECT * 
        from $cate
        where $cate.tipoCategoria = 1 or $cate.tipoCategoria=3
        ORDER BY $cate.nomCategoria
       ");
      $rst = mysql_query($qry);
      $tN = mysql_num_rows($rst);
      $cN = 1;
      ?>
      <?
          while($categoria = mysql_fetch_assoc($rst)) {


      ?>
      <li class="col-xs-12 col-sm-12 col-md-3">
      <div class="col-xs-12 col-sm-12 col-md-12 text-center">
      <figure>
        <div><img src="<?= $categoria['fotoCategoria'] ?>" alt="<?= $categoria['nomCategoria'] ?>"></div>
        <figcaption class="subcate">
         <?
            $sub = 'communit_new_site_2016.subcategoria';
            $catein = 'communit_new_site_2016.categorias';
            $qry1 = ("SELECT nomSubcate 
              from $sub

              ORDER BY $sub.nomSubcate
             ");
            $rst1 = mysql_query($qry1);
            $tN1 = mysql_num_rows($rst1);
            $cN1 = 1;
      ?>
      <?
          while($subcate = mysql_fetch_assoc($rst1)) {


      ?>
         <span> <?= $subcate['nomSubcate']?></span>
          <?

      if($cN1 != $tN1) {
      ?>

      <?
          }
      $cN1++;
      }
      ?>     
        </figcaption>
      </figure>
      </div>
      <div class="col-xs-12 col-sm-12 col-md-12 text-center">
              <a href=""><span class="caption"><?=$categoria['nomCategoria']?></span></a>
            </div>
    </li>
      <?

      if($cN != $tN) {
      ?>

      <?
          }
      $cN++;
      }
  ?>    
  </ul>

The webpage is www.communita.com.mx/interior.php. 该网页是www.communita.com.mx/interior.php。 Also, there are two types: indoor (interior) and outdoor (exterior). 另外,有两种类型:室内(内部)和室外(外部)。 That's why I use tipoCategoria = 1 (indoor) and 3 for both indoor and outdoor. 这就是为什么我在室内和室外都使用tipoCategoria = 1(室内)和3的原因。

Thanks in advance. 提前致谢。

Here's how I would do this using a left join. 这就是我使用左联接的方法。 I haven't tested this because I don't have your database, but it should show you the general idea of my approach: 我没有进行测试,因为我没有您的数据库,但是它应该向您展示我的方法的总体思路:

First, execute the query and read the rows into a multidimensional array grouped by category id: 首先,执行查询并将行读入按类别ID分组的多维数组:

<?php
$cate = 'communit_new_site_2016.categorias';
$sub = 'communit_new_site_2016.subcategoria';
$qry = ("SELECT idCategoria, nomCategoria, fotoCategoria, idSubcate, nomSubcate
         FROM $cate c LEFT JOIN $sub s ON c.idCategoria = s.cateSubcate
         WHERE tipoCategoria = 1 OR tipoCategoria = 3 ORDER BY nomCategoria, nomSubcate");
$rst = mysql_query($qry);
while ($row = mysql_fetch_assoc($rst)) {
    // use the category id as a key here to group the array

    // these two values will just overwrite with the same thing if they're repeated
    $categorias[$row['idCategoria']]['nomCategoria'] = $row['nomCategoria'];
    $categorias[$row['idCategoria']]['fotoCategoria'] = $row['fotoCategoria'];

    // the subcategory will be added to an inner array
    $categorias[$row['idCategoria']]['subcategoria'][$row['idSubcate']] = $row['nomSubcate'];
}?>

After you have an array in that structure, it will be easier to output the HTML you are trying to get. 在具有该结构的数组之后,将更容易输出要获取的HTML。

<ul class="grid cs-style-4">
<?php foreach ($categorias as $categoria): ?>
    <li class="col-xs-12 col-sm-12 col-md-3">
        <div class="col-xs-12 col-sm-12 col-md-12 text-center">
            <figure>
                <div>
                     <img src="<?= $categoria['fotoCategoria'] ?>" 
                          alt="<?= $categoria['nomCategoria'] ?>">
                </div>
                <figcaption class="subcate">
                    <?php foreach ($categoria['subcategoria'] as $id => $subcategoria): ?>
                        <span><?= "$id: $subcategoria" ?></span>
                    <?php endforeach ?>
                </figcaption>
            </figure>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12 text-center">
            <a href=""><span class="caption"><?= $categoria['nomCategoria']?></span></a>
        </div>
    </li>
<?php endforeach ?>
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM