Display number of items on list page

5 posts by 3 authors in: Forums > CMS Builder
Last Post: April 6, 2011   (RSS)

By Toledoh - March 31, 2011

Hi Guys.

I'm sure I've seen this, but cant find the answer.

I've got a number of listings, and each listing has a "type" field that calls from another table - 'product_types'.

I then have a list view of the 'product_types' which in turn link, ie.


<?php foreach ($product_typesRecords as $record): ?>
<a href="results.php?type=<?php echo $record['num'] ?>"><?php echo $record['title'] ?></a>
<?php endforeach ?>


My question is, can I display the number of items within that link? ie.
<?php foreach ($product_typesRecords as $record): ?>
<a href="results.php?type=<?php echo $record['num'] ?>"><?php echo $record['title'] ?></a> [XXX Items for sale]
<?php endforeach ?>

Cheers,

Tim (toledoh.com.au)

Re: [Toledoh] Display number of items on list page

By gkornbluth - March 31, 2011 - edited: March 31, 2011

Hi Tim,

Here’s a recipe from the CMSB Cookbook http://www.thecmsbcookbook.com that might help.

WANT TO SHOW A RECORD COUNT ON YOUR WEB PAGE


You can use this to see the total number of records displayed:

<?php echo $your_table_nameMetaData['totalRecords']; ?>

There are a few other variables that can be used besides “totalRecords”. You can list them all with this code:

<xmp><?php print_r($your_table_nameMetaData); ?></xmp>


Here's another approach to counting the number of records in a particular category. (change the "if" criteria to suit your needs, and if you're using more than one counter on your page, change $count to some unique name, like $count2, or $count3),

First set a counter to zero.
Then in your "foreach" loop, if a record meets a particular set of criteria, increment the counter by 1 for each record that meets that criteria.
Finally, display the record count.

<?php $count = 0; ?><?php foreach ($my tableRecords as $record): ?<?php if ($record[your field'] != 1): ?><?php $count++; ?>

And where you want to display the count:

There are <?php echo $count ?> records

Hope that gives you some ideas.

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [Toledoh] Display number of items on list page

By Jason - March 31, 2011

Hi Tim,

Another way to go about this would be to use the function mysql_select_count_from(). This allows you to set a table name and a where clause. The function will then return the number of records that meet that where clause.

For example, let's assume you're storing all your items for sale in a table called "items". This table has a field called "type" whose value is the "num" field that you're outputting on your list page. You can then use the function to display how many items you have for each type like this:

<?php foreach ($product_typesRecords as $record): ?>
<a href="results.php?type=<?php echo $record['num'] ?>"><?php echo $record['title'] ?></a> [<?php echo mysql_select_count_from('items', "type ='".$record['num']."'");?> Items for sale]
<?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/

Re: [Toledoh] Display number of items on list page

By Jason - April 6, 2011

Hi Tim,

No problem. Glad that's working for you!
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

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