【发布时间】:2018-08-16 22:41:50
【问题描述】:
我正在尝试为一家活动公司制定小组成员的议程 - 他们的网站是用 PHP 制作的。他们已经有一个列出小组成员的 CSV 文件。我编写了一些代码,以便他们可以将 CSV 上传到他们的服务器并将其呈现为表格。
csv 或多或少是这样设置的:
Panel, Name, Last Name, Title, Company, Moderator
tuesday, John, Doe, Partner, Acme,1
tuesday, Jane, "O Reily", Partner, SkyNet,0
tuesday, Samatha, Klein, CEO, Sea World,0
tuesday, Bill, Clarke, Head of Marketing, TNT,0
wednesday, Mohammed, Algarisi, Managing Director, Cheesy Photos,1
wednesday, Tim, Draper, Founding and Managing Partner, Draper Associates,0
无论如何,他们希望小组成员按姓氏字母顺序排序,主持人首先显示。我在 PHP 中执行此操作时遇到问题。
我不太习惯 PHP 代码,所以我确定我一定缺少一些东西,我应该以不同的方式进行设置吗?最好的排序方法是什么?
这基本上就是我所做的-
首先我创建了一个小组成员课程:
class Panelist {
function __construct($panel, $name, $lastname, $title, $company, $moderator){
$this -> panel = $panel;
$this -> name = $name;
$this -> lastname = $lastname;
$this -> title = $title;
$this -> company = $company;
$this -> moderator = $moderator;
}
}
然后是一个空数组,我们将在其中存储 Panellist 对象 $panelists =array();
$row = 1;
//accesses our csv file from which we will get the data for the objects
if(($handle = fopen("agenda.csv", "r")) !== FALSE) {
//loops through the csv file by row
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
//skips the header (first) row
if($row == 1) {$row++; continue;}
//instanciates a Panelist object for every row in the csv file
$name = new Panelist($data[0], $data[1], $data[2], $data[3], $data[4], data[5] );
//adds object to our $panelist array
array_push($panelists, $name);
}
然后我有一个接收两个参数的输出函数:
1. $arr - the array where the objects are stored
2. $panelName - the name of the panel to output
function outputSpeakers($arr, $panelName){
// loops through objects in $arr
foreach($arr as $obj){
//only outputs objects with a panel value matching $panelName:
if($obj->panel == $panelName){
$name = $obj->name;
$lastname = $obj->lastname;
$title = $obj->title;
$company= $obj->company;
//lots of condition formatting stuff here that's not important such as...
if($obj->moderator == '1'){
//if the moderator is "TBA" - don't output title or company:
if($name == ' TBA'){
//format this way
} //else ...
}
}
}
}
?>
然后,在我的议程.php 文件中,我包含上面的类文件并执行:
<div class="panel-list">
<? outputSpeakers($panelist, "tuesday"); ?>
</div>
谢谢! :-)
【问题讨论】: