1

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) {.....}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Priya
  • 11
  • 3
  • I have edited you code, please have a look at how you enter code in SO. all you need is a single tab or 4 spaces at the beginning of a line to get it to look like a code block – RiggsFolly Apr 11 '18 at 16:59
  • Why re-invent the wheel, see [The PHP Manual for fgetcsv()](http://php.net/manual/en/function.fgetcsv.php) – RiggsFolly Apr 11 '18 at 17:03
  • Thank you RiggsFolly. This is my first time posting a question and I didn't know how to enter the code. – Priya Apr 11 '18 at 19:54

1 Answers1

2

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();
}
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Bryan
  • 3,449
  • 1
  • 19
  • 23