List products for each category

7 posts by 3 authors in: Forums > CMS Builder
Last Post: September 28, 2012   (RSS)

By Mohaukla - September 27, 2012 - edited: September 28, 2012

I have created a sitemap that should list each category and the products for that category. The categories list correctly but the products list all products in each category.


<div class="left">
<ul><?php foreach ($categoriesRecords as $category): ?>
<li>
<a href = "category.php?categories=<?php echo $category['num'];?>"><?php echo $category['title'];?></a>
<ul><?php foreach ($productsRecords as $record): ?>
<li>
<a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['name']) ?></a>
</li>
<?php endforeach ?>
</ul>
</li>
<?php endforeach ?>
</ul>
</div>



Thanks in advance,
Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"

Re: [justritedesign] List products for each category

By Mohaukla - September 28, 2012

Forgot my php top code:
// load records from 'categories'
list($categoriesRecords, $categoriesMetaData) = getRecords(array(
'tableName' => 'categories',
'loadUploads' => false,
'allowSearch' => false,
));

// load records from 'products'
list($productsRecords, $productsMetaData) = getRecords(array(
'tableName' => 'products',
'loadUploads' => false,
'allowSearch' => false,
));

$categories = mysql_select ('categories');
//make the array key the 'num' field
$categoriesSorted = array_groupBy($categories,'num');

Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"

Re: [justritedesign] List products for each category

By gregThomas - September 28, 2012

Hi Michael,

From the code you've posted it looks as if your not filtering the $productRecords in anyway.

I've just looked at this post (http://www.interactivetools.com/forum/gforum.cgi?post=95074#95074) to see how your products are linked to the categories, and I think this code below should do the trick:

<div class="left">
<ul><?php foreach ($categoriesRecords as $category): ?>
<li>
<a href="<?php echo $category['_link'] ?>"><?php echo htmlencode($category['title']) ?></a>
<ul><?php foreach ($productsRecords as $record): ?>
<?php if(in_array($category['num'], $subRecord[categories:values])): ?>
<li>
<a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['name']) ?></a>
</li>
<?php endif ?>
<?php endforeach ?>
</ul>
</li>
<?php endforeach ?>
</ul>
</div>




Let me know if this doesn't work.

Thanks!
Greg Thomas







PHP Programmer - interactivetools.com

Re: [justritedesign] List products for each category

By gregThomas - September 28, 2012

Sorry about that, I forgot to rename a variable after testing the code on my machine. You should just need to rename the variable highlighted in blue:

<div class="left">
<ul><?php foreach ($categoriesRecords as $category): ?>
<li>
<a href="<?php echo $category['_link'] ?>"><?php echo htmlencode($category['title']) ?></a>
<ul><?php foreach ($productsRecords as $record): ?>
<?php if(in_array($category['num'], $record['categories:values'])): ?>
<li>
<a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['name']) ?></a>
</li>
<?php endif ?>
<?php endforeach ?>
</ul>
</li>
<?php endforeach ?>
</ul>
</div>


Hopefully it should work this time!
Greg Thomas







PHP Programmer - interactivetools.com

Re: [greg] List products for each category

By Mohaukla - September 28, 2012

Sorry that did not work

Maybe I am not setting it up right...

<?php if(in_array($category['num'], $record['name'])): ?>
Michael Moyers



Owner of Just Rite Design Inc. A growing network of professionals in web design, graphic design, flash development, programming, and audio & video productions.



"Due to budget constraints, the Light at the end of the tunnel will be temporarily out!"

Re: [justritedesign] List products for each category

By Jason - September 28, 2012

Hi Michael,

Try this:

<?php if(in_array($category['num'], $record['categories:values'])): ?>

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/