2013-08-18 40 views
0

我想第一次使用json編碼,需要一些洞察力,我做錯了什麼。使用json編碼

它應該是這樣的:

{隊:[[ 「2147483647」, 「9」],[ 「2147483647」, 「6」],[ 「2147483647」, 「4」], [「11」,「2147483647」],[「5」,「2147483647」],[「12」,「8」],[「10」,「3」],[「2147483647」,「7」 ] [0,1],[0,1],[1,0],[1,0],[0,0],[0,0],[ 0,1],[0,0],[0,0],[0,0],[0,0],],[[0,0],[0,0],],[ [0,0],[0,0]]]]}

但返回的數據看起來像這樣: -

{「teams」:[[「2147483647」,「10」,「5」,「12」,「11」,「2147483647」,「2147483647」,「2147483647」],[「7」,「3」 , 「2147483647」, 「8」, 「2147483647」, 「4」, 「6」, 「9」]], 「結果」:[[ 「0」, 「0」, 「0」, 「0」,」 0" , 「0」, 「0」, 「0」],[ 「0」, 「0」, 「0」, 「0」, 「0」, 「0」, 「0」, 「0」]] }

代碼:

public function getAutoCompleteData($tournID, $db) 
{ 

    $max   = $this->max($tournID); 
    $one   = $this->tourn_info("1on1", $tournID); 
    $total_matches = $max; 
    $after_matches = $max/2; 

    $matches = $db->query($this->select("*", "matches", "leagueID='{$tournID}' AND league='tourn'")); 
    while ($row = $db->fetchAssoc($matches)) { 

     $clan1 = $this->getname($row['clan1'], $tournID, "tourn", $ac = NULL, 1, 1, $wc2 = 0); 
     $clan2 = $this->getname($row['clan2'], $tournID, "tourn", $ac = NULL, 1, 1, $wc2 = 1); 

     if ($row['matchno'] <= $after_matches) { 
      $clan_one[] = $row['clan1']; 
      $clan_two[] = $row['clan2']; 
      $score_one[] = $row['score1']; 
      $score_two[] = $row['score2']; 
     } 
    } 

    $data = array(
     'teams' => array(
      $clan_one, 
      $clan_two 
     ), 
     'results' => array(
      $score_one, 
      $score_two 
     ) 
    ); 

    return $data; 
} 

顯示位置的球隊,就應該關閉支架]每兩支球隊? 希望有人能幫助。

+1

但第一個代碼不是JSON ... – elclanrs

+0

你的班級在哪裏? '$ this'是指一個類。在類中使構造函數使用'public function __construct(){}'。 – PHPglue

+0

你的代碼很混亂。變量$ 1,$ clan1,$ clan2永遠不會被使用。您正在創建一個包含兩個項目的數組,每個項目都有兩個項目。你的JSON顯示的是什麼。 – Mundi

回答

0

不是說這會解決你的問題,但它可以幫助你理解PHP。

class mustBeAClass{ 
    protected function max($tnid){ 
    // must have this function - can be public for outside method use 
    } 
    protected function tourn_info($typ, $tnid){ 
    // must have this function - can be public for outside method use 
    } 
    protected function getname($arg0, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6 = 0){ 
    /* must have this function - can be public for outside method use 
    notice that $arg6 is how you assign default to arguments */ 
    } 
    public function getAutoCompleteData($tournID, $db){ 
    $db->open(); 
    $max = $this->max($tournID); $one = $this->tourn_info("1on1", $tournID); 
    // what is the point of this ---->>>> $total_matches = $max; 
    $after_matches = $max/2; 
    // notice query issue below 
    $matches = $db->query("SELECT * FROM matches WHERE leagueID ='$tournID' && league='tourn'")); 
    // while loop has problems 
    if($matches->num_rows < 1){ 
     die('You have no matches'); 
    } 
    else{ 
     while($row = $db->fetch_assoc()){ 
     $clan1 = $this->getname($row['clan1'], $tournID, 'tourn', null, 1, 1, 0); 
     $clan2 = $this->getname($row['clan2'], $tournID, 'tourn', null, 1, 1, 1); 
     if($row['matchno'] <= $after_matches) { 
      $clan_one[] = $row['clan1']; 
      $clan_two[] = $row['clan2']; 
      $score_one[] = $row['score1']; 
      $score_two[] = $row['score2']; 
     } 
     } 
     $data = array(
     'teams' => array($clan_one, $clan_two), 
     'results' => array($score_one, $score_two) 
    ); 
    } 
    $matches->free(); $db->close(); 
    return $data; 
    } 
} 
$cls = new mustBeAClass; 
echo json_encode($cls->getAutoCompleteData($tournId, $db)); 

當然可以用正確的價值觀爲$tournId和你new mysqli(/*args*/)$db