Below is my Code. This code reads a csv file having around 100 student profiles. These profiles are displayed as a slideshow in this HTML page - http://www.depts.ttu.edu/honors/medallion-ceremony/test/. I would like to display a advertisement slide for every 5th profile. Please help !

$profiles = csv_to_array($_SERVER['DOCUMENT_ROOT'].'/...../profiles.csv');

function csv_to_array($filename, $delimiter=',', $enclosure='"', $escape = '\\')
{
    if(!file_exists($filename) || !is_readable($filename)) return false;
    $header = null;
    $data = array();
    $lines = file($filename);
    foreach($lines as $line) {
        $values = str_getcsv($line, $delimiter, $enclosure, $escape);
        if(!$header) 
            $header = $values;
        else 
            $data[] = array_combine($header, $values);
    }
return $data;
}

function displayProfiles($limit=100)
{
    global $profiles;
    $count = 1;
    foreach ($profiles as $profile)
    {
        if ($count <= $limit)
        {
            displayProfile($profile);
            $count++;
        }
    }   
}

function displayProfile($profile) {.....}
1

There are 1 best solutions below

0
On

You can check in your displayProfiles function where you are. Use the modulo operator http://php.net/manual/en/language.operators.arithmetic.php

Then you could do something like this in your loop:

if($count % 5 == 0) { 
   displayAdvertisement();
}