Name Count in foreach loop

7 posts by 3 authors in: Forums > CMS Builder
Last Post: September 13, 2017   (RSS)

By Zicky - September 11, 2017

Thanks to help of the thread below I was able to remove the duplicates from my foreach loop.

But I still can't figure out how to perform the total of each name in counting function.

Example of what I'm trying to achieve:

John: 24
Tommy: 18
Victor: 12
Jane: 21
Janet: 13

Example of what I'm getting, which is only counting a single instance of the first name and not all the Johns, Tommys, Victors, Janes, and Janets:

John: 1
Tommy: 1
Victor: 1
Jane: 1
Janet: 1

<?php $alreadyListed = []; ?>
<?php foreach ($namesCountRecords as $key => $record): ?>
    <?php if(!in_array(strtolower($record['first_name']), $alreadyListed)): ?>
        <?php echo $record['first_name'] ?>
        <?php $alreadyListed[] = strtolower($record['first_name']); ?>
            <?php $str = $record['first_name']; ?>
            <?php $key = count($str); ?>
            <?php print_r($key); ?><br>
    <?php endif; ?>
<?php endforeach ?>

Anyone have any suggestions on how to get this to work or a better way to remove duplicate names and still count the totals for person's similar each name?

Also, any suggestions on how to change <?php print_r($key); ?> to an <?php echo $key; ?> ? When I try this it breaks.

Thanks, Zicky

By Dave - September 11, 2017

Hi Zicky, 

How about this? 

  $firstNames    = array_column($namesCountRecords, 'first_name');
  $namesToCounts = array_count_values($firstNames);
  foreach ($namesToCounts as $firstName => $count) {
    print htmlencode($firstName) .": $count<br/>\n";   

Let me know if that works for you!

Dave Edis - Senior Developer

By Zicky - September 11, 2017

Dave - Thank!!! You're the man!

Works like a charm and it's so clean.

By Zicky - September 12, 2017 - edited: September 12, 2017

I built a bar chart using Charts.js "" that displays people's first names and the total number of people with the same first name. Thought I'd share this for anyone else who may find it useful. You'll need to get a copy of Chart.js to make this work.

...and if you build any of your own charts, please share what you came up with.

Many thanks to Dave for the help getting the first names and counting worked out.

Below is the code, and I also attached a "Bar Chart" PHP file to this post.


<?php header('Content-type: text/html; charset=utf-8'); ?>
  /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
  // load viewer library
  $libraryPath = 'cmsb/lib/viewer_functions.php';
  $dirsToCheck = array('/match/your/directory/','','../','../../','../../../');
  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 from 'name'
  list($nameRecords, $nameMetaData) = getRecords(array(
    'tableName'   => 'name',
    'loadUploads' => true,
    'allowSearch' => false,


<!doctype html>
<meta charset="UTF-8">
<title>Bar Chart</title>

<style type="text/css">
    #myChart {
        margin:0 auto;

<script src="js/Chart.2.6.0.bundle.js"></script>
<script src="js/utils.js"></script>



<canvas id="myChart"></canvas>

var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: [
                $pagenames    = array_column($nameRecords, 'first_name');
                $namesToCounts = array_count_values($pagenames);
                    foreach ($namesToCounts as $pagename => $count) {
                    print ('"'.$pagename.'",');
            } ?>
        datasets: [{
            label: 'Most Used Names',
            data: [
                    $pagenames    = array_column($nameRecords, 'first_name');
                    $namesToCounts = array_count_values($pagenames);
                        foreach ($namesToCounts as $pagename => $count) {
                        print htmlencode($count.',');
                } ?>
            backgroundColor: [
                    <?php foreach ($nameRecords as $record): ?>
                        <?php { $r = rand(0,255); $g = rand(0,255); $b = rand(0,255); $opacity = 0.4; } ?>
                        '<?php echo "rgba(".$r.",".$g.",".$b.",".$opacity.")"; ?>',
                    <?php endforeach ?>
            borderColor: [
                            'rgba(0, 0, 0, 0.4)',
            borderWidth: 1
    options: {
        scales: {
            yAxes: [{
                ticks: {



bar-chart.php 3K

By Deborah - September 13, 2017

Zicky, thanks for sharing this with us all!

I set up a chart according to your example and it works beautifully. If I use Chartjs on a future project, I'll be sure to post back here with my code.

~ Deborah

By Zicky - September 13, 2017

Glad you found it useful. I'll post up any other charts I build as well. Got a few others in mind I plan to build.

Cheers, Zicky