Hi all.
I have a problem with retrieving data in modx and I really don’t know how to get out of it.
I have to build a simple catalog:
- the first page will show all products of all categories in a scrolling div, and a clickable list of categories too (children and children of children)
- if I click a category, the category page will open, and it shows, in the same scrolling div, all its products (children)
- then, if I click a product, I must have a detail page where I find again the same scrolling div, with all products of the same category (like before - children of the same parent)
My document tree is something similar to this one:
- home
- about
- products
- category 1
- product 1
- product 2
- category 2
- product 3
- product 4
- ...
- contacts
The biggest problem is the first page scrolling div, where I have to show all products, but divided into categories.
I thought that the best solution was to use two getResources calls: with the first I retrieve all children of first level (category 1, category 2) and then I have a second getResources call, inside the template chunk I use for the first call, to retrieve all children of the first level children. But it didn’t work for me.
So I thought that the second best solution was to create a custom query. I did it, it works outside modx, but it does not work at all in modx (NB: I need a custom template variable with the product image, too).
I’ve used this code:
<?php
$output = '';
$stmt = $modx->query("SELECT `pagetitle`,`id` FROM `[[+PREFIX]]site_content` WHERE `parent` = 4 AND `published`= 1");
if($stmt) {
$i = 0;
$output .= '<ul id="items"><!-- slider-items -->';
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$output .= '<li'; if($i==0){$output .= 'class="first"';} $output .= '>';
$output .= '<h2>'.$row['pagetitle'].'</h2>';
$stmt2 = $modx->query("SELECT `[[+PREFIX]]site_content`.`id`,`[[+PREFIX]]site_content`.`pagetitle`,`value` FROM `[[+PREFIX]]site_content` INNER JOIN (`[[+PREFIX]]site_tmplvar_templates` INNER JOIN (`[[+PREFIX]]site_tmplvar_contentvalues` INNER JOIN `[[+PREFIX]]site_tmplvars` ON `[[+PREFIX]]site_tmplvar_contentvalues`.tmplvarid=`[[+PREFIX]]site_tmplvars`.id) ON `[[+PREFIX]]site_tmplvar_templates`.tmplvarid=`[[+PREFIX]]site_tmplvar_contentvalues`.tmplvarid) ON `[[+PREFIX]]site_content`.template=`[[+PREFIX]]site_tmplvar_templates`.templateid WHERE `[[+PREFIX]]site_tmplvars`.`name`='img_confezione' AND `[[+PREFIX]]site_content`.`published`= 1 AND `[[+PREFIX]]site_content`.`parent`='".$row['id']."' AND `modx_site_tmplvar_contentvalues`.`contentid`=`modx_site_content`.`id`");
if($stmt2) {
$ii = 0;
$output .= '<ul>';
while ($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)) {
$output .= '<li'; if($ii==0){$output .= 'class="first"';} $output .= '>';
$output .= '<h4>'.$row1['pagetitle'].'</h4><a href="[[~'.$row1['id'].']]"><img src="'.$row1['value'].'" alt="'.$row1['pagetitle'].'" width="205" height="280" border="0" /></a>';
$output .= '</li>';
$ii++;
}
$output .= '</ul>';
}
$output .= '</li>';
$i++;
}
$output .= '</ul><!-- /slider-items -->';
}
return $output;
What is wrong?
Thank you very much!
(Sorry for my written english

)