我已经坚持了一段时间了。我有一个表正确更新($ table),但似乎无法得到第二个" $ tabled"要更新的表。我知道这一定是傻事。非常感谢对此的任何见解!
以下是我的完整代码:
<?php add_action( 'admin_menu', 'my_plugin_menu' );
function admin_register_head() {
$siteurl = get_option('siteurl');
$url = $siteurl . '/wp-content/plugins/' . basename(dirname(__FILE__)) . '/style.css';
echo "<link rel='stylesheet' type='text/css' href='$url' />\n";
}add_action('admin_head', 'admin_register_head');
function my_plugin_menu() {
add_menu_page( 'Elo Calculator', 'Elo Calculator', 'manage_options', 'elo', 'my_plugin_options');}
function my_plugin_options() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
global $wpdb;
$table = $wpdb->prefix.elo;
$tabled = $wpdb->prefix.elomatches;
// check if elo db exists, otherwise create it
if($wpdb->get_var("SHOW TABLES LIKE '$table'") != $table)
{
$sql = "CREATE TABLE $table (
id int(11) NOT NULL AUTO_INCREMENT,
fn VARCHAR(255) NOT NULL,
ln VARCHAR(255) NOT NULL,
elo INT(4) DEFAULT '1500' NOT NULL,
experience INT(11) NOT NULL,
UNIQUE KEY id (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// check if elomatches db exists, otherwise create it
if($wpdb->get_var("SHOW TABLES LIKE '$tabled'") != $tabled)
{
$sql = "CREATE TABLE $tabled (
winnerid int(11) NOT NULL,
loserid int(11) NOT NULL,
date int(11) NOT NULL,
matchlength int(11) NOT NULL
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// delete users
if (isset($_GET['delete']) && is_numeric($_GET['delete']))
{
$id = $_GET['delete'];
// delete from elo
$wpdb->query("DELETE FROM $table WHERE id='$id'");
// delete from elomatch
$wpdb->query("DELETE FROM $tabled WHERE winnerid='$id' || loserid = '$id'");
}
// undo last match
if (isset($_GET['undo']) && is_numeric($_GET['undo']))
{
// get last match info
$matchid = $_GET['undo'];
$last = $wpdb->get_row("SELECT * FROM $tabled WHERE matchid='$matchid';");
$match = $last->matchlength;
$winnerid = $last->winnerid;
$winnerelo = $last->winnerelo;
$exp = $wpdb->get_row("SELECT * FROM $table WHERE id='$winnerid';");
$winnerexp = $exp->experience - $match;
$loserid = $last->loserid;
$loserelo = $last->loserelo;
$exp = $wpdb->get_row("SELECT * FROM $table WHERE id='$loserid';");
$loserexp = $exp->experience - $match;
// input old elo and experience to the winner
$wpdb->update($table, array('elo' => $winnerelo, 'experience' => $winnerexp), array('id' =>$winnerid));
// input old elo to the loser
$wpdb->update($table, array('elo' => $loserelo, 'experience' => $loserexp), array('id' =>$loserid));
// delete match
$wpdb->query("DELETE FROM $tabled WHERE matchid='$matchid'");
?><p class="success">✔ Undo successful</p><?php
}
// submit forms
if (isset($_POST['add_submit']))
{ // insert new player with default Elo of 1500
$errors = array();
if (isset($_POST['fn']) && $_POST['fn'] != 'First name')
{
$fn = $wpdb->escape($_POST['fn']);
}
else
{
$errors[] = 'Please enter a First name';
}
if (isset($_POST['ln']) && $_POST['ln'] != 'Last name')
{
$ln = $wpdb->escape($_POST['ln']);
}
else
{
$errors[] = 'Please enter a Last name';
}
if (empty($errors))
{ // enter new player
$elo = '1500';
$wpdb->insert(
$table,
array(
'fn' => $fn,
'ln' => $ln,
'elo' => $elo
)
);
?><p class="success">✔ New player added</p><?php
unset($fn);
unset($ln);
}
else
{
?><h2 class="delete">Errors</h2>
<ul><?php
foreach ($errors as $error)
{
?><li><?=$error;?></li><?php
}
?></ul><?php
}
}
if (isset($_POST['submit']))
{ // calculate Elo
$errors = array();
// IDs
if (isset($_POST['playerone']) && is_numeric($_POST['playerone']))
{
$p1id = $wpdb->escape($_POST['playerone']);
}
else
{
$errors[] = 'Please select Player one';
}
if (isset($_POST['playertwo']) && is_numeric($_POST['playertwo']))
{
$p2id = $wpdb->escape($_POST['playertwo']);
}
else
{
$errors[] = 'Please select Player two';
}
// are they same?
if ($p1id == $p2id)
{
$errors[] = 'Players cannot be the same.';
}
// winner
if (isset($_POST['results']) && is_numeric($_POST['results']))
{
$winner = $wpdb->escape($_POST['results']);
}
else
{
$errors[] = 'Please select a winner';
}
// match length
if (isset($_POST['matchlength']) && is_numeric($_POST['matchlength']))
{
$n = $wpdb->escape($_POST['matchlength']);
}
else
{
$errors[] = 'Please select a match length';
}
if (empty($errors))
{ // do all calculations
function getElo($exp, $n, $winnerelo, $loserelo, $f)
{
if ($exp < 400)
{ // use the ramp or not?
$pe = 5-(($exp+$n)/100);
}
else
{
$pe = 1;
}
$d = $winnerelo - $loserelo;
$u = 1/((pow(10, $d*sqrt($n)/2000))+1);
if ($f == 1)
{
$u = (1-$u);
};
// return new Elo
return number_format(4*$pe*sqrt($n)*$u);
}
$p1 = $wpdb->get_row("SELECT * FROM $table WHERE id = $p1id");
$p2 = $wpdb->get_row("SELECT * FROM $table WHERE id = $p2id");
$p1elo = $p1->elo;
$p2elo = $p2->elo;
$p1exp = $p1->experience;
$p2exp = $p2->experience;
// f or u
if ($p1elo >= $p2elo && $winner == 1)
{
$f = NULL;
}
if ($p1elo >= $p2elo && $winner == 2)
{
$f = 1;
}
if ($p1elo < $p2elo && $winner == 1)
{
$f = 1;
}
if ($p1elo < $p2elo && $winner == 2)
{
$f = NULL;
}
// player one
if ($winner == 1)
{
$win = $p1id;
$p1 = $p1elo + getElo($p1exp, $n, $p1elo, $p2elo, $f);
// elo for undo
$winelo = $p1elo;
}
else
{
$lose = $p1id;
$p1 = $p1elo - getElo($p1exp, $n, $p1elo, $p2elo, $f);
// elo for undo
$losselo = $p1elo;
}
$wpdb->update($table, array('elo' => $p1, 'experience' => $p1exp+$n), array('id' =>$p1id));
// player two
if ($winner == 2)
{
$win = $p2id;
$p2 = $p2elo + getElo($p2exp, $n, $p1elo, $p2elo, $f);
// elo for undo
$winelo = $p2elo;
}
else
{
$lose = $p2id;
$p2 = $p2elo - getElo($p2exp, $n, $p1elo, $p2elo, $f);
// elo for undo
$losselo = $p2elo;
}
$wpdb->update($table, array('elo' => $p2, 'experience' => $p2exp+$n), array('id' =>$p2id));
// update win/loss database
$date = time();
$wpdb->update($tabled, array('winnerid' => $win, 'loserid' => $lose, 'date' => $date, 'matchlength' => $n, 'winnerelo' => $winelo, 'loserelo' => $losselo));
?>
<p class="success">✔ Elo updated</p><?php
unset($win);
unset($lose);
unset($winner);
unset($n);
unset($p1id);
unset($p2id);
}
else
{
?><h2 class="delete">Errors</h2>
<ul><?php
foreach ($errors as $error)
{
?><li><?=$error;?></li><?php
}
?></ul><?php
}
}
// last match date
$lastmatch = $wpdb->get_row("SELECT MAX(date) AS updated FROM $tabled ORDER BY date DESC;");
// get last match
$lastwin = $wpdb->get_row("SELECT * FROM $tabled INNER JOIN $table ON $tabled.winnerid=$table.id ORDER BY $tabled.date DESC LIMIT 1;");
$lastloss = $wpdb->get_row("SELECT * FROM $tabled INNER JOIN $table ON $tabled.loserid=$table.id ORDER BY $tabled.date DESC LIMIT 1;");
// get user list
$users = $wpdb->get_results("SELECT * FROM $table ORDER BY ln ASC;");
$elo = $wpdb->get_results("SELECT * FROM $table ORDER BY elo DESC, ln ASC;");
?><div class="wrap">
<style>
ul.half li {width: 50%; float: left; font-family: georgia;}
.wrap p {font-family: georgia; font-size: 1.2em;}
.length {font-size: 1.2em; margin-right: 20px; margin-left: 5px;}
.select {font-size: 1.2em;}
table {font-size: 1.4em; line-height: 1.4em;}
input[type=submit] {font-size: 1.4em; padding: 5px;}
.delete {color: red; font-size: 2em; font-weight: bold;}
.success:first-letter {color: green; font-size: 2em; font-weight: bold;}
.small-caps {font-variant: small-caps;}
</style>
<h2><a href="admin.php?page=elo">Elo Calculator</a></h2>
<p><strong>Last match:</strong> <?=$lastwin->fn;?> <?=$lastwin->ln;?> <span class="small-caps">vs</span> <?=$lastloss->fn;?> <?=$lastloss->ln;?> on <?=date('F j, Y @ h:ia', $lastmatch->updated-25200);?></p>
<form name="new_elo" method="post" action="<?=$_SERVER['REQUEST_URI'];?>">
<p>
<label for="new_player">Add player:</label>
<input type="text" value="First name" name="fn" onfocus="if (this.value == 'First name') {this.value = '';}" onblur="if (this.value == '') {this.value = 'First name';}" />
<input type="text" value="Last name" name="ln" onfocus="if (this.value == 'Last name') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Last name';}" />
<input type="submit" value="Submit" name="add_submit" />
</p>
<hr />
<form name="add_elo" method="post" action="<?=$_SERVER['REQUEST_URI'];?>">
<ul class="half">
<li>
<h3>Player one</h3>
<select name="playerone" class="select">
<option></option>
<?php
foreach ($users as $row)
{
if (isset($p1id) && $p1id == $row->id)
{
$selected = ' SELECTED';
}
else
{
$selected = NULL;
}
?><option value="<?=$row->id;?>"<?=$selected;?>><?=$row->fn;?> <?=$row->ln;?> (<?=$row->elo;?>)</option><?php
}
?>
</select>
<?php
if (isset($winner) && $winner == 1)
{
$checked = ' checked';
}
else
{
$checked = NULL;
}
?>
<p><input type="radio" name="results" value="1" id="p1"<?=$checked;?> /> <label for="p1" class="length">Winner</label></p>
</li>
<li>
<h3>Player two</h3>
<select name="playertwo" class="select">
<option></option>
<?php
foreach ($users as $row)
{
if (isset($p2id) && $p2id == $row->id)
{
$selected = ' SELECTED';
}
else
{
$selected = NULL;
}
?><option value="<?=$row->id;?>"<?=$selected;?>><?=$row->fn;?> <?=$row->ln;?> (<?=$row->elo;?>)</option><?php
}
?>
</select>
<?php
if (isset($winner) && $winner == 2)
{
$checked = ' checked';
}
else
{
$checked = NULL;
}
?>
<p><input type="radio" name="results" value="2" id="p2"<?=$checked;?> /> <label for="p2" class="length">Winner</label></p>
</li>
</ul>
<br style="clear: both" />
<h3>Match length</h3>
<p>
<input type="radio" name="matchlength" checked="checked" value="1" id="l1"<?php if (isset($n) && $n == 1) { echo ' CHECKED';}?>> <label for="l1" class="length">1</label>
</p>
<br />
<p>
<input type="submit" value="Submit" name="submit" />
</p>
</form>
<br />
<table class="widefat">
<thead>
<tr>
<th>Player</th>
<th>Elo</th>
<th>Record</th>
<th>Win percentage</th>
<th>Experience</th>
<th>Delete player</th>
</tr>
</thead>
<tbody>
<?php
foreach ($elo as $row)
{
$wins = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $tabled WHERE winnerid = $row->id;"));
$losses = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $tabled WHERE loserid = $row->id;"));
?><tr>
<td><?=$row->fn;?> <?=$row->ln;?></td>
<td><?=$row->elo;?></td>
<td><?=$wins;?> - <?=$losses;?></td>
<td><?php if ($wins > 0) {echo 100*number_format($wins/($wins+$losses), 2).'%';} else {echo '0%';}?></td>
<td><?=$row->experience;?></td>
<td><a class="delete" href="http://vancouverbg.com/wp-admin/admin.php?page=elo&delete=<?=$row->id;?>">×</a></td>
</tr><?php
}
?>
</tbody>
</table>
<p><a href="admin.php?page=elo&undo=<?=$lastwin->matchid;?>">Undo last match</a></p>
</div><?php }