tebrown Posted September 23, 2012 Share Posted September 23, 2012 Hello, I'm working on this feature where the user fills out a 'match report' which is used to set the amount of goal scorers in a game. At the moment i have set up a jQuery form which the manager can click the '+' button to add another form select dropdown depending on the amount of scorers within the game. The players who have scored are then stored in an array once submitted. What i would like to do now is assign a variable to each of these players so i can then insert them into the database. What im having trouble with is that what if there are only 4 scorers. How do i assign empty values to the other 3? The columns in the database structure is set out like this, so ideally the players that the manager selects would go into the slots accordingly. goal_scorer_1, goal_scorer_2, goal_scorer_3, goal_scorer_4, goal_scorer_5, goal_scorer_6, goal_scorer_7. This is what i have got so far: $goal_scorers = $_POST['items']; $scorer_1 = $goal_scorers[0]; $scorer_2 = $goal_scorers[1]; $scorer_3 = $goal_scorers[2]; $scorer_4 = $goal_scorers[3]; $scorer_5 = $goal_scorers[4]; $scorer_6 = $goal_scorers[5]; $scorer_7 = $goal_scorers[6]; So if the manager only selects 2 players who have scored, how do i make the other 5 slots be empty so that this notice doesn't happen: Notice: Undefined offset. Any help would be much appreciated. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/268693-assigning-variable-to-each-player-inside-array/ Share on other sites More sharing options...
Christian F. Posted September 23, 2012 Share Posted September 23, 2012 Don't do this, use the arrays instead. Much better solution, as this is what arrays were made for, and you already have them made. What you've done here is adding artificial limits, and requirements, on a situation which is by nature completely dynamic. You should read up on arrays in PHP, as well as the control structures to loop through their values. Also, if you have multiple fields in your table named "goal_scorer_#" then you really need to redesign your database tables as well. Move the goal_scoring into its own table, and reference the match and user ID inside it. Classic many-to-many relation, in other words. The PHP manual's getting started page, or the language reference, would perhaps be the best places to start for you at this point. Quote Link to comment https://forums.phpfreaks.com/topic/268693-assigning-variable-to-each-player-inside-array/#findComment-1380270 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.