Warning: count(): Parameter must be an array or an object that implements Countable

If you get this message after switching to php version 7.x, change your code to from count() to count(get_object_vars()).

Getting keys from an object

You can use array_keys to get the keys from an array but how do you get the keys from elements in an object. 

Easiest method is to cast the object to an array and use array_keys.

$Keys = array_keys(array)$ObjectName);

PDF generation

This code was used to generate a PDF from a special file. It's probably not useful to you in this form but I put it here so that I could find it again. It required fpdf.php library.

 

defined('_JEXEC') or die('Restricted access');
$db = JFactory::getDBO();
$query = $db->getQuery(true);

echo '<p>Program reads a file named ' . JPATH_SITE . '\tmp\PlannedProgramContent.txt which is produced by EntryEeze export.</p>';


// create_pdf();

create_table_for_sc_merge();


function create_table_for_sc_merge()
// This function takes the data and creates a CSV file that can be merged with the Word doc templates.
{
    $db = JFactory::getDBO();
    $query = $db->getQuery(true);

    // load the data file
    $results = parse_file();

    // format the data into the right format
    $PPC = generate_ppc_data($results);
    
    $FinalArray = reformat_ppc($PPC);

    // write array to CSV file
    $myfile = fopen(JPATH_SITE . '\tmp\SC_PPC_IMPORT.csv', "w") or die("Unable to open file!");
    fwrite($myfile, 'Time,MemberId,PartnerId,Start Order,Name,Coach,Choreographer,Section,Discipline,Category,Segment,E1,E2,E3,E4,E5,E6,E7,E8,E9,E10,E11,E12,E13');
    
    foreach($FinalArray as $Line)
    {
        fwrite($myfile, $Line ."\n");
    }

    fclose($myfile);
}


function reformat_ppc($PPC)
{
    $FinalArray = array();

    foreach($PPC as $key=>$Entry)
    {
        foreach($Entry as $key2=>$Level)
        {
            foreach ($Level as $key3=>$Types)
            {
                foreach($Types as $key4=>$Type)
                {
                    $x=1;
                    $Discipline = get_gender(addslashes($key));
                    
                    switch (strtoupper($Discipline))
                    {
                        case 'MALE':
                            $Discipline = 'Mens';
                            break;

                        case 'FEMALE':
                            $Discipline = 'Womens';
                            break;

                        default:
                            $Discipline = $key3;
                            break;
                    }
                    
                    // create first part of table
                    $Array1 = array("Time" => '', "MemberId" => '', "PartnerId" => '', "Start Order" => '', "Name" => utf8_decode($key), "Coach" => '',
                        "Choreographer"=>'', "Section"=>'', "Discipline"=>$Discipline, "Category"=>$key2, "Segment"=>$key4);

                    $Array2 = array();
                    foreach($Type as $Code)
                    {
                        $Array2['E'.$x] = trim($Code, " \t\n\r\0\x0B");
                        $x++;
                    }
                    while($x<14)
                    {
                        $Array2['E'.$x] = '';
                        $x++;
                    }
                    $FinalArray[] = implode(',', array_merge($Array1, $Array2));
                }
            }
        }
    }
    return($FinalArray);
}

function get_gender($Name)
{
    $db = JFactory::getDBO();
    $query = $db->getQuery(true);
    
    $sql = "SELECT `gender` FROM `#__skatersorter_css` WHERE CONCAT(`firstName`, ' ', `lastName`) = '{$Name}'";
        $db->setQuery($sql);
    $db->query();
    $result = $db->loadResult();
    return $result;
}

function create_pdf()
{
    require_once JPATH_SITE . '\fpdf\fpdf.php';

    $db = JFactory::getDBO();
    $query = $db->getQuery(true);

    $pdf = new FPDF('P', 'cm', 'Letter');
    $pdf->SetAutoPageBreak(1,1);

    // load the data file
    $results = parse_file();
    
    // format the data into the right format
    $PPC = generate_ppc_data($results);
    
    // set the line height for the elements
    $EH = 1.26;

    foreach($PPC as $key=>$Entry)
    {
        foreach($Entry as $key2=>$Level)
        {
            foreach ($Level as $key3=>$Types)
            {
                foreach($Types as $key4=>$Type)
                {
                    $x=1;
                    $pdf->AddPage();
                    $pdf->SetFont('Arial','B',12);

                    $pdf->Image(JPATH_SITE.'/images/sc_logo.png',2,.75,1.87, 1.44);
                    $pdf->SetY(1);
                    $pdf->Cell(0, 0, 'Skate Canada', 0, 1, 'C');
//                    $pdf->Cell(0, 1, '(Singles / Pairs) ' . $key2 . ' ' . $key4, 0, 1, 'C');
                    $pdf->SetFont('Arial','',12);
                    $pdf->Cell(0, 1, 'Judges Worksheet', 0, 1, 'C');
                    $pdf->Cell(0, 0, $key3 . ' ' . $key2 . ' ' . $key4, 0, 1, 'C');
                    $pdf->Ln(.5);
                    $pdf->Cell(0, .75, 'Star #          Skater  / Team Name                                                       Jdg#   Judge Name', 'LTR', 1, 'L');
                    $pdf->SetFont('Arial','B',12);
                    $pdf->Cell(0, .75, '                    ' . utf8_decode($key), 'BLR', 1, 'L');
                    $pdf->SetFont('Arial','',10);
                    $pdf->Ln(.3);

                    // first line of elements table
                    $Y = $pdf->GetY();
                    $pdf->Cell(.6, $EH, '' , 'LTB', 0);
                    $pdf->Cell(2, $EH, 'Transitions' , 'LTB', 0);
                    $pdf->Cell(2, $EH, 'Element', 'LTB', 0);
                    $X=$pdf->GetX() + 3.4;
                    $pdf->MultiCell(3.4, $EH/2, 'Comments / Transitions', 'LTB', 'C');

                    $pdf->SetXY($X, $Y);
                    $pdf->MultiCell(1.5, $EH, 'GOE', 1, 1);

                    foreach($Type as $Code)
                    {
                        $pdf->Cell(.6, $EH, $x++ , 'LT', 0);
                        $pdf->Cell(2, $EH, '' , 'LT', 0);
                        $pdf->Cell(2, $EH, $Code , 'LT', 0);
                        $pdf->Cell(3.4, $EH, '', 'LT', 0);
                        $pdf->Cell(1.5, $EH,'', 'LTR', 1);
                    }
                    while($x<=14)
                    {
                        $pdf->Cell(.6, $EH, $x++ , 'LTB', 0);
                        $pdf->Cell(2, $EH, '' , 'LTB', 0);
                        $pdf->Cell(2, $EH, '' , 'LTB', 0);
                        $pdf->Cell(3.4, $EH, '', 'LBT', 0);
                        $pdf->Cell(1.5, $EH, '', 'LTRB', 1);
                    }

                    // bottom comments box
                    $pdf->Ln(.3);
                    $Y1=$pdf->GetY();
                    $pdf->SetFont('Arial','B',8);
                    $pdf->Cell(13, 1, 'General Comments:', 'LTR', 1);
                    $pdf->Cell(13, 1, '', 'LR', 1);
                    $pdf->Cell(13, .7, '', 'LBR', 0);
                    $pdf->SetXY(14,$Y1);
                    $pdf->Cell(6.6, .6, 'Costume/Prop Violation', 'LTR', 1);
                    $pdf->SetX(14);
                    $pdf->Cell(6.6, .8, '', 'LBR', 0);

                    $pdf->SetXY(19.1,$Y1+.1);
                    $pdf->Cell(1.4,0.8,'',1);

            // start second column
                    $HH = 2.675; // height of spacer in second column
                    $HW = 9.8; // width of second column
                    $X=10.8;
                    $X1 = $X+$HW-1.7;
                    $Y1 = $Y+.1;
                    $pdf->SetFont('Arial','B', 10);
                    $pdf->SetXY($X, $Y);
                    $pdf->Cell($HW, .6, 'Skating Skills' , 'LTR', 1);
                    $pdf->SetX($X);
                    $pdf->Cell($HW, $HH, '' , 'LR', 1);
                    $pdf->SetFont('Arial','',8);
                    $pdf->SetX($X);
                    $pdf->MultiCell($HW, .25, 'Cleanness & Sureness  /  Deep Edges, Steps & Turns  /   Balance  /  Flow  /  Speed & Acceleration  /  Multi Directional  /  One Foot Skating' , 'LBR', 'C');

                    $pdf->SetFont('Arial','B', 10);
                    $pdf->SetX($X);
                    $pdf->Cell($HW, .6, 'Transitions' , 'LTR', 1);
                    $pdf->SetX($X);
                    $pdf->Cell($HW, $HH, '' , 'LR', 1);
                    $pdf->SetFont('Arial','',8);
                    $pdf->SetX($X);
                    $pdf->MultiCell($HW, .25, 'Continuity of Movement  /  Variety (Holds, Footwork, Body Movements)  /  Difficulty  /  Quality' , 'LBR', 'C');

                    $pdf->SetFont('Arial','B', 10);
                    $pdf->SetX($X);
                    $pdf->Cell($HW, .6, 'Performance' , 'LTR', 1);
                    $pdf->SetX($X);
                    $pdf->Cell($HW, $HH, '' , 'LR', 1);
                    $pdf->SetFont('Arial','',8);
                    $pdf->SetX($X);
                    $pdf->MultiCell($HW, .25, 'Involvement  & Projection  /  Carriage & Clarity of Movement  /  Variety & Contrast  /  Individuality  /  Unison  /  Spatial Awareness' , 'LBR', 'C');

                    $pdf->SetFont('Arial','B', 10);
                    $pdf->SetX($X);
                    $pdf->Cell($HW, .6, 'Composition' , 'LTR', 1);
                    $pdf->SetX($X);
                    $pdf->Cell($HW, $HH, '' , 'LR', 1);
                    $pdf->SetFont('Arial','',8);
                    $pdf->SetX($X);
                    $pdf->MultiCell($HW, .25, 'Purpose  /  Ice Coverage & Pattern  /  Use of Space  /  Phrase & Form  /  Originality' , 'LBR', 'C');

                    $pdf->SetFont('Arial','B', 10);
                    $pdf->SetX($X);
                    $pdf->Cell($HW, .6, 'Interpretation of the Music' , 'LTR', 1);
                    $pdf->SetX($X);
                    $pdf->Cell($HW, $HH, '' , 'LR', 1);
                    $pdf->SetFont('Arial','',8);
                    $pdf->SetX($X);
                    $pdf->MultiCell($HW, .25, 'Musical Movement  /  Expression  /  Finesse & Nuance  / Skaters Relationship Reflecting Music (Pairs)' , 'LBR', 'C');

                    // Right check boxes
                    $pdf->SetXY($X1, $Y1);
                    $pdf->Cell(1.6, 1, '', 1, 0);

                    $Y1 += 3*$EH;
                    $pdf->SetXY($X1, $Y1);
                    $pdf->Cell(1.6, 1, '', 1, 0);

                    $Y1 += 3*$EH;
                    $pdf->SetXY($X1, $Y1);
                    $pdf->Cell(1.6, 1, '', 1, 0);

                    $Y1 += 3*$EH;
                    $pdf->SetXY($X1, $Y1);
                    $pdf->Cell(1.6, 1, '', 1, 0);

                    $Y1 += 3*$EH;
                    $pdf->SetXY($X1, $Y1);
                    $pdf->Cell(1.6, 1, '', 1, 0);
                }
            }
        }
    }
    
    $pdf->Output('F', JPATH_SITE . '\tmp\Judges.pdf');
    
    echo '<p>PDF file created at ' . JPATH_SITE . '\tmp\Judges.pdf</p>';
}


function utf8_fopen_read($fileName)
{
    $fc = iconv('ASCII', 'UTF-8//IGNORE', file_get_contents($fileName));
    $handle=fopen("php://memory", "rw");
    fwrite($handle, $fc);
    fseek($handle, 0);
    return $handle;
}

function parse_file()
// load the file and explode it on tabs, then format it for the generate PPC routine.
{
    $db = JFactory::getDBO();
    $query = $db->getQuery(true);

    $fn = JPATH_SITE . '\tmp\PlannedProgramContent.txt';

    $results = array();
    
//    $handle = utf8_fopen_read($fn, "r");
    $handle = fopen($fn, "r");
    
    if ($handle)
    {
        while (($buffer = fgets($handle, 4096)) !== false)
        {
            $comp = preg_split("/[\t]/", $buffer);
            $results[] = $comp;
        }
        if (!feof($handle))
        {
            echo "Error: unexpected fgets() fail\n";
        }
        fclose($handle);    
    }
    else echo 'File Not Found';
    
    return $results;
}

function generate_ppc_data_legacy($results)
// gets the PPC data and structures it for the report
{
    foreach($results as $result)
    {
        if($result->Type == 'FS/FD')
            {
                $Type = 'Free';
            }
        else $Type = 'Short';

        if($result->PartnerName == '') // free skaters
        {
            $PPC[$result->Name][$result->Level][$result->Event][addslashes($Type)][] = $result->Code;
        }
        else
        {
            if ($result->Event =='Pairs' || $result->Event =='Dance') $PPC[($result->Name . ' & ' .$result->PartnerName)][$result->Level][$result->Event][$Type][] = $result->Code;
        }
    }
    return($PPC);
}


function generate_ppc_data($results)
// gets the PPC data and structures it for the report
{
    $db = JFactory::getDBO();
    $query = $db->getQuery(true);

    foreach($results as $result)
    {
        if($result[2] == 'FS/FD')
            {
                $Type = 'Free';
            }
        else $Type = 'Short';

        if($result[8] == '') // free skaters
        {
            $PPC[$result[0]][$result[3]][$result[4]][addslashes($Type)][] = $result[7];
        }
        else // pairs or dancers
        {
            $PPC[($result[0] . ' & ' .$result[8])][$result[3]][$result[4]][$Type][] = $result[7];
        }
    }
    return($PPC);
}

 

 

WAMP Joomla Really Slow

At some point, my Joomla system running on my local WAMP became incredibly slow. It was taking 30-60 seconds to load a page that only takes a few seconds to load on my cloud server.

I discovered that when I had enabled xdebug in my php.ini settings, I had turned on the profiler which was generating 80 MB files on each page load. When I disabled the profiler, then the site went back to loading normally.

Sort array of objects based on one of the object parameter values

$Skaters is an array of objects which have one parameter skatername and this will sort the array basedon teh skatername parameter.

usort($Skaters, function($a, $b)
        {
            return strcmp($a->skatername, $b->skatername);
        });