繁体   English   中英

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

[英]Using php, how can we read 5 rows from a csv file, call another function and then read next 5 rows and repeat this?

下面是我的代码。 此代码读取的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) {.....}

您可以在哪里签入displayProfiles函数。 使用取模运算符http://php.net/manual/en/language.operators.arithmetic.php

然后,您可以在循环中执行以下操作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM