首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用php,我们如何从csv文件中读取5行,调用另一个函数,然后读取下5行并重复此操作?

使用php,我们如何从csv文件中读取5行,调用另一个函数,然后读取下5行并重复此操作?
EN

Stack Overflow用户
提问于 2018-04-12 00:51:23
回答 1查看 126关注 0票数 1

下面是我的代码。这段代码读取一个包含大约100个学生配置文件的csv文件。这些配置文件在这个HTML页面中以幻灯片的形式显示-- http://www.depts.ttu.edu/honors/medallion-ceremony/test/。我想显示一个广告幻灯片为每5个配置文件。请帮帮我!

代码语言:javascript
复制
$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) {.....}
EN

回答 1

Stack Overflow用户

发布于 2018-04-12 01:31:32

你可以在你所在的地方签入你的displayProfiles函数。使用模运算符http://php.net/manual/en/language.operators.arithmetic.php

然后你可以在你的循环中做类似这样的事情:

代码语言:javascript
复制
if($count % 5 == 0) { 
   displayAdvertisement();
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49780573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档