Jump to content

Recommended Posts

I am using this code for to udpate a database that holds stats for my hockey league.  This particular section updates the penalty minutes for a given players.  I get these values by inputting them into a form and then using a script that updates the database.  The penalty section is the difficult one for me because I in one textarea I input the players jersey number and then in onother textarea I input the corresponding penalty minutes for that player.  So I get the values from the form and then convert it them to arrays with the explode function. 

 

$array_homepenalties is the array containing the player's numbers that received penalties

$array_homepenmins is the array containing the corresponding penalty minutes

 

So $array_homepenalties[1] is the player number and the number of minutes he received is $array_homepenmins[1].  And it continues this way for each entire array. 

 

An example would be: $array_homepenalties = (3, 5, 8, 11) and $array_homepenmins = (2, 4, 5, 2).

 

So this tells me that player #3 received 2 minutes in penalties for this game, #5 got 4 minutes, #8 got 5 minutes, and #11 got 2 mintues.

 

Here is my code for it:

 

//UPDATE Penalty Minutes										

foreach ($array_homepenalties as $ahp)	{
                                                                $sql_getplayer_pens = "SELECT penaltymins FROM playerstats WHERE jerseynumber = '$ahp' AND teamname = '$hometeam'";
				$result_getplayer_pens = mysql_query($sql_getplayer_pens);

				foreach ($array_homepenmins as $ahpm)	{

																			                                                    $new_pentotal = $result_getplayer_pens + $ahpm;

																			                                                    $sql_updateplayer_pens = "UPDATE playerstats SET penaltymins = '$new_pentotal' WHERE jerseynumber = '$ahp' AND teamname = '$hometeam'";
																			                                                    $result_updateplayer_pens = mysql_query($sql_updateplayer_pens);

																			                                                     break;

																			                                                      }
		}

 

So this is the only way that I could come up with to be able to link the two arrays in the way I need.  But I am new to php and so I'm not sure if what will happen when I break out of the second foreach.  I know that it will go back to the top first foreach loop and go to the 2nd element in $array_homepenalties but I don't know if when it reaches the second foreach loop if it will iterate the second element in $array_homepens because of the break function. 

 

Can someone tell me if this will work the way I have it or if there is an easier way to do the same thing?  I had thought that there might be a way to do it with muti-dimensional arrays but couldn't figure it out so I came up with this.  Thanks for the help.

 

Nick

Link to comment
https://forums.phpfreaks.com/topic/63362-solved-nested-foreach-loopwill-this-work/
Share on other sites

Sorry, it didn't look that way when I preveiwed it.

 

foreach ($array_homepenalties as $ahp)	{

$sql_getplayer_pens = "SELECT penaltymins FROM playerstats WHERE jerseynumber = '$ahp' AND teamname = '$hometeam'";
$result_getplayer_pens = mysql_query($sql_getplayer_pens);

foreach ($array_homepenmins as $ahpm)	{


$new_pentotal = $result_getplayer_pens + $ahpm;


$sql_updateplayer_pens = "UPDATE playerstats SET penaltymins = '$new_pentotal' WHERE jerseynumber = '$ahp' AND teamname = '$hometeam'";
$result_updateplayer_pens = mysql_query($sql_updateplayer_pens);

break;

																			}
}

$new_pentotal = $result_getplayer_pens + $ahpm;

that line returns u nothing, as $result_getplayer_pens is just the mysql_query. To retrieve the data u must use mysq_fetch_array within a loop. Smth like:

 

while($values_player_pens = mysql_fetch_array($result_getplayer_pens)){
        $new_pentotal = $values_player_pens['columnValue'] + $ahpm;
        //update query and stuff
}

Well you're dealing with parallel arrays... which can be messy so be careful with them. They are just hard to always keep in sync. But anyways, you shouldn't need the enter loop at all. By looping through one you have the corresponding values in the other, again because they are parallel arrays.

 

$len = count($array_homepenalties);
for($i = 0; $i  < $len; ++$i)	
{
    $sql_getplayer_pens = "SELECT penaltymins FROM playerstats WHERE jerseynumber = '" . $array_homepenalties[$i] . "' AND teamname = '$hometeam'";
    $result_getplayer_pens = mysql_query($sql_getplayer_pens);

    $new_pentotal = $result_getplayer_pens + $array_homepenmins[$i];

    $sql_updateplayer_pens = "UPDATE playerstats SET penaltymins = '$new_pentotal' WHERE jerseynumber = '" . $array_homepenalties[$i] . "' AND teamname = '$hometeam'";
    $result_updateplayer_pens = mysql_query($sql_updateplayer_pens);																		
}

 

It's very likely that I botched your variable names but hopefully you get the point.

A better array would be one where the player number is the key and the value is the pen mins.

 

Also there is no need to select a record to update it.

 

So you could then have

<?php

foreach ($array as $ahp => $ahpm)
{
        mysql_query ("UPDATE playerstats SET 
               penaltymins = penaltymins + $ahpm
           WHERE jerseynumber = '$ahp' AND teamname = '$hometeam'  ");
}

That seems way easier.  Thanks Barand.  As I said I am new to php so I have another question as I haven't worked at all with assoc. arrays.  How do you take 2 arrays and make one assoc. array like to one you talked about above?

 

Nick

Hehe yeah that's why I didn't want to suggest that way :P Think of it as key and value pars. Here's the easiest example.

 

$hash = Array();

$hash['first'] = "Nick";

$hash['middle'] = "Nicholson";

$hash['last'] = "Nichols";

 

$name = $hash['first'] + " " + $hash['middle'] + " " + $hash['last'];

 

From there you can get more complicated.

 

$hash = Array();

$hash['name1'] = Array('Nick', 'Nicholson', 'Nichols');

$hash['name2'] = Array('Bob', 'Bobbert', 'Bobbertson');

 

$cool_name = $hash['name2'][0] + " " + $hash['name2'][1] + " " + $hash['name2'][2];

 

I hope some of this gets in in the right direction.

 

I get the values from a form and then use explode to turn the values into an array.  So for each game there will be a different number of players in the array but the 2 arrays will always have the count value.

 

I meant to say at the bottom there "the 2 arrays will always have the same count value."

 

Nick

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.