使用php,我们如何从csv文件中读取5行,调用另一个函数,然后读取接下来的5行并重复此操作?
下面是我的代码。此代码读取一个csv文件,该文件包含大约100个学生档案。这些配置文件在此HTML页面中显示为幻灯片-。我想为每5个人资料显示一张广告幻灯片。请帮忙使用php,我们如何从csv文件中读取5行,调用另一个函数,然后读取接下来的5行并重复此操作?,php,csvtoarray,Php,Csvtoarray,下面是我的代码。此代码读取一个csv文件,该文件包含大约100个学生档案。这些配置文件在此HTML页面中显示为幻灯片-。我想为每5个人资料显示一张广告幻灯片。请帮忙 $profiles = csv_to_array($_SERVER['DOCUMENT_ROOT'].'/...../profiles.csv'); function csv_to_array($filename, $delimiter=',', $enclosure='"', $escape = '\\') { if(!
$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) {.....}
$profiles=csv_to_数组($_服务器['DOCUMENT_ROOT'./../profiles.csv');
函数csv\u to\u数组($filename,$delimiter=',',$enclosure=',$escape='\\')
{
如果(!file_exists($filename)| |!is_readable($filename))返回false;
$header=null;
$data=array();
$lines=文件($filename);
foreach($line作为$line){
$values=str_getcsv($line,$delimiter,$enclosure,$escape);
如果(!$header)
$header=$value;
其他的
$data[]=数组组合($header,$values);
}
返回$data;
}
函数显示配置文件($limit=100)
{
全球美元概况;
$count=1;
foreach($profile作为$profile)
{
如果($count您可以在您所在的位置签入您的displayProfiles
函数。使用模运算符
然后,您可以在循环中执行以下操作:
if($count % 5 == 0) {
displayAdvertisement();
}
我已经编辑了你的代码,请看看你是如何输入代码的。你只需要在一行的开头加上一个标签或4个空格,就可以让它看起来像一个代码块。为什么要重新发明轮子,请参阅Thank you RiggsFolly。这是我第一次发布问题,我不知道如何输入代码。