SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2006
    0 Post(s)
    0 Thread(s)

    Post Grouping Dynamic HTML Form Values for Insert statment


    I'm trying to group dynamic form values to Insert into a database using php and mysql.

    The background is that I'm trying to develop a web based Football Prediction League.

    I'm retrieving the following values from my Fixtures table

    Fixid, Team1Name, Team2Name

    and populating a HTML form where the user will enter, obviously enough, the goals for either team

    My HTML form displays
    Team1 Name Team 1Goals Team2 Goals Team2 Name

    I want to submit this form to another page which will insert the following values into a Prediction table

    Prediction Id, UserId,FixtureId,Team1 Goals, Team 2 Goals

    The code is

    HTML Code:
    <form name="fixtures" action="target.php" method="get">
    <table class="fixtures">
    <th>Team 1</th><th>Goals</th><th>Goals</th><th>Team 2</th>
    PHP Code:
    while ($row $res->fetch_array(MYSQLI_NUM)) {
    '<input type="hidden" name="fixture[]" value ="'.$row[0].'">';
    '<td>'.$row[2].'</td><td><input type="text" name="f'.$row[0].'t1g" id="f'.$row[0].'t1g" value="" maxlength="3" class="fixtures" /></td>';
    '<td class="fixtures"><input type="text" name="f'.$row[0].'t2g" id="f'.$row[0].'t2g" value="" maxlength="3" /></td>';
    HTML Code:
    <input class="wsubmit" type="submit" name="submit" value="Submit" id="submit" />
    <input class="wreset" type="reset" name="reset" value="Reset" id="reset" /
    My difficulty is how do I deal with the values coming in when the page is submitted. I've tried doing a foreach loop on the $_GET array for all values but cant seem to figure out how I a group each teams goal prediction with the relevant fixture id
    i.e. something like insert into prediction userid,fixid,team1goals, team2goals where fixid = 1 and loop this for, say, 10 fixtures....

    What would be the best approach for grouping the variables so I can achieve this ?

    Should I change my original form ? Can I incorporate the fixtures array in the hidden field ? Can I group or make an array from the values in the $_GET array ? so many questions!

    Many thanks in advance for any help,


  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    0 Post(s)
    0 Thread(s)
    Be aware I don't really know what a fixture or fixid is, and have no idea what a prediciton league is, so I don't really understand your problem fully.

    But I think you're trying to display an html form, and let a user enter multiple sets of data at a time, instead of having to submit the form many times for each set of data?

    You would probably want something like this
    <input name="groups[555][team1goal]">
    <input name="groups[555][team2goal]">
    <input name="groups[556][team1goal]">
    <input name="groups[556][team2goal]">
    <input name="groups[557][team1goal]">
    <input name="groups[557][team2goal]">
    The 555 number would be some peice of data that associates the team1 and team2 together, which I think is your fixid.

    To receive the data
    PHP Code:
    foreach ($_POST['groups'] as $fixid => $data_associated_to_fixid) {
    "$fixid$data_associated_to_fixid[team1goal] $data_associated_to_fixid[team2goal]\n\n";

    As for the sql, again due to me not understanding what you're doing, I'm not really sure. Are there more than 2 different teams( teams?) How would you know which team is team 1, and which is team 2? Is that info related to fixid?

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2006
    0 Post(s)
    0 Thread(s)

    Thanks for the reply and I think you may be on to something here....I've been looking at this for a couple of days and i think its prob a simple answer but just needs fresh eyes, and yes it is exactly a football teams prediction league.

    My prediction league system allows users to log in and predict scores between two teams in many rounds of fixtures/games. These predictions are logged and compared against the actual results of the games, points awarded, and the user table is then updated to display the user with the most points.

    So I've got 10 teams and they each play each other once. I've got a table for Users, Teams, Fixtures, Predicitions and Results. They are related in that

    Teams have TeamID,TeamName,

    Fixtures have FixID,Fix_Team1,Fix_Team2,Round

    Fixtures.Fix_Team1 = Teams.TeamID
    Fixtures.Fix_Team2 = Teams.TeamID

    For Round 1 of fixtures I've got
    Team 1 vs Team Team 9 vs Team 10
    My HTML form will display a form containing the 5 Fixtures of round 1
    The user will see a form with 5 fixtures(games) and they will enter goals for Team 1 vs Team 2 up to goals between Team 9 vs Team 10

    The form is submitted once for this round of fixtures. The data submitted will contain 5 different fixture id's and 5 predicitons like below with 10 user inputted values for the goals column.

    Fix id Team 1 Id Team 1 Goals Teams 2 Goals Team 2 Id
    1 1 2(user input value) 3(user input value) 2

    up to the 5th fixture

    I cannot figure out the best way to either post the data and/or handle the submitted data.

    I want to be able to insert each prediction into the prediction table
    My prediction table will contain the following columns

    Prediciton ID User ID FixID Team1Goals Team2Goals

    where Prediction ID is an auto increment ID, User ID is got from the session variable and the other values got from the submitted form.

    I hope this makes it a bit clearer and thanks for the reply again. Its late at night now so I'm going to give your reply a go tomorrow.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts