Category records

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

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: [Jason] Category records

By Ginslinger - April 29, 2011

Hi Jason and thank you for the reply. Having a few issues getting this to work. Might be I don't totally don't understand where to put the (for each statements) within a table structure.

Entry below shows how code currently appears on my page. As you can see there is already statements that Ross created from consulting.

Your example code is correct except products would be called articles instead.

I tried entering the code a few times but get a screwed up page with no counts so I removed it. Code below is how it looks presently. Forgive the harsh HTML code.


<?php
require_once "modelsAdmin/lib/viewer_functions.php";

list($categoryRecords) = getCategories(array(
'tableName' => 'categories', // REQUIRED
'categoryFormat' => 'onelevel', // showall, onelevel, twolevel, breadcrumb
));


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Facts & Reviews</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<link rel="stylesheet" href="http://diecast-pub.com/model_list/styles/facts.css" type="text/css" />

</head>
<body bgcolor="#c2c2c2">


<table width="960" border="1" cellpadding="6" bgcolor="white" align="center" cellspacing="3">
<tr>
<td colspan="2" bgcolor="#2c2c2c">
<table width="100%">
<tr>
<td>
<img src="http://www.diecast-pub.com/images/gallery_logo.jpg">
</td>
<td align="right">
<iframe src="http://www.diecast-pub.com/adserver//image.php?size_id=29" width="468" height="60" scrolling="no" frameborder="0" marginheight="0" marginwidth="0" vspace="0" hspace="0"><br><font size=-1></iframe>

</td>
</tr>
</table>


</td>
</tr>

<TR>
<TD COLSPAN="2">
<table cellpadding="4" cellspacing="1" width="100%" class="tableborders">
<tr>
<td class="menubar" align="left">Site Links: |<span class="medium"><a href="/index2.php">Front Page</a> | <a href="/forums2/">Forums</a> | <a href="/data/facts_reviews/">Facts & Reviews</a> | <a href="/classifieds/">Classifieds</a></span>
</td>
</tr>
</table>
</tr>
<tr>
<td colspan="3" valign="top">

<table width="100%" class="tableborders" cellpadding="4" cellspacing="1">
<tr>
<td class="menubar">

You are here -><a href="categories.php">&nbsp; Directory Index</a>
</td>
</tr>
</table>

</td>
</tr>

<TR>
<td valign="top">
<table border="1" width="100%" cellpadding="5">

<tr bgcolor="#336699"><!-- Row 1 -->
<td width="65%"><b><font color="#FFFFFF">Category</font></b></td><!-- Col 1 -->
<td width="15%"><b><font color="#FFFFFF">Products</font></b></td><!-- Col 2 -->
<td width="20%"><b><font color="#FFFFFF">Last Updated</font></b></td><!-- Col 3 -->
</tr>

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


</td>
</tr>

<?php endforeach ?>


</table>

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/