Используя php, как мы можем прочитать 5 строк из CSV-файла, вызвать другую функцию, а затем прочитать следующие 5 строк и повторить это?

Ниже мой код. Этот код читает CSV-файл, содержащий около 100 профилей студентов. Эти профили отображаются в виде слайд-шоу на этой HTML-странице - http://www.depts.ttu.edu/honors/medallion-ceremony/test/. Я хотел бы показать рекламный слайд для каждого 5-го профиля. Пожалуйста помоги!

$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 ответ

Вы можете проверить в своем displayProfiles Функция, где вы находитесь. Используйте оператор по модулю http://php.net/manual/en/language.operators.arithmetic.php

Тогда вы можете сделать что-то вроде этого в вашем цикле:

if($count % 5 == 0) { 
   displayAdvertisement();
}
Другие вопросы по тегам