Hello how are you? I bother you with a question?
I set out to group products according to the subcategory (id_subcate) but when accessing the Category (id_cate)
I start from the base, as @amenadiel commented and made me realize,
Your query only uses the ID of the main category
that I take the url via $_GET
and that I am always positioned in the Category productos.php?id_cate=7
there I access a list of all the related products but without taxonomic order.
Categories
tbl_categorias id_cate id_catemaster nombre
Products
tbl_productos id_prod id_cate id_subcate id_subnivcate nombre precio
Here the id_subcate is an INT eg. two
Subcategories
tbl_subcategorías id_subcate, id_cate, nombre
Here the id_subcate is a VARCHAR eg. torso garments
I think I should relate the tables with JOIN and then Echo the name of the subcategory in VARCHAR.-
The following is the schematic I hope to achieve
Category Title (I already have this)
Subcategory Title
Products that correspond to the subcategory
Then a line break<br>
Title of another subcategory
Products that correspond to the subcategory
Then a line break <br>
*
Here is the code where I am going that only shows the products without order
<?php
$id_prod = $HttpVars->TraerGet('id_prod');
$id_subcate = $HttpVars->TraerGet('id_subcate');
$id_cate = $HttpVars->TraerGet('id_cate');
$id_subnivcate = $HttpVars->TraerGet('id_subnivcate');
if($id_cate != "") {
//sql para titulo de categoria
$sqltit = "SELECT nombre FROM tbl_categorias WHERE id_cate = ".intval($id_cate);
//sql para los productos
$sql = "SELECT P.id_prod, P.nombre, P.filech, P.precio, P.dcorta, P.publicado, P.id_cate, P.id_subcate ";
$sql.= "FROM tbl_productos as P ";
$sql.= "WHERE P.id_cate = ".intval($id_cate)." AND P.publicado = 1 and P.borrado = 0 ";
$sql.= "ORDER BY P.id_subcate";
}
$resulttit = $db->Query($sqltit,$connection);
$myrowtit = mysqli_fetch_array($resulttit, MYSQLI_BOTH);
?>
<div class="titcate"><p><?php echo $myrowtit["nombre"]; ?></p></div>
<div class="clear"></div>
<?php
$result = $db->Query($sql,$connection);
$totreg = mysqli_num_rows($result);
if ($db->CantidadFilas($result) > 0){
$cont=0;
while ($myrow = mysqli_fetch_array($result, MYSQLI_BOTH)) {
$precio = $myrow["precio"];
$nombre = $myrow["nombre"];
$cont++;
$imagen = $myrow["filech"];
$id_prod = $myrow["id_prod"];
if($imagen == "") {
$imagen = "noimagen.gif";}
?>
<div class="productoshome">
<p id="tit_prod"><? echo $nombre;?> </p>
<a title="<? echo $nombre ?>" href="<? echo "producto.php?id_prod=$id_prod/" ?>"></a>
<img src="timthumb.php?src=imgprod/<? echo $myrow['filech'] ?>&h=178&w=188&zc=1" alt="<? echo $nombre ?>" title="<? echo $nombre ?>" />
<p id="precio_prods">$ <? echo $precio ?></p>
</div>
<?php
}
}
else {
echo "<p>No hay productos disponibles para esta categoría. <a href='index.php'>Haga click aquí para seguir navegando</a></p>";
}
?>
This for now, I've been trying to find the right logic for several days now... thanks
Your query only uses the ID of the main category, so the title you put corresponds to one of the child subcategories of the category
id_cat
. Then you list the products whose parent category isid_cat
. At no time are you usingid_subcate
. I also do not see a JOIN where you try to matchid_subcate
as the question statement says.Let's suppose:
And that your products table looks like this:
Therefore, if you wanted to list,
prendas torso
you would pass subcategory 2 to it. In that case, your code should display something like:And the code itself would look like this
(Example: https://examples.ffflabs.com/listar_productos.php?id_subcate=2 try subcategories 1 (no results) 3, 4 and 5) )
If you pay attention:
NOTICE: undefined index id_subcate
.EDIT
Since we now have the main category table, it seems appropriate to indicate that
id_subcate
in the subcategory table and the product table it should be a number.The value "torso garments" goes in the "name" field of the subcategory.
With this, the subcategory table is practically a step table between category and product. (also containing the name of the subcategory).
The products table doesn't need a subcategory field, by the third normal way you can always get to the category table by going through the subcategory one.
To recap, the category table looks like this:
The subcategory table (where I converted
id_subcat
to integer) looks like this:And the products :
The schema looks like this:
The query should be:
(as you already sanitized
$id_cate
we are going to skip the sentence prepared)The result looks like this:
And the code would be: (you will accommodate your html and its styles)
You can try https://examples.ffflabs.com/listar_productos2.php?id_cate=1
and https://examples.ffflabs.com/listar_products2.php?id_cate=2