Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to save and load an activity log in PHP?

Tags:

php

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.

like image 836
Fizzix Avatar asked Oct 22 '22 04:10

Fizzix


1 Answers

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:

  • How to create a file with file_put_contents(...)
  • How to read a file with file(...)
like image 197
STT LCU Avatar answered Oct 23 '22 19:10

STT LCU