【问题标题】:#PHP i wanna do make the code better#PHP 我想做更好的代码
【发布时间】:2017-10-21 16:33:32
【问题描述】:

我试图让代码更好更干净。

工作但看起来很糟糕。 => http://wklej.org/id/3275269/

现在我试着让它变得更好。

$db = $this->database[GDB];

$message = '';
$num_rows = $db->doQuery('SELECT strUserID FROM USERDATA WHERE AUTHORITY=0');
if ($num_rows == -1)
{
    $this->Error('DB_ERROR');
    $db->getError();
    return;
}
else if($num_rows == NULL){
    $message = '<h3 class="error">List of Game Master is empty.</h3>';
}

$content = '';
while($result = $db->doRead()){
    $gm = $result['strUserID'];

    $query = $db->doQuery('select count(*) from CURRENTUSER where strCharID=?', $gm); 
    $row = $db->doRead();

    if($query == 1)
        $data['Status']='Online';
    else 
        $data['Status']='Offline';

    $content .= Template::Load('gmonline-1', array(
        'name' => $gm,
        'char_status' => $data['Status'])
    );

} //End while

$this->content = Template::Load('gmonline', array(
  'GM_LIST' => $content,
  'message' => $message
));

它不显示错误,但不显示所有 GM LIST。

【问题讨论】:

  • 你能说得清楚一点吗? “看起来很糟糕”的代码实际上比这更干净。但我仍然不知道你想要什么
  • 感谢 asnwer,因为我想显示 Game Master List (Nick + status) with function while,但是 num_rows 不会像我想要的那样工作,因为我不知道如何使用它
  • 我不知道的东西太多了。对象$db 是什么?从方法来看,我会说你使用的是 mongo DB,但是查询是用 SQL 进行的。
  • 再次感谢您的回答,wklej.org/id/3275281 它的方法

标签: php sql


【解决方案1】:

好的,我做到了。很好用

function Run()
    {
        $db = $this->database[GDB];
        $message = '';
        $num_rows = $db->doQuery("SELECT * FROM USERDATA ud LEFT JOIN CURRENTUSER cu ON (strUserId = strCharID) WHERE authority = 0;");
        if ($num_rows == -1)
        {
            $this->Error('DB_ERROR');
            $db->getError();
            return;
        }
        elseif($num_rows == NULL){
            $message = '<h3 class="error">List of Game Master is empty.</h3>';
        }

        $content = '';
        while ($row = $db->doRead())
            {
            if($row['strUserId'] == $row['strCharID'])
             $status = 'Online';
            else
            $status = 'Offline';
            $content .= Template::Load('gmonline-1', array(
                'name' => $row['strUserId'], 
                'char_status' => $status ));
        }
    $this->content = Template::Load('gmonline', array('GM_LIST' => $content, 'message' => $message));
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 2011-02-27
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    相关资源
    最近更新 更多