将PHP / MySQL查询转换为wpdb

时间:2016-12-25 15:26:52

标签: php mysql wordpress

我一直在WordPress网站上使用以下代码,没有太多问题(使用Code Snippets Extended和一个短代码),直到几天前它停止工作。

<?php

//function to add an ordinal value to the number
function showOrdinal($number)
{
    // first convert to string if needed
    $num = (string) $number;
    // now we grab the last digit of the number
    $last_digit = substr($number, -1, 1);
    // if the string is more than 2 chars long, we get
    // the second to last character to evaluate
    if (strlen($number)>1)
    {
        $next_to_last = substr($num, -2, 1);
    }
    else
    {
        $next_to_last = "";
    }
    // now iterate through possibilities in a switch
    switch($last_digit)
    {
        case "1":
            // testing the second from last digit here
            switch($next_to_last)
            {
                case "1":
                    $number.="th";
                    break;
                default:
                    $number.="st";
            }
            break;
        case "2":
            // testing the second from last digit here
            switch($next_to_last)
            {
                case "1":
                    $number.="th";
                    break;
                default:
                    $number.="nd";
            }
            break;
        // if last digit is a 3
        case "3":
            // testing the second from last digit here
            switch($next_to_last)
            {
                case "1":
                    $number.="th";
                    break;
                default:
                    $number.="rd";
            }
            break;
        // for all the other numbers we use "th"
        default:
            $number.="th";
            break;
    } 

    // finally, return our string with it's new suffix
    return $number;
}

//assign the week number
$weekno = 1;

//build query
$sql = "SELECT rank_number, player_id, name, score ";
$sql .= "FROM ( ";
$sql .= "   SELECT @rank:=@rank+1 AS rank_number, player_id, name, score ";
$sql .= "   FROM ( ";
$sql .= "       SELECT player_id, name, en.score AS score ";
$sql .= "       FROM users ";
$sql .= "       JOIN entries en ON player_id = en.player_id";
$sql .= "       WHERE score != 0 ";
//here we only want score pulled out for that particular weeks game
if ($weekno > 0){
    $sql .= "AND en.week_no = " . $weekno . " ";
}
$sql .= "       GROUP BY player_id ";
$sql .= "       ORDER BY score DESC";
$sql .= "   ) AS rankings, (SELECT @rank:=0) AS r ";
$sql .= ") AS overall_rankings ";
$sql .= "LIMIT 0, 100; ";

// Perform Query
$result = mysql_query($sql);

//Shows some user details and the current top 100 ranks
while ($row = mysql_fetch_assoc($result)) {

    echo '<div class="one-fifth column  column_our_team" style="float: left;" >';
    echo '<div style="border: 1px solid #d4e1ea; background: #fff; padding: 20px; height: 250px; border-radius: 10px; box-shadow: 0 0 10px 5px rgba(0, 0, 0, 0.08);">';


    echo '<div class="team team_circle ">';
    echo '<h2 class="title" style="text-align:center;"><strong>'.  showOrdinal($row['rank_number']) . '</strong></h2>' ;
    echo '<hr class="hr_color">';
    echo '<div class="image_frame no_link scale-with-grid">';
    echo '<div class="image_wrapper">';
    global $userdata; get_currentuserinfo(); echo get_avatar( $userdata=($row['player_id']), 100); 
    echo '</div>';
    echo '</div>';
    echo '<div class="desc_wrapper">';
    echo '<h4>'.  $row['name'] . '</h4>';
    echo '<hr class="hr_color">';
    echo '<h4><strong> Score: ' .  $row['score'] . '</strong></h4>';
    echo '</div>';
    echo '</div>';
    echo '</div>';

    echo '</div>';
  }
//free the result
mysql_free_result($result);
?>

自定义表连接和代码段插件似乎都正常工作,因为我可以使用

访问该表
global $wpdb;
$entries = $wpdb->get_results("SELECT * FROM entries;");

我决定创建一个WP函数也许是一个好主意,因为我在一些地方使用这个脚本(输出略有变化)。但是,当我尝试将PHP构建的查询转换为兼容wpdb的查询时,我开始遇到麻烦。

    //build query
$sql = "SELECT rank_number, player_id, name, score ";
$sql .= "FROM ( ";
$sql .= "   SELECT @rank:=@rank+1 AS rank_number, player_id, name, score ";
$sql .= "   FROM ( ";
$sql .= "       SELECT player_id, name, en.score AS score ";
$sql .= "       FROM users ";
$sql .= "       JOIN entries en ON player_id = en.player_id";
$sql .= "       WHERE score != 0 ";
//here we only want score pulled out for that particular weeks game
if ($weekno > 0){
    $sql .= "AND en.week_no = " . $weekno . " ";
}
$sql .= "       GROUP BY player_id ";
$sql .= "       ORDER BY score DESC";
$sql .= "   ) AS rankings, (SELECT @rank:=0) AS r ";
$sql .= ") AS overall_rankings ";
$sql .= "LIMIT 0, 100; ";

// Perform Query
$result = mysql_query($sql);

任何指导都非常感谢。

0 个答案:

没有答案