PHP - 5k - Race
Hello everyone. I’m trying to hone my skills with arrays and forms. I previously coded a 2-page form with a single input and I thought I would give a 1-page form with multiple inputs a shot in PHP. I use to run cross-country, so I came up with the following .. FICTIONAL SCENARIO: I have high school runners competing in a 5K race. Every year 300 or less participants attend; the cut-off is 300 runners. There are three types of runners competing which are broken up according to their division. 1) Frosh / Soph , 2) Jr. Varsity and 3) Varsity Before the race, each runner is assigned a 3-digit number sticker to their jersey, so the judges can keep track of the runners. Medals are usually awarded to the top 14 runners to finish the course. Lets pretend on the day of the race, there were 86 participants in the Frosh / Soph level, 62 participants in the Jr. Varsity level and 42 participants in the Varsity level division. I have question marks below that act as placeholders for each of the 3 group’s runners numbers (See 1st: before_race.jpg ). These variable numbers would be predetermined before the race event and data entered into their respective column by the head coach. What I am trying to code is what group the “Top 14 Finishers” belong to after the race takes place. The User Interface would have to be visible to the announcer(s). I’m thinking a one page form with 14 inputs (???, etc.) for the 14 unique runners. Then, compare each of the 14 finishers against the 3 array database. For example, lets say the Top 14 runners came in this order .. “001”, “238”, “358”, “267”, “159”, “013”, “099”, “135”, “469”, “477”, “288”, “016”, “689”, “177”. The announcer(s) also have a back-up hard copy list in numerical order with them which they can visually scan at a glance to locate the number assigned to each runner. So if for instance, Mike Reynolds was assigned the number 238, the announcer would look up the number 238 into the UI at the conclusion of the race and know instantly that Mike Reynolds is a Jr. Varsity level runner. (See 2nd: after_race.jpg) Moreover, the race officials could quickly see there were 5 Frosh / Soph runners, 4 Jr. Varsity runners and 5 Varsity runners in the Top 14 finishers. In conclusion, each column would be tallied up with a cumulative total output to verify the 14 runners total. Again, (See 2nd: after_race.jpg ) I’m not sure what would be the best way to efficiently code this. How should I code this? Any suggestions? Please review the 2 attached images. ============ PHP Code: <?php if(isset($_POST['Submit'])){ $runnerone = $_POST[“runner1"]; // Start variables for Top 14 Finishers $runnertwo = $_POST["runner2"]; $runnerthree = $_POST["runner3"]; $runnerfour = $_POST["runner4"]; $runnerfive = $_POST["runner5"]; $runnersix = $_POST["runner6"]; $runnerseven = $_POST["runner7"]; $runnereight = $_POST["runner8"]; $runnernine = $_POST["runner9"]; $runnerten = $_POST["runner10"]; $runnereleven = $_POST["runner11"]; $runnertwelve = $_POST["runner12"]; $runnerthirteen = $_POST["runner13"]; $runnerfourteen = $_POST["runner14"]; // End variables tracking // Begin - High School (3 Division Database) $arrayFrosh = “013, ???, 358, ???, 099, ???, 469, ???, 689, ???, etc..”; // This entry list can be variable, but I intentionally placed #s to illustrate a possible race outcome $arrayJr = “???, 016, ???, ???, 238, ???, 267, 288, ???, etc..”; // This entry list can be variable .. $arrayVarsity = “001, ???, ???, 135, ???, ???, 159, ???, 177, ??, 477, etc..”; // This entry list can be variable .. } ?> <html> <head> <title>5K :: Championship Race</title> <body bgcolor=“#FFFFFF”> <div> <form action="#" method="post"> 1: <input type="text" name=“runner1" autofocus=""><br> 2: <input type="text" name="runner2"><br> 3: <input type="text" name="runner3"><br> 4: <input type="text" name="runner4"><br> 5: <input type="text" name="runner5”><br> 6: <input type="text" name="runner6"><br> 7: <input type="text" name="runner7"><br> 8: <input type="text" name="runner8"><br> 9: <input type="text" name="runner9"><br> 10: <input type="text" name="runner10"><br> 11: <input type="text" name="runner11"><br> 12: <input type="text" name="runner12"><br> 13: <input type="text" name="runner13"><br> 14: <input type="text" name="runner14"><br><br> <input type="submit" name="Submit"> </form> </div> </body> </head> </html>
Edited January 7, 2019 by Jayfromsandiego Similar TutorialsDo race conditions usually exist when writing OR appending to an existing file? Code: [Select] $f1 = fopen($filename1, 'r+'); $f2 = fopen($filename2, 'a'); flock($f1, LOCK_EX); flock($f2, LOCK_EX); fwrite($f1, 'foo'); fwrite($f2, 'foo'); flock($f1, LOCK_UN); flock($f2, LOCK_UN); fclose($f1); fclose($f2); What would happen if this code ran at the exact same time? Is there a chance that the data could be corrupted in the aforementioned code example? I have the following code:
$fp = fopen(“path_to_file”, ‘a’); flock($fp, LOCK_EX); fwrite($fp, $string); flock($fp, LOCK_UN); fclose($fp);If I try to lock the file in two different places at the same time, this will cause a race condition. How can I prevent this? I know in Java, for example, it has a concurrent library which contains reentrant lock, which basically tries to get the lock and if can't waits. What can I do in PHP? |