Jump to content

Recommended Posts

Posted (edited)

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.jpgMoreover, 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: &nbsp;<input type="text" name=“runner1" autofocus=""><br>

     2: &nbsp;<input type="text" name="runner2"><br>

     3: &nbsp;<input type="text" name="runner3"><br>

     4: &nbsp;<input type="text" name="runner4"><br>

     5: &nbsp;<input type="text" name="runner5”><br>    

      6: &nbsp;<input type="text" name="runner6"><br>

     7: &nbsp;<input type="text" name="runner7"><br>

     8: &nbsp;<input type="text" name="runner8"><br>

     9: &nbsp;<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>

before_race.jpg

after_race.jpg

Edited by Jayfromsandiego

Share this post


Link to post
Share on other sites
Posted (edited)

some suggestions -

1) don't create a bunch of variables without a good reason. the original $_POST variable(s) the data is in are perfectly fine to use in the rest of the code.

2) use arrays for sets of data. the form field name should be an array, i.e. name='runner[x]' with the array index (x) value being 1-14. this will let you loop over the submitted data using a simple foreach(){} loop so that you can dynamically process the data. this will eliminate the temptation to write out 14 lines of code that are doing nothing useful, but to waste your time typing (unless this is part of a keyboard/typing class?)

3) leading zero's on numbers in php cause them to be treated as octal. either treat all the values as integers and zero fill them when displaying them, or treat all the values as strings.

4) the three separate arrays of registration values should be instead one array (use the same base array name for all three), with an index value that indicates the division (the index can either just be a number or a string, but needs to be in the order that you want to produce the output in.)

5) to produce the final output, loop over the array of submitted $_POST['runner'] data. this will give you the runner number for positions 1-14. at the point that you have the current runner number for a position, loop over the registration main array and use in_array() to find if the current runner number is in each successive registration sub-array. if the current runner number is not in the current registration sub-array, output a &nbsp; in the current html table cell. if the current runner number is in the current registration sub-array, output the runner number in the current html table cell and increment a counter for the current registration sub-array index (use an array for the counter, with the index being the same index value you used in the registration main array.) to produce the last count row in the html table, just loop over the registration main array one more time and access the corresponding entry in the counter array.

6) you would want to make the form fields 'sticky' and set the value to any corresponding submitted $_POST['runner'] value, since you aren't actually storing the results in a database to make them persistent.

7) you would dynamically produce the form fields, rather than to have them statically written out.

 

Edited by mac_gyver

Share this post


Link to post
Share on other sites

when looping to produce that html table, you would actually loop from 1 to 14 and test if the corresponding $_POST['runner'] element is set, since there won't initially be any $_POST data.

Share this post


Link to post
Share on other sites

I advised him about using arrays and loops in his last topic.

... and some fell on stony ground.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.