0

我正在尝试打印 txt 文件的内容。我在表格中正确打印了每个项目,虽然我想做一些小的修改,但我不知道如何做。

这就是我的 activity.txt 文件的外观

1,Cycle,3,Wednesday 8am
2,Aqua,7,Thursday 10am
3,Circuit,20,Monday 1pm
4,Zumba,15,Tuesday 5pm

我不想打印出文件开头的唯一 ID。相反,我想将它们全部保存为单独的变量以供使用。如

$1
$2
$3
$4

我还想在每个活动的末尾添加用户名,然后将这些用户名保存到一个数组中,如下所示(用户名不应打印在页面上):

1,Cycle,3,Wednesday 8am: userY,userX,joe
2,Aqua,7,Thursday 10am: jack,john,joe
3,Circuit,20,Monday 1pm: oneUser,twoUser
4,Zumba,15,Tuesday 5pm: testUser,user

然后将具有以下数组:

array1(userY,userX,joe);
array2(jack,john,joe);
array3(oneUser,twoUser);
array4(textUser,user);

我目前拥有的将我的活动.txt 文件打印到页面的代码如下:

function bookActivity()
{
    $fileContentsArray = file ("activity.txt");
    echo "<table>";
    foreach ($fileContentsArray as $one_persons_data)
    {
        echo '<tr>';
        $splitted = preg_split('/,/', $one_persons_data);
        foreach ($splitted as $one) 
        {
            echo "<td>$one</td>";
        }
        echo '</tr>';
    }
    echo "</table>";

}

所有的帮助和建议都会很棒。

谢谢!

4

2 回答 2

1
$_SESSION['userName'] = 'oneUser';
$activities = array();
$activityKeys = array('ID','name','spaces','date');
function getActivities()
{
    global $activities,$activityKeys;
    $activities = array();
    $contents = file('activity.txt');
    foreach($contents as $line)
    {
        $activity = explode(':',$line);
        $data = array_combine($activityKeys,explode(',',$activity[0]));
        $users = isset($activity[1]) ? array_filter(explode(',',trim($activity[1]))) : array();
        $activities[$data['ID']] = compact('data','users');
    }
}
getActivities();
function userInActivity($activityID)
{
    global $activities;
    return in_array($_SESSION['userName'],$activities[$activityID]['users']);
}
function saveActivities()
{
    global $activities;
    $lines = array();
    foreach($activities as $id => $activity)
    {
        $lines[$id] = implode(',',$activity['data']).':'.implode(',',array_filter($activity['users']));
    }
    return file_put_contents('activity.txt',implode("\r\n",$lines)) !== false;
}
function bookActivity()
{
    global $activities;
    if(isset($_POST['book']))
    {
        $unbook = false;
        $bookID = intval($_POST['book']);
        if($bookID < 0)
        {
            //If a negative ID has been submitted, that means we are "unbooking"
            $unbook = true;
            $bookID = abs($bookID); //Set the ID back to the normal, positive value
        }
        if($unbook)
        {
            $users = $activities[$bookID]['users'];
            $userKey = array_search($_SESSION['userName'],$users);
            array_splice($activities[$bookID]['users'],$userKey,1);
        }
        else
        {
            $activities[$bookID]['users'][] = $_SESSION['userName'];
        }
        saveActivities();
    }
}
bookActivity();
function activityList($form=true)
{
    global $activities;
    if($form === true) echo '<form action="" method="post">';
    ?>
    <table>
        <thead>
            <tr>
                <?php
                $firstKey = @array_shift(array_keys($activities));
                foreach(array_keys($activities[$firstKey]['data']) as $index => $column)
                {
                    echo '<th>'.$column.'</th>';
                }
                ?>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <?php
            foreach($activities as $activity)
            {
                echo '<tr>';
                $id = $activity['data']['ID'];
                $activity['data']['spaces'] = $activity['data']['spaces']-count($activity['users']);
                foreach($activity['data'] as $item)
                {
                    echo '<td>'.$item.'</td>';
                }
                $booked = userInActivity($id,$_SESSION['userName']);
                $buttonText = $booked ? 'Unbook' : 'Book';
                $val = $booked ? -$id : $id;
                echo '<td><button type="submit" name="book" value="'.$val.'">'.$buttonText.'</button></td>';
                echo '</tr>';
            }
            ?>
        </tbody>
    </table>
    <?php
    if($form === true) echo '</form>';
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Activities</title>
    </head>
    <body>
        <?php
        activityList();
        ?>
    </body>
</html>
于 2013-07-30T08:58:22.300 回答
0

我想这会对你有所帮助

$i=1;
    foreach ($splitted as $one) 
    {
        if($i==1)
        {
            $i=0;
        }
        else
        {
            echo "<td>$one</td>";
        }
    }
于 2013-07-30T08:50:27.727 回答