Tournament algorithm in php - php

I need some help. I want to create one tournament. Let's say I have 6 players. 1 2 3 4 5 6
I want to create some.. lets' say stages... Every player will play 5 matches(number of players - 1), in 5 different stages. In one stage, all the players must appear only once.
For example, with 6 players I want to generate these results:
Squad 1:
1-2
3-4
5-6
Squad 2:
1-3
2-5
4-6
Squad 3:
1-4
2-6
3-5
Squad 4:
1-5
2-4
3-6
Squad 5:
1-6
2-3
4-5
So, in every stage, the matches must be unique, and every player must play with every player.
I want one algorithm that will work even if I want 8 players, or 12, or 16, or 28.
Thanks

<?php
$numplayers = 6;
if ($numplayers % 2 != 0) $numplayers++; // Dummy
for ($round = 0;$round < $numplayers - 1;$round++) {
echo 'Squad ' . ($round+1) . ":\n\n1-";
for ($i = 0;$i < $numplayers-1;$i++) {
if ($i % 2 == 0) {
$player = ($numplayers-2) - ($i/2) - $round;
} else {
$player = ((($i-1)/2) - $round);
}
if ($player < 0) $player += $numplayers - 1;
echo ($player+2);
echo ($i % 2 == 0) ? "\n" : '-';
}
echo "\n\n";
}

Related

divide samples (numbers) into tests (in the form of intervals)

I am trying to divide samples (numbers) into tests (in the form of intervals). But my results are not correct as per the below required result. The code below is working fine till samples of 20 or less than 20. But if more than 20 samples, the results are wired.
The result should come with this case :
If 19 samples - 2 tests: 1-10, 11-19
If 20 samples - 2 tests: 1-10, 11-20
If 21 samples - 3 tests: 1-10, 11-16, 17-21
If 23 samples - 3 tests: 1-10, 11-17, 18-23
If 24 samples - 3 tests: 1-10, 11-17, 18-24
If 31 samples - 4 tests: 1-10, 11-20, 21–26, 27-31.
<?php
$samples = 23;
$tests = 1;
if($samples > 10) {
$tests = ceil($samples/10);
}
echo $tests." tests\n";
$s = 1
for ($x = 0; $x < $tests; $x++) {
if($samples > 20) {
$c = $s.'-'.ceil($samples/$tests);
}
if($x > 0) {
$c = (ceil($samples/$tests)+1).'-'.$samples;
}
echo $c."\n";
}
?>
Result coming which is wrong:
3 tests : 1-8, 9-23, 9-23

PHP - Skip multiple for loop iterations

I have this piece of code that goes through an array where each position contains a line of previously pasted text. I want the for loop to skip 2 times the number of columns input by the user, once it reaches a line that contains the word "Total". I've searched around but all I found were answers for other languages. Can anybody enlighten me on this?
Code:
1 $j = -1;
2 $step = 1;
3 for($i = 0; $i < count($statisticsinput); $i++){
4 if(strpos($statisticsinput[$i], "Total") !== false){
5 $i += 2*$_POST['columno']; //Trying to make the counter skip the 2*col iterations but seems to have no effect
6 if($step !== 2){ //The word Total appears 2 times in the pasted text, the first time
7 $step++; //should keep the script going but not the second one
8 }else{
9 break;
10 }
11 continue; //After incrementing the counter 2*col, skip over the next steps and
12 } //go to the next loop. I expected it to jump 2*col loops (usually 22)
13 if($i % $_POST['columno'] == 0){
14 $j++;
15 }
16 $employees[$j][] = $statisticsinput[$i];
17 }
Many thanks.

Divide an amout equally beteen no of user

I want to distribute an amount among some users equally.If amount is not divisible equally then all other member will get equal amount expect the last member who will get rest of the money.Below is what i have tried
$number = $_POST['number'];
$noOfTime = $_POST['no_of_time'];
$perHead = ceil($number / $noOfTime);
for ($i = 1; $i <= $noOfTime; $i++) {
if ($i == $noOfTime) {
echo $perHead * $noOfTime - $number;
} else {
echo $perHead;
}
}
Here if number is 7 and member are 4 the first 3 member will the 2 and the last will get 1. Like 2,2,2,1.
But this logic seems to be not working for all cases.
Please help.Thanks.
I think it should help you.
$no = 22;
$users = 8;
// count from 0 to $users number
for ($i=0;$i<$users;$i++)
// if the counting reaches the last user AND $no/$users rests other than 0...
if ($i == $users-1 && $no % $users !== 0) {
// do the math rounding fractions down with floor and add the rest!
echo floor($no / $users) + ($no % $users);
} else {
// else, just do the math and round it down.
echo floor($no / $users)." ";
}
OUTPUTS:
2 2 2 2 2 2 2 8
EDIT: I nested an if verification so the logic won't fail even if users are 1 or 2. And since it received more upvotes, I commented the code to make it more clear.

PHP Counting Negative Numbers and Bold Only Even Numbers

On a form, I used PHP to display every number from 1 to the entered number. For example, if I enter 10 on the form, it displays 1 2 3 4 5 6 7 8 9 10. Now, I want it to be able to handle negative numbers by counting up to 0 (-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0) and make every even number in the results bold, e.g., 2 4 6 8 10, etc.). I've searched exhaustively for the answers without any luck. How would any of you suggest doing this? My code for the first part is displayed below. Thank you in advance.
<?php
$num = $_POST['num'];
$limit = $_POST['num'];
echo "<pre>";
do {
echo ($counter).'<br>';
$counter++;
} while ($counter <= $limit);
echo "<pre>";
?>
You can do this
<?php
$num = $_POST['num'];
$limit = $_POST['num'];
echo "<pre>";
do {
if( $counter % 2 == 0 )
{
echo "<strong>" . $counter . "</strong><br />";
}
else
{
echo ($counter).'<br>';
}
$counter++;
} while ($counter <= $limit);
echo "<pre>";
?>
Solution 1
Ok, some more information about the %. This is a modulo. It gives you back the remaining number if you divide it by the mod number. For example
0 % 2 = 0
1 % 2 = 1
2 % 2 = 0
3 % 2 = 1 this because 3 / 2 = 1 and a bit,
you can't divide the last 1 completely by 2. So remain 1
4 % 2 = 0
Solution 2
Like crush said, you can use $counter & 1. What does this do?
If you look at a number bitwise. You want to AND it wit 1.
Bitwise number 2 = 0010 AND it with 0001 and your return will be 0000 (zero).
Bitwise number 3 = 0011 AND it with 0001 and your result will be 0001 (one).
If you ceep that going and only check the last bit, you can see if it is a even number. More about bitwise operations.

Add Ellipsis in PHP Pagination

.
//Prev
.
for($number = 1; $number <= $num_pages; $number++)
{
if($page == $number)
{
$navigator .= "<b>[$number]</b> ";
}
else
{
$navigator .= "<a href='?c=".$_SESSION['cID']".&rows=".$per_page."&page=$number'>$number</a> ";
}
}
.
//Next
.
This is the snippet that prints number of pages.
Sample output:
Previous 1 2 3 4 [5] 6 7 8 9 10 Next
5 is the current page.
Problem: page numbers are shown in sequence with no restrictions. If i have 100 pages, all numbers show up.
Question: I need my paging numbers appear as the following...
Assume we only have 7 ($num_pages) pages:
Previous 1 2 [3] 4 5 6 7 Next
Assume we have 90 pages:
[1] 2 3 4 5 6 7 ... 90 Next
Assume user clicked the 7th page:
Previous 1 ... 5 6 [7] 8 9 10 11 ... 90 Next
Assume user clicked 11th page:
Previous 1 ... 9 10 [11] 12 13 14 15 ... 90 Next
Assume user clicked 15th page:
Previous 1 ... 13 14 [15] 16 17 18 19 ... 90 Next
Assume user clicked 90th page:
Previous 1 ... 84 85 86 87 88 89 [90]
Any help will be appreciated.
$radius = 3;
for($i = 1; $i <= $total; $i++){
if(($i >= 1 && $i <= $radius) || ($i > $current - $radius && $i < $current + $radius) || ($i <= $total && $i > $total - $radius)){
if($i == $current) echo "<b>".$i."</b>";
}
elseif($i == $current - $radius || $i == $current + $radius) {
echo "... ";
}
}
This should be more than enough to get you started at least
$count = 7; // number to show
// start at half threshold down from the current location.
$number = $current - round($count/2);
if( $number > 1 ) echo '...';
else $ // increase to have number start at 1.
for( $number; $number < $number + $count; $number++)
{
// your for loop as normal
}
if( $number < $total ) echo '...';
An elegant solution for this kind of thing is to use "logarithmic page navigation". See my answer to this question (PHP code included):
How to do page navigation for many, many pages? Logarithmic page navigation

Categories