Jump to content

Assigning Variable To Each Player Inside Array


tebrown

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.