Category records

4 posts by 2 authors in: Forums > CMS Builder
Last Post: May 2, 2011   (RSS)

By Ginslinger - April 28, 2011 - edited: April 28, 2011

Have a category page that list 15-16 categories with a link to all records under each category. How would I go about listing a record count for each category that is listed within the category page?

You can see how I need this to display from the link below. Been working with Ross through consulting but have run out of money for this project. [:(]

http://diecast-pub.com/model_list/categories.php

Thanks

Re: [Ginslinger] Category records

By Jason - April 29, 2011

Hi,

There are a couple of ways of doing this. Probably the easiest would be to use the function mysql_select_count_from(). This function returns the number of records from a table that meet a given WHERE clause.

Please note that the example below is making a number of assumptions:
1) Your list of categories is in a variable called $categoryRecords.
2)You're storing your listings in a section called "products"
3) Your products section is using a field called "category" to store it's category value
4) The category field used the "num" from the category section as it's value.

Given these assumptions, your code could look like this:

<?php foreach ($categoryRecords as $category): ?>
<?php
$where = "category = '".$category['num']."'";
$recordCount = mysql_select_count_from('products', $where);
?>
<?php endforeach ?>


You then have a variable called $recordCount that you can output where ever you like, like this:

<?php echo $recordCount;?>

Hope this helps get you started.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

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

Re: [Ginslinger] Category records

By Jason - May 2, 2011

Hi,

You can put this code right into your existing foreach loop.

Try this (changes in red):

<?php foreach ($categoryRecords as $categoryRecord): ?>

<?php
$where = "category = '".$categoryRecord['num']."'";
$recordCount = mysql_select_count_from('products', $where);
?>

<tr>
<td>
<a href="<?php echo $categoryRecord['_link']; ?>">
<b> <?php echo $categoryRecord['breadcrumb']; ?></b>
</a>
</td>
<td>
<?php echo $recordCount; ?>
</td>
<td align="left">
<?php echo date("M jS, Y", strtotime($categoryRecord['updatedDate'])) ?>


</td>
</tr>

<?php endforeach ?>


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/