WSC - Top 10 Records with Comments

6 posts by 2 authors in: Forums > CMS Builder: Plugins & Add-ons
Last Post: April 1, 2015   (RSS)

By Perchpole - March 31, 2015

Hi, All -

I'd like to generate a top 10 list of records ordered by the number of comments they have received. For example...

Record Five (12)
Record Seven (11)
Record Ten (6)
Record One (4)
etc...

The number of comments is shown in the brackets.

I'm sure I could do this with query but would like to use getRecords calls if possible.

Do-able?

:0/

Perch

By gregThomas - April 1, 2015

Hi Perch,

Here is how you could display this data on the page, below is some example code using a test blog section I've created.

If you're pulling record from a section like this:

  // load records from 'blog'
  list($blogs, $blogMetaData) = getRecords(array(
    'tableName'   => 'blog',
    'loadUploads' => true,
    'allowSearch' => false,
  ));

Then you could display a list of records and the how many comments each one has like this:

<ul>
  <?php foreach($blogs as $blog): ?>
    <li><?php echo $blog['title']; ?> (<?php echo mysql_count('_wsc_comments', "`recordNum` = '{$blog['num']}' AND `tableOrTag` = 'blog'"); ?>)</li>
  <?php endforeach; ?>
</ul>

So the mysql_count function is one that's built into CMS Builder, and is used to count records. The first variable is the table name, and the second is a where statement that you want to use to filter the records.

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Perchpole - April 1, 2015

Hi, Greg -

Thanks for this. It's almost perfect. The only thing missing is the order. I need to pull out the top 10 most commented blog records and then show them in descending order.

How can I do that?

Thanks,

Perch

By Perchpole - April 1, 2015

Greg -

Brilliant. It works perfectly.

I've always believed that this kind of operation is quite heavy in terms of the load it puts on page rendering times. Is that right?

Thanks again,

Perch

By gregThomas - April 1, 2015

Hi Perch,

That's correct, once you've got a few hundred comments, this will have an impact on your load times.

What I do for this kind of meta data is write a cron plugin that runs every minute, and calculates and stores this meta information against each record itself. This will reduce the load on the server significantly. 

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com