5

Before I begin, THIS IS AN ASSIGNMENT. I am just after a helping hand an some guidance :)

I am developing a gym membership website which allows users to register, login, and book an activity.

I have everything working, besides the activity booking system, and that is what I need guidance with. I have a single txt file that contains an activity log of each activities name, time, how many can attend in total. I would like to add to the end of each activities line of who is attending exactly. Therefore, allowing users to book the activity, which adds their username to the activity.

This is exactly how I currently have my activity.txt file printing (besides the 'Book' buttons on the far right.

NumberOne       3       Wednesday 8am       (BOOK)
NumberTwo       7       Thursday 10am       (BOOK)
NumberThree     20      Monday 1pm          (BOOK)
NumberFour      15      Tuesday 5pm         (BOOK)

Basically, what I need guidance with is the 'Book' buttons. Once a 'Book' button is pressed, it will book the user in to the activity.

This is how my activity.txt file currently looks:

NumberOne,3,Wednesday 8am
NumberTwo,7,Thursday 10am
NumberThree,20,Monday 1pm
NumberFour,15,Tuesday 5pm

And this is the code I currently have that is printing the activities:

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>";
}

Below is my users.txt file if needed. All passwords are saved in MD5

jiten:3fc0a7acf087f549ac2b266baf94b8b1
jb:3fc0a7acf087f549ac2b266baf94b8b1
test:bed128365216c019988915ed3add75fb
joeBlogg:bed128365216c019988915ed3add75fb
userX:bed128365216c019988915ed3add75fb
userY:bed128365216c019988915ed3add75fb

All help is greatly appreciated. Very confused on how to do this.

4

2 回答 2

2

Here's a rough guideline (I won't go in the specifics because it's homework after all) and I'll keep the assumption you're not allowed to use anything different than text file but can create/delete as many of them as you wish.

  1. parse the activity txt file, using file(...) with the proper flags set
  2. build an array which key are the activities "IDs" (numberOne, numberTwo, etc) and value is another array containing the activity details. You may find handy explode() here.
  3. For each activity create a file dedicated to it (if it doesn't exist already, of course). This file will contain the names of the registered users that booked the activity. I suggest you to write one name per line in this file.
  4. when somebody press the "book activity" button, you'll have to do the following steps:

    • Check if the requested activity file exists. if it doesn't, create an empty one.
    • Count the current number of booked users and compare it with the maximum allowed
    • If it's less, add the new user to the file, otherwise reject the booking
  5. To show the list of the booked users, simply read the file contents from each activity file. You may find useful implode() this time.

I hope this will help you finding the right track. A last note, text files aren't designed to do this: a small database is easier to use, more fun to learn and more useful for your future.

References:

于 2013-07-30T07:39:56.917 回答
1

A few things:

  1. This doesn't sound like a log; it sounds like a file-based database.
  2. If allowed, use a .csv file (not .txt) and parse it using fgetcsv().
  3. Since it's your database you'll need to write to it. You can use fputcsv() for that.

(FWIW, your .txt file is already a csv, you're just not treating it that way.)

Per the specific question: Clicking "Book" just needs to pass the Course Name to a script that will add the current user to that course (if they haven't already registered). You can pass the course name using POST (HTML forms) or, more simply, GET (links and query parameters).

Using links (GET) might look like this:

    $bookingUrl = "register.php?courseName=".urlencode($courseName);

Using a form (POST) might look like this:

    <form action="register.php" method="POST">
        <input type="hidden" value="<?= $courseName; ?>" />
        <input type="submit" value="Book" />
    </form>

Another answer mentioned using separate files to track registrations per course, but I don't think that's necessary; just use one. It's helpful to think of what you're really tracking:

  1. Available Courses, "courses.txt": Course Name, Course Time (got it)
  2. Users, "users.txt": Username, Password (got it)
  3. Registrations, "registrations.txt": Username, Course Name (don't got it)
于 2013-07-30T07:50:35.860 回答