tebrown Posted September 24, 2012 Share Posted September 24, 2012 Hey Guys, Yesterday i had got this feature to a working standard but the problem that i had that it wasn't very good in terms of usability. Originally, the manager would have the option to click a '+' button in which it would create a drop-dow list. This would allow the user to pick the players who have scored during a match. If there had been a player who had scored 3 times, the user would have to click this 3 times and select the same player each time. I have now changed it so that when the user clicks the + button, it will show a drop-down to select the player and another drop-down next to it, which will allow the user to select the number of tries they have scored. * See attachment for image. This is the form HTML: (There are 2 arrays, items[] and count[]). <select name="items[]" style="width: 200px;"><option value="">Select Player</option><option value="Tim Brown">Tim Brown</option><option value="Bobby Duppree">Bobby Duppree</option><option value="Joe Bloggs">Joe Bloggs</option></select><select name="count[]" style="margin-left:10px;width: 30px;"><option value="">1</option><option value="(2)">2</option><option value="(3)">3</option></select> Ideally I would like to do 2 things from this form. 1) Get the players who have scored and display them. For example, if Tim Brown scores 1 try and Bobby Dupree scores 3 tries it will show like this: Tim Brown, Bobby Dupree (3) This result would be a variable so then i can insert it into the database like it shows above. 2) The second thing i would like to do is update the players statistic 'tries' in the database. This will depend on how much the user sets next to their name. Going of the example above, Tim Brown would increment +1 and Bobby Dupree would increment +3. Yes, i have tried to do this and have got the following, but it defiantly not showing what i want. $fixture = mysql_real_escape_string($_REQUEST['fixture']); $player = $_POST['items']; $count = $_POST['count']; $new_array = array(); foreach ($player as $key => $value) { if(isset($new_array[$value])) $new_array[$value] += $_POST['count'][$key]; else $new_array[$value] = $_POST['count'][$key]; } $column = ""; foreach($new_array as $name => $count) { $column .= $name.($count > 1 ? " (".$count."), " : ""); } $column = trim($column); echo $column; $update_fixture = mysql_query("UPDATE `fixtures` SET `tries` = '$column' WHERE id = '$fixture'") or die(mysql_error()); foreach($player as $scorer) { mysql_query("UPDATE `users` SET `tries` = `tries` +1 WHERE `name` = '{$scorer}' ") or die(mysql_error()); Any help on this would be much appreciated. Thanks Heaps Guys! Cheers Tim Quote Link to comment https://forums.phpfreaks.com/topic/268761-php-combine-the-arrays/ Share on other sites More sharing options...
darkfreaks Posted September 24, 2012 Share Posted September 24, 2012 http://php.net/array_combine Quote Link to comment https://forums.phpfreaks.com/topic/268761-php-combine-the-arrays/#findComment-1380705 Share on other sites More sharing options...
tebrown Posted September 24, 2012 Author Share Posted September 24, 2012 Hey darkfreaks, I did try that array_combine but couldn't get it work properly either. For example, $player = array("Tim","Joe","Ben"); // The players who scored. $count = array("1","2","2"); // The number of trys the player had scored. $combine = array_combine($player, $count); $result = implode($combine); echo $result; The will output the following: 122 Need it to be exactly like below (as you can see there is no 1 next to Tim, this because he only got the 1 try) Tim, Joe (2), Ben (2) Cheers Quote Link to comment https://forums.phpfreaks.com/topic/268761-php-combine-the-arrays/#findComment-1380710 Share on other sites More sharing options...
jcbones Posted September 24, 2012 Share Posted September 24, 2012 Run this: <?php $player = array("Tim","Joe","Ben"); // The players who scored. $count = array("1","2","2"); // The number of trys the player had scored. $combine = array_combine($player, $count); $result = implode($combine); echo '<pre>' . print_r($combine,true) . '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/268761-php-combine-the-arrays/#findComment-1380712 Share on other sites More sharing options...
darkfreaks Posted September 24, 2012 Share Posted September 24, 2012 if you don't want it to rewrite the array data you can do something like $combine = $player+$count; $result= implode($combine); Quote Link to comment https://forums.phpfreaks.com/topic/268761-php-combine-the-arrays/#findComment-1380713 Share on other sites More sharing options...
tebrown Posted September 24, 2012 Author Share Posted September 24, 2012 Thanks for the replies, I tried the following: $player = array("Tim","Joe","Ben"); // The players who scored. $count = array("1","2","2"); // The number of trys the player had scored. $combine = array_combine($player, $count); $combine = $player+$count; $result= implode(", ", $combine); echo $result; It only outputs the name though like this: Tim, Joe, Ben Cheers Tim Quote Link to comment https://forums.phpfreaks.com/topic/268761-php-combine-the-arrays/#findComment-1380714 Share on other sites More sharing options...
darkfreaks Posted September 24, 2012 Share Posted September 24, 2012 why do you have combine twice? do it his way first tell us what it outputs then do it my way tell us what it outputs. not both. Quote Link to comment https://forums.phpfreaks.com/topic/268761-php-combine-the-arrays/#findComment-1380715 Share on other sites More sharing options...
tebrown Posted September 24, 2012 Author Share Posted September 24, 2012 Ok i did it jcbones way and it outputs this: Array ( [Tim] => 1 [Joe] => 2 [ben] => 2 ) Your way, darkfreaks, outputs this: $player = array("Tim","Joe","Ben"); // The players who scored. $count = array("1","2","2"); // The number of trys the player had scored. $combine = $player+$count; $result= implode(", ", $combine); echo $result; Tim, Joe, Ben Cheers Quote Link to comment https://forums.phpfreaks.com/topic/268761-php-combine-the-arrays/#findComment-1380717 Share on other sites More sharing options...
jcbones Posted September 24, 2012 Share Posted September 24, 2012 I was simply showing how the array was built, and that you couldn't just output an implode to get your desired results. You should be looking at something along the lines of: <?php $player = array("Tim","Joe","Ben"); // The players who scored. $count = array("1","2","2"); // The number of trys the player had scored. $combine = array_combine($player, $count);//combine players to scores. foreach($combine as $names => $count) { //loop through the combined array. $store[] = $names . (($count > 1) ? '(' . $count . ')' : NULL ) . ' '; //optionally you could echo this line, but I stored it so I could add a comma with implode. } $result = implode(', ',$store); //implode the store array, using a comma and space as the glue. echo $result; //send it to the page. ?> Quote Link to comment https://forums.phpfreaks.com/topic/268761-php-combine-the-arrays/#findComment-1380718 Share on other sites More sharing options...
darkfreaks Posted September 24, 2012 Share Posted September 24, 2012 i don't see the output but it looks like his works fine. it does combine both array properly. Quote Link to comment https://forums.phpfreaks.com/topic/268761-php-combine-the-arrays/#findComment-1380719 Share on other sites More sharing options...
tebrown Posted September 24, 2012 Author Share Posted September 24, 2012 jcbones, darkfreaks thankyou! Ok so this will now be updated into the fixtures table. For the second part, how would i do the update for the players statistic 'tries'. So depending on how many tries they score it will update their try count. Cheers Tim Quote Link to comment https://forums.phpfreaks.com/topic/268761-php-combine-the-arrays/#findComment-1380720 Share on other sites More sharing options...
tebrown Posted September 25, 2012 Author Share Posted September 25, 2012 I did originally have this: foreach($players as $scorer) { mysql_query("UPDATE `users` SET `tries` = `tries` +1 WHERE `name` = '{$scorer}' ") or die(mysql_error()); } I think this would work, but it would only select the player and not the amount of tries they scored. Using the example above, ideally Tim would increment by +1, Joe +2, and Ben +2. At the moment, using the code above would only update each player +1. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/268761-php-combine-the-arrays/#findComment-1380721 Share on other sites More sharing options...
tebrown Posted September 25, 2012 Author Share Posted September 25, 2012 Think I have got it. foreach($combine as $names => $count) { mysql_query("UPDATE `users` SET `tries` = `tries` +'$count' WHERE `name` = '{$names}' ") or die(mysql_error()); } If you think theres some improvement on this let me know. Cheers Tim Quote Link to comment https://forums.phpfreaks.com/topic/268761-php-combine-the-arrays/#findComment-1380724 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.