Jump to content

Recommended Posts

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.

 

 

post-128420-0-16926600-1348527045_thumb.png

 

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

Link to comment
https://forums.phpfreaks.com/topic/268761-php-combine-the-arrays/
Share on other sites

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

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>';
?>

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

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

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.
?>

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

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

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

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.