?
Solved

PHP Mysql fixture generator

Posted on 2008-11-18
6
Medium Priority
?
2,630 Views
Last Modified: 2013-12-13
I have a php code that generates soccer fixtures, but I have to manually enter teams in to a text area to generate this fixtures, I have all the teams in database but don't know how to pull them in to this text area and generate the fixtures, please help!       working code below
<?php mysql_select_db($database_php, $php);
$query_Recordset1 = "SELECT * FROM tbl_team";
$Recordset1 = mysql_query($query_Recordset1, $php) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
 
 
Manual fixture generator code....
 
<?php
    // Find out how many teams we want fixtures for.
    if (! isset($_GET['teams']) && ! isset($_GET['names'])) {
        print get_form();
    } else {
        # XXX check for int
        print show_fixtures(isset($_GET['teams']) ?  nums(intval($_GET['teams'])) : explode("\n", trim($_GET['names'])));
    }
}
 
function nums($n) {
    $ns = array();
    for ($i = 1; $i <= $n; $i++) {
        $ns[] = $i;
    }
    return $ns;
}
 
function show_fixtures($names) {
    $teams = sizeof($names);
 
    print "<p>Fixtures for $teams teams.</p>";
 
    // If odd number of teams add a "ghost".
    $ghost = false;
    if ($teams % 2 == 1) {
        $teams++;
        $ghost = true;
    }
   
    // Generate the fixtures using the cyclic algorithm.
    $totalRounds = $teams - 1;
    $matchesPerRound = $teams / 2;
    $rounds = array();
    for ($i = 0; $i < $totalRounds; $i++) {
        $rounds[$i] = array();
    }
   
    for ($round = 0; $round < $totalRounds; $round++) {
        for ($match = 0; $match < $matchesPerRound; $match++) {
            $home = ($round + $match) % ($teams - 1);
            $away = ($teams - 1 - $match + $round) % ($teams - 1);
            // Last team stays in the same place while the others
            // rotate around it.
            if ($match == 0) {
                $away = $teams - 1;
            }
            $rounds[$round][$match] = team_name($home + 1, $names)
                . " v " . team_name($away + 1, $names);
        }
    }
 
    // Interleave so that home and away games are fairly evenly dispersed.
    $interleaved = array();
    for ($i = 0; $i < $totalRounds; $i++) {
        $interleaved[$i] = array();
    }
   
    $evn = 0;
    $odd = ($teams / 2);
    for ($i = 0; $i < sizeof($rounds); $i++) {
        if ($i % 2 == 0) {
            $interleaved[$i] = $rounds[$evn++];
        } else {
            $interleaved[$i] = $rounds[$odd++];
        }
    }
 
    $rounds = $interleaved;
 
    // Last team can't be away for every game so flip them
    // to home on odd rounds.
    for ($round = 0; $round < sizeof($rounds); $round++) {
        if ($round % 2 == 1) {
            $rounds[$round][0] = flip($rounds[$round][0]);
        }
    }
   
    // Display the fixtures        
    for ($i = 0; $i < sizeof($rounds); $i++) {
        print "<p>Round " . ($i + 1) . "</p>\n";
        foreach ($rounds[$i] as $r) {
            print $r . "<br />";
        }
        print "<br />";
    }
    print "<p>Second half is mirror of first half</p>";
    $round_counter = sizeof($rounds) + 1;
    for ($i = sizeof($rounds) - 1; $i >= 0; $i--) {
        print "<p>Round " . $round_counter . "</p>\n";
        $round_counter += 1;
        foreach ($rounds[$i] as $r) {
            print flip($r) . "<br />";
        }
        print "<br />";
    }
    print "<br />";
 
    if ($ghost) {
        print "Matches against team " . $teams . " are byes.";
    }
}
 
function flip($match) {
    $components = split(' v ', $match);
    return $components[1] . " v " . $components[0];
}
 
function team_name($num, $names) {
    $i = $num - 1;
    if (sizeof($names) > $i && strlen(trim($names[$i])) > 0) {
        return trim($names[$i]);
    } else {
        return $num;
    }
}
 
function get_form() {
    $s = '';
    $s = '<p>Enter number of teams OR team names</p>' . "\n";
    $s .= '<form action="' . $_SERVER['SCRIPT_NAME'] . '">' . "\n";
    $s .= '<label for="teams">Number of Teams</label><input type="text" name="teams" />' . "\n";
    $s .= '<input type="submit" value="Generate Fixtures" />' . "\n";
    $s .= '</form>' . "\n";
 
    $s .= '<form action="' . $_SERVER['SCRIPT_NAME'] . '">' . "\n";
    $s .= '<div><strong>OR</strong></div>' . "\n";
    $s .= '<label for="names">Names of Teams (one per line)</label>'
        . '<textarea name="names" rows="8" cols="40"></textarea>' . "\n";
    $s .= '<input type="submit" value="Generate Fixtures" />' . "\n";
    $s .= "</form>\n";
    return $s;
}
 
main();
 
?>

Open in new window

0
Comment
Question by:sinanosan
  • 3
  • 3
6 Comments
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 22992093
foreach loop records
<?php 
mysql_select_db($database_php, $php);
$query_Recordset1 = "SELECT * FROM tbl_team";
$Recordset1 = mysql_query($query_Recordset1, $php) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
 
 
Manual fixture generator code....
 
<?php
    // Find out how many teams we want fixtures for.
    if (! isset($_GET['teams[]']) && ! isset($_GET['names'])) {
        print get_form();
    } else {
        # XXX check for int
        print show_fixtures(isset($_GET['teams']) ?  nums(intval($_GET['teams'])) : explode("\n", trim($_GET['names'])));
    }
}
 
function nums($n) {
    $ns = array();
    for ($i = 1; $i <= $n; $i++) {
        $ns[] = $i;
    }
    return $ns;
}
 
function show_fixtures($names) {
    $teams = sizeof($names);
 
    print "<p>Fixtures for $teams teams.</p>";
 
    // If odd number of teams add a "ghost".
    $ghost = false;
    if ($teams % 2 == 1) {
        $teams++;
        $ghost = true;
    }
   
    // Generate the fixtures using the cyclic algorithm.
    $totalRounds = $teams - 1;
    $matchesPerRound = $teams / 2;
    $rounds = array();
    for ($i = 0; $i < $totalRounds; $i++) {
        $rounds[$i] = array();
    }
   
    for ($round = 0; $round < $totalRounds; $round++) {
        for ($match = 0; $match < $matchesPerRound; $match++) {
            $home = ($round + $match) % ($teams - 1);
            $away = ($teams - 1 - $match + $round) % ($teams - 1);
            // Last team stays in the same place while the others
            // rotate around it.
            if ($match == 0) {
                $away = $teams - 1;
            }
            $rounds[$round][$match] = team_name($home + 1, $names)
                . " v " . team_name($away + 1, $names);
        }
    }
 
    // Interleave so that home and away games are fairly evenly dispersed.
    $interleaved = array();
    for ($i = 0; $i < $totalRounds; $i++) {
        $interleaved[$i] = array();
    }
   
    $evn = 0;
    $odd = ($teams / 2);
    for ($i = 0; $i < sizeof($rounds); $i++) {
        if ($i % 2 == 0) {
            $interleaved[$i] = $rounds[$evn++];
        } else {
            $interleaved[$i] = $rounds[$odd++];
        }
    }
 
    $rounds = $interleaved;
 
    // Last team can't be away for every game so flip them
    // to home on odd rounds.
    for ($round = 0; $round < sizeof($rounds); $round++) {
        if ($round % 2 == 1) {
            $rounds[$round][0] = flip($rounds[$round][0]);
        }
    }
   
    // Display the fixtures        
    for ($i = 0; $i < sizeof($rounds); $i++) {
        print "<p>Round " . ($i + 1) . "</p>\n";
        foreach ($rounds[$i] as $r) {
            print $r . "<br />";
        }
        print "<br />";
    }
    print "<p>Second half is mirror of first half</p>";
    $round_counter = sizeof($rounds) + 1;
    for ($i = sizeof($rounds) - 1; $i >= 0; $i--) {
        print "<p>Round " . $round_counter . "</p>\n";
        $round_counter += 1;
        foreach ($rounds[$i] as $r) {
            print flip($r) . "<br />";
        }
        print "<br />";
    }
    print "<br />";
 
    if ($ghost) {
        print "Matches against team " . $teams . " are byes.";
    }
}
 
function flip($match) {
    $components = split(' v ', $match);
    return $components[1] . " v " . $components[0];
}
 
function team_name($num, $names) {
    $i = $num - 1;
    if (sizeof($names) > $i && strlen(trim($names[$i])) > 0) {
        return trim($names[$i]);
    } else {
        return $num;
    }
}
 
function get_form() {
    $s = '';
    $s = '<p>Enter number of teams OR team names</p>' . "\n";
    $s .= '<form action="' . $_SERVER['SCRIPT_NAME'] . '">' . "\n";
    $s .= '<label for="teams">Number of Teams</label>';
 foreach ($row_Recordset1 as $key => $value)
 {
    $s .= $value . '<input type="checkbox" name="teams[]" value="' . $value . '" /><br />'
 }
    $s .= '<input type="submit" value="Generate Fixtures" />' . "\n";
    $s .= '</form>' . "\n";
 
    $s .= '<form action="' . $_SERVER['SCRIPT_NAME'] . '">' . "\n";
    $s .= '<div><strong>OR</strong></div>' . "\n";
    $s .= '<label for="names">Names of Teams (one per line)</label>'
        . '<textarea name="names" rows="8" cols="40"></textarea>' . "\n";
    $s .= '<input type="submit" value="Generate Fixtures" />' . "\n";
    $s .= "</form>\n";
    return $s;
}
 
main();
 
?>

Open in new window

0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 22992142
Correction :)
USE THIS
<?php 
mysql_select_db($database_php, $php);
$query_Recordset1 = "SELECT * FROM tbl_team";
$Recordset1 = mysql_query($query_Recordset1, $php) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
 
 
Manual fixture generator code....
 
<?php
    // Find out how many teams we want fixtures for.
    if (! isset($_GET['teams']) && ! isset($_GET['names[]'])) {
        print get_form();
    } else {
        # XXX check for int
        print show_fixtures(isset($_GET['teams']) ?  nums(intval($_GET['teams'])) : explode("\n", trim($_GET['names[]'])));
    }
}
 
function nums($n) {
    $ns = array();
    for ($i = 1; $i <= $n; $i++) {
        $ns[] = $i;
    }
    return $ns;
}
 
function show_fixtures($names) {
    $teams = sizeof($names);
 
    print "<p>Fixtures for $teams teams.</p>";
 
    // If odd number of teams add a "ghost".
    $ghost = false;
    if ($teams % 2 == 1) {
        $teams++;
        $ghost = true;
    }
   
    // Generate the fixtures using the cyclic algorithm.
    $totalRounds = $teams - 1;
    $matchesPerRound = $teams / 2;
    $rounds = array();
    for ($i = 0; $i < $totalRounds; $i++) {
        $rounds[$i] = array();
    }
   
    for ($round = 0; $round < $totalRounds; $round++) {
        for ($match = 0; $match < $matchesPerRound; $match++) {
            $home = ($round + $match) % ($teams - 1);
            $away = ($teams - 1 - $match + $round) % ($teams - 1);
            // Last team stays in the same place while the others
            // rotate around it.
            if ($match == 0) {
                $away = $teams - 1;
            }
            $rounds[$round][$match] = team_name($home + 1, $names)
                . " v " . team_name($away + 1, $names);
        }
    }
 
    // Interleave so that home and away games are fairly evenly dispersed.
    $interleaved = array();
    for ($i = 0; $i < $totalRounds; $i++) {
        $interleaved[$i] = array();
    }
   
    $evn = 0;
    $odd = ($teams / 2);
    for ($i = 0; $i < sizeof($rounds); $i++) {
        if ($i % 2 == 0) {
            $interleaved[$i] = $rounds[$evn++];
        } else {
            $interleaved[$i] = $rounds[$odd++];
        }
    }
 
    $rounds = $interleaved;
 
    // Last team can't be away for every game so flip them
    // to home on odd rounds.
    for ($round = 0; $round < sizeof($rounds); $round++) {
        if ($round % 2 == 1) {
            $rounds[$round][0] = flip($rounds[$round][0]);
        }
    }
   
    // Display the fixtures        
    for ($i = 0; $i < sizeof($rounds); $i++) {
        print "<p>Round " . ($i + 1) . "</p>\n";
        foreach ($rounds[$i] as $r) {
            print $r . "<br />";
        }
        print "<br />";
    }
    print "<p>Second half is mirror of first half</p>";
    $round_counter = sizeof($rounds) + 1;
    for ($i = sizeof($rounds) - 1; $i >= 0; $i--) {
        print "<p>Round " . $round_counter . "</p>\n";
        $round_counter += 1;
        foreach ($rounds[$i] as $r) {
            print flip($r) . "<br />";
        }
        print "<br />";
    }
    print "<br />";
 
    if ($ghost) {
        print "Matches against team " . $teams . " are byes.";
    }
}
 
function flip($match) {
    $components = split(' v ', $match);
    return $components[1] . " v " . $components[0];
}
 
function team_name($num, $names) {
    $i = $num - 1;
    if (sizeof($names) > $i && strlen(trim($names[$i])) > 0) {
        return trim($names[$i]);
    } else {
        return $num;
    }
}
 
 
function get_form() {
    $s = '';
    $s = '<p>Enter number of teams OR team names</p>' . "\n";
    $s .= '<form action="' . $_SERVER['SCRIPT_NAME'] . '">' . "\n";
    $s .= '<label for="teams">Number of Teams</label><input type="text" name="teams" />' . "\n";
    $s .= '<input type="submit" value="Generate Fixtures" />' . "\n";
    $s .= '</form>' . "\n";
 
    $s .= '<form action="' . $_SERVER['SCRIPT_NAME'] . '">' . "\n";
    $s .= '<div><strong>OR</strong></div>' . "\n";
    $s .= '<label for="names">Names of Teams (one per line)</label>' . "\n";
 
    mysql_select_db($database_php, $php);
    $query_Recordset1 = "SELECT * FROM tbl_team";
    $Recordset1 = mysql_query($query_Recordset1, $php) or die(mysql_error());
 
    while ($row_Recordset1 = mysql_fetch_assoc($result)) {
       $s .= $row_Recordset1['teamname'] . '<input type="checkbox" name="teams[]" value="' . $row_Recordset1['teamname'] . '" /><br />' . "\n";
    }
 
    $s .= '<input type="submit" value="Generate Fixtures" />' . "\n";
    $s .= "</form>\n";
    return $s;
}
 
main();
 
?>

Open in new window

0
 
LVL 20

Expert Comment

by:NerdsOfTech
ID: 22992147
In the above code just change the ['teamname'] field to the actual field tat refers to the team name
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:sinanosan
ID: 22992819
Giving Error mesages
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link .......
Warning: mysql_query(): supplied argument is not a valid MySQL-Link ....

also what is the purpose of having checkbox?
0
 

Author Comment

by:sinanosan
ID: 22992823
Hi NerdsOfTech
thanks for the repply but your code is giving Error mesages
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link .......
Warning: mysql_query(): supplied argument is not a valid MySQL-Link ....

also what is the purpose of having checkbox?
0
 

Accepted Solution

by:
sinanosan earned 0 total points
ID: 23180525
This problem has been fixed, requesting to be deleted, please
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question