close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Query with advance Grouping

Thread begun 1/16/2017 7:58 am by danielsebas | Last modified 1/17/2017 10:56 am by Ray Borduin | 1264 views | 4 replies |

danielsebas

Query with advance Grouping

Hi, I want to do a query where I have a table for products and this table is linked to a category table so for 1 category I have 4 or 5 products, the tables are linked like this:

products.prodCATID = category.categoryID

|PRODUCT | CATEGORY |
-------------------------------------
|Product 1 | hammers
|Product 2 | hammers
|Product 3 | screwdrivers
|Product 4 | screwdrivers
|Product 5 | sockets

I want to do a recordset that when I repeat it in the page I can have a list like this (were the categories won't repeat with each product, but with the group of products:

Hammers
Product 1
Product 2

Screwdrivers
Product 3
Product 4

Sockets
Product 5

My recordset is build like this:

$products = new WA_MySQLi_RS("products",$conn_mysqli_base,3);
$products->setQuery("SELECT products.productsID, products.productsNOM, products.productsADD, products.productsSUBCAT, products.productsORD, categoy.categoryID, category.categoryNOM, subcategory.subcatID, subcategory.subcatNOM, subcategory.subcatCATID FROM (((products LEFT JOIN subcategory ON products.productsSUBCAT = subcategory.subcatID) LEFT JOIN category ON subcategory.subcatCATID = category.categoryID WHERE subcategory.subcatCATID = ? ORDER BY products.productsORD ASC");
$products->bindParam("i", "".$_GET['cat'] ."", "-1"); //colname_1
$products->execute();
Sign in to reply to this post

Ray BorduinWebAssist

You have two options. You would either need to hand code some IF statements and keep track of the previous category so that it doesn't get repeated. I've done it this way, but the code can be a little tricky.

The other option is to use two recordsets. Create a recordset for the categories alone and loop on those. Then create a second recordset and filter it based on a CategoryID and set the CategoryID to the value from the category recordset. Then add the product information into a repeat region on the page below the category repeat region.

Then you can manually copy and paste the product code and repeated region inside of the category repeat region.

Sign in to reply to this post
Did this help? Tips are appreciated...

danielsebas

Thank you Ray, I did this, but the information of the products is repeating the same for all categories:

<?php
$breadcrumbs = new WA_MySQLi_RS("breadcrumbs",$conn_mysqli_base,0);
$breadcrumbs->setQuery("SELECT * FROM subcategoria, categoria WHERE categoria.categoriaID = subcategoria.subcatCATID AND subcategoria.subcatCATID = ?");
$breadcrumbs->bindParam("i", "".$_GET['cat'] ."", "-1"); //colname
$breadcrumbs->execute();
?>
<?php
$fotos = new WA_MySQLi_RS("fotos",$conn_mysqli_base,0);
$fotos->setQuery("SELECT fotos.fotosID, fotos.fotosNOM, fotos.fotosADD, fotos.fotosSUBCAT, fotos.fotosLARGOID, fotos.fotosCABID, fotos.fotosORD, largo.largoID, largo.largoDES, categoria.categoriaID, categoria.categoriaNOM, cabeza.cabezaID, cabeza.cabezaDES, subcategoria.subcatID, subcategoria.subcatNOM, subcategoria.subcatCATID FROM (((fotos LEFT JOIN largo ON fotos.fotosLARGOID = largo.largoID) LEFT JOIN cabeza ON fotos.fotosCABID = cabeza.cabezaID) LEFT JOIN subcategoria ON fotos.fotosSUBCAT = subcategoria.subcatID) LEFT JOIN categoria ON subcategoria.subcatCATID = categoria.categoriaID WHERE subcategoria.subcatID = ? ORDER BY fotos.fotosORD ASC");
$fotos->bindParam("i", "".($breadcrumbs->getColumnVal("subcatID")) ."", "-1"); //colname_1
$fotos->execute();
?>



<?php
while(!$breadcrumbs->atEnd()) {
?>
<div class="row">
<div class="col-md-12">
<div class="subcat-titulo">
<h4><?php echo($breadcrumbs->getColumnVal("subcatNOM")); ?></h4>
</div>
</div>
<?php
while(!$fotos->atEnd()) {
?>
<div class="col-sm-6 col-md-4">
<div class="thumbnail"> <img class="img-responsive center-block" src="media/gallery/<?php echo($fotos->getColumnVal("categoriaID")); ?>/<?php echo($fotos->getColumnVal("subcatID")); ?>/<?php echo($fotos->getColumnVal("fotosADD")); ?>" alt=""/>
<div class="caption caption_prod"><?php echo($fotos->getColumnVal("fotosNOM")); ?></div>
</div>
</div>
<?php
$fotos->moveNext();
}
$fotos->moveFirst(); //return RS to first record
?>
</div>
<?php
$breadcrumbs->moveNext();
}
$breadcrumbs->moveFirst(); //return RS to first record
?>
Sign in to reply to this post

Ray BorduinWebAssist

Move the second recordset inside the repeated region so that it gets regenerated for each row (each category)

Sign in to reply to this post
Did this help? Tips are appreciated...

danielsebas

Thank you Ray, it worked perfectly!

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...