Record counter from another section list menu type

7 posts by 2 authors in: Forums > CMS Builder
Last Post: November 18, 2010   (RSS)

Hi,

I created a multi menu type section (used_items) where one of the fields is list field type (item) and selected from another category menu section (cat_items).
I'm trying to display in list page every records from cat_items & count and display how many records are created in the used_items with each cat_items.

Until now:
<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php


// load viewer library
$libraryPath = 'bcmAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/home/vournas/public_html/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records
list($cat_itemsRecords, $cat_itemsMetaData) = getRecords(array(
'tableName' => 'cat_items',
'loadUploads' => '0',
'allowSearch' => '0',
));

// load records
list($used_itemsRecords, $used_itemsMetaData) = getRecords(array(
'tableName' => 'used_items',
'loadUploads' => '0',
'allowSearch' => '0',
));

?>

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>

<?php foreach ($cat_itemsRecords as $record): ?>
<li><a href="#"><?php echo $record['name'] ?>&nbsp;(x)</a></li>
<?php endforeach ?>

</body>
</html>


The (x) is the counter holder of each category.
Can anybody advise how i can count these records in the foreach for every category.

Thank you in advance,

Re: [ht1080z] Record counter from another section list menu type

By Jason - November 18, 2010

Hi,

What you can do is create an array that keeps a tally of the number of times each cat_item is used in used_items.

The question is, in the used_items section, what are you using for the value field of your item list? Are you using num or name?

For this example, I'm going to assume you're using num, if not, you can change the code to use name.

First you need to do a foreachloop to go through each of your used_items records. The loop will increment an element in a array:

$cat_count=array();

foreach($used_itemsRecords as $used_item){
@$cat_count[$used_item['item']]++;
}


You can now output this count as part of your other foreach loop.

<?php foreach ($cat_itemsRecords as $record): ?>
<li><a href="#"><?php echo $record['name'] ?>&nbsp;(<?php echo intval(@$cat_count[$record['num']];?>)</a></li>
<?php endforeach ?>


Give this a try and let me know if you run into any problems.

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: [ht1080z] Record counter from another section list menu type

By Jason - November 18, 2010

Hi,

The problem is there is a missing semicolon ";" after you output the record's name. Try changing it to this:

<li><a href="#"><?php echo $record['name']; ?>&nbsp;(<?php echo intval(@$cat_count[$record['item']];?>)</a></li>

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: [Jason] Record counter from another section list menu type

Jason,

i corrected the syntax but got the same Parse error in the same line.
Parse error: syntax error, unexpected ';' in /home/vournas/public_html/test.php on line 155

<li><a href="#"><?php echo $record['name']; ?>(<?php echo intval(@$cat_count[$record['item']]; ?>)</a></li>

Can you please recheck my previous reply, think i messed up the script, also i attached some screenshot of the CMSB tables.

Thank you,

Re: [ht1080z] Record counter from another section list menu type

By Jason - November 18, 2010

Hi,

Oops, we forgot a closing bracket. Give this a try:

<li><a href="#"><?php echo $record['name']; ?>(<?php echo intval(@$cat_count[$record['item']]); ?>)</a></li>

That should take care of that error 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/

Re: [Jason] Record counter from another section list menu type

Jason,

I correct the syntax error and changed the $record['item'] to $record['num'] and it works!

Thank you for your support on this! [:)]