#PHP我想让代码变得更好

时间:2017-10-21 16:33:33

标签: php sql

我尝试使代码更好更干净。

工作但看起来很糟糕。 => 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。

1 个答案:

答案 0 :(得分:0)

好吧,我做到了。干得好

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));
    }