goeknax goeknax - 4 months ago 9
SQL Question

Query to Show Categories and Subcategories

I want to show CATEGORIES, and then show SUBCATEGORIES on mouse over. I have 2 tables:

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:

<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.
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.

Thanks in advance.

Answer

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:

<?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.

<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>
Comments