所以我有两个文本文件,一个叫做users.txt和schedule.txt。计划.txt如下所示:(请注意,上面的数字不在文件中,它们用作用户文件的参考)
0 1 2
2013-04-11^12:00|4:00|14:00
3 4 5 6
2013-07-21^10:00|15:00|18:00|15:00
7 8
2013-12-11^13:00|14:00
用户.txt文件如下所示:
约翰·史密斯^2|4|6
星球大战^0|1|3|6
卢克·天行者^2|6|7|8
这些数字中的每一个都对应于另一个文件的索引......例如,John Smith 在 2013-04-11 上有一个时间 14:00(从 2 开始),一个时间为 15:00(从 4 开始),依此类推......
好吧,我使用 HTML 为所有这些构建了一个表,我在提取时间(例如 12:00)的值时遇到问题。
另外,我对html和PHP非常陌生,所以请放轻松...如果您有指针或任何其他有用的信息,请分享=)当我尝试添加编辑(使用单选按钮进行编辑时间)并添加新用户时,我肯定会需要它。
非常感谢您的任何帮助/建议!
这是我的代码:
<?php
<!DOCTYPE html>
<html>
<head>
<title>Scheduler</title>
</head>
<body>
<h2>
<center>Select Your Meeting Times</center></h2>
<table border="1"
cellpadding="10">
<tr>
<th>User</th>
<th>Action</th>
<?php
//error_reporting(0);
date_default_timezone_set('America/New_York');
// used for displaying schedule times
$schedule= file('schedule.txt');
// loop through array and output contents
foreach($schedule as $s) {
list($year, $month, $day) = explode('-', $s);
$ga = explode("|", $s);
var_dump($year);
//echo $ga[5];
$year= intval($year, 10);
$month= intval($month, 10);
$day= intval($day, 10);
$h = mktime(0, 0, 0, $month, $day,$year);
$d = date("F dS, Y", $h); //used to get the day of the week
$w= date("l", $h); // w now holds the day of the week.
foreach($schedule as $t)
{
// $split = preg_split('/| /', $t, -1, PREG_SPLIT_NO_EMPTY);
list($time) = explode("|", $t);
list($time1) = explode(" ", $time);
// var_dump($time1);
// $output = trim($time1);
// echo $test;
echo "<th>" . $w . "<br>" . $month . "/" . $day . "/" . $year . "</th>'n";
}
}
?>
</tr>
<tr>
<?php
$text = file('user.txt');
// loop through array and output contents
foreach($text as $user) {
list($u) = explode('^', $user);
echo "<tr>" . "<td>" . $u . "</td>" . "</tr>'n";
}
?>
</tr>
<tr>
<th><br></th>
</tr>
<tr>
<th>Total</th>
</tr>
</table>
</body>
</html>
请不要这样做。如果要使用文本文件(对于可以的小应用程序),请使用合理的文本格式。取消所有这些自定义分隔符,只使用 CSV 格式(您可以使用 fgetcsv() 将一行读取到数组中),或者更好的是,使用带有适当标签的 JSON 格式(使用 json_decode() 解压缩它),您在提取时间方面的麻烦将消失。
你也不需要所有那些糟糕的提取日期 - 查找DateTime::createFromFormat()并让自己的生活变得轻松。
上面的人已经评论了存储信息的更好方法,但要回答你关于 extract() 的问题:
当你这样做时
list($year, $month, $day) = explode('-', "2013-04-11^12:00|4:00|14:00");
你得到"11^12:00|4:00|14:00"
值为$day
,而不是"11"
。同样,当你这样做时
$ga = explode("|", "2013-04-11^12:00|4:00|14:00");
您将"2013-04-11^12:00"
作为第一个值。从您的其余代码来看,这可能不是您想要的。
我为你写了一个小班。我没有包括任何错误检查。^
后第一次仍然有点困惑,所以我在代码中添加了有关如何包含它的注释。另请注意,我没有注释代码,希望您能理解。
class UserTime{
var $data=array();
function __construct(){
$lines = file('test.txt', FILE_IGNORE_NEW_LINES);
foreach ($lines as $l){
list($d,$t)=explode('^', $l);
$this->data[]=$d;
$t=explode('|', $t);
// you have to remove the following line if you want to
// include the first time after ^
array_shift($t);
$this->data = array_merge($this->data,$t);
}
}
function getUser($user_string){
list($user_name,$ix)=explode('^', $user_string);
$userData['userName']=$user_name;
$userData['time']=array();
$idx = explode('|', $ix);
for($i=0;$i<count($idx);$i++){
if(isset($this->data[$idx[$i]])){
$userData['time'][]=$this->data[$idx[$i]];
}
}
return $userData;
}
}
您可以按如下方式使用该类
$users = new UserTime();
$udata = $users->getUser('John Smith^2|4|6');
print_r($udata);
$udata = $users->getUser('Star Wars^0|1|3|6');
print_r($udata);
输出如下。我认为您可以在HTML中自己使用它。
Array
(
[userName] => John Smith
[time] => Array
(
[0] => 14:00
[1] => 15:00
[2] => 15:00
)
)