2017-10-21 90 views
-3

我試着讓代碼更好,更乾淨。#PHP我想讓代碼更好

工作,但很糟糕。 =>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列表。

+0

你能更清楚了嗎? 「看起來不好」的代碼實際上比這更乾淨。但是我仍然不知道你想要什麼 – mloureiro

+0

感謝您的支持,因爲我想用功能顯示Game Master List(Nick + status),但是num_rows不會像我想要的那樣工作,因爲我不知道如何使用它 –

+0

有這麼多我不知道。什麼是對象'$ db'?從這些方法中,我會說你使用的是mongo DB,但是這些查詢是在SQL中完成的。 – mloureiro

回答

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