php
  • json
  • 2014-02-11 109 views -1 likes 
    -1

    這是我的代碼PHP的循環問題

    $rQuery="SELECT * FROM rowManagr WHERE id=0"; 
        $rResult = $mysqli->query($rQuery)or die($mysqli->error.__LINE__); 
        while($row = $rResult->fetch_assoc()) 
        { 
    
         $rowQuery='SELECT * FROM row_vid WHERE rowid='.$row['rowid']; 
         $rowResult = $mysqli->query($rowQuery)or die($mysqli->error.__LINE__); 
    
         while($rowHome = $rowResult->fetch_assoc()) 
         { 
          if($rowHome['showid']!=0) 
          { 
    
           $json[] = array(....); 
    
    
          } 
    
    
           elseif($rowHome['season_id']!=0){ 
            $json[] = array(....); 
    
           } 
           elseif($rowHome['showid']==0 && $rowHome['season_id']==0){ 
           $json[] = array(....); 
    
    
           } 
    
    
    
    
    
        } 
    
    $jsondata[$row['rowname']]=$json; 
    $g['responses']= $jsondata; 
        } 
    
    
    
        $jsons = json_encode($g); 
        echo $jsons; 
    

    這段代碼的輸出是(例如)

    {"responses":{ 
        "Seasons":[ 
        {"1a"}, 
        {"2a"}, 
        {"3a"}, 
        {"4a"} 
        ], 
        "Other Show":[ 
        {"1a"}, 
        {"2a"}, 
        {"3a"}, 
        {"4a"}, 
        {"1b"}, 
        {"2b"}, 
        {"3b"}, 
        {"4b"} 
        ], 
        "Videos":[ 
        {"1a"}, 
        {"2a"}, 
        {"3a"}, 
        {"4a"}, 
        {"1b"}, 
        {"2b"}, 
        {"3b"}, 
        {"4b"}, 
        {"1a"}, 
        {"2a"}, 
        {"3a"}, 
        {"4a"}, 
        {"1c"}, 
        {"2c"}, 
        {"3c"}, 
        {"4c"} 
        ] 
    

    } }

    季節,其他顯示,視頻中包含4results但在輸出將季節複製到其他節目中,並將其他節目複製到視頻中。

    實際產量應該是

    { 
        "responses":{ 
         "Seasons":[ 
         {"1a"}, 
        {"2a"}, 
        {"3a"}, 
        {"4a"} 
         ], 
         "Other Show":[ 
         {"4a"}, 
        {"1b"}, 
        {"2b"}, 
        {"3b"}, 
        {"4b"} 
    
         ], 
         "Videos":[ 
         {"1c"}, 
        {"2c"}, 
        {"3c"}, 
        {"4c"} 
         ] 
        } 
    } 
    

    感謝

    +0

    請你的代碼剝離下來到相關配件! – urzeit

    +0

    你能解釋我們這是什麼意思'我的問題是響應。季節,響應。其他顯示和響應。視頻顯示包括4結果,但在輸出響應。季節被複制到響應。其他顯示和響應。季節,響應。 OtherShow被複制到response.Video' ?? – Roopendra

    +0

    strip down ..... – user3294288

    回答

    0

    使用break;走出while循環,還可以使用else if結構,而不是nested if

    +0

    這也不起作用:( – user3294288

    +0

    把'繼續;'在'if'塊和showid和seasonid和其他部分是季節編號 – vivekpansara

    +0

    我編輯了我的代碼,仍然有問題 – user3294288

    1

    只要看看我的代碼。

    它會給你你的例子中定義的代碼。

    這也是一個例子。

    <?php 
    $mysqli = mysqli_connect('localhost','root','','DBName'); 
        $json = '{ responses {"Seasons:["'; 
        $rQuery = "SELECT * FROM TBL_NAME"; 
        $rResult = $mysqli->query($rQuery)or die($mysqli->error.__LINE__); 
        while($row = $rResult->fetch_assoc()) 
        { 
         $json .= "{" . $row['name'] . "}" ; 
        } 
        $json .= "]}}"; 
        echo $json ; 
    ?> 
    
    +0

    對不起,數組不是空的,我剛剛刪除了它來減少代碼的大小 – user3294288

    +0

    是............ .... :( – user3294288

    +0

    答案更新。現在看,可能會幫助你。 –

    1

    前的內部while循環只是初始化數組$jon

    while($row = $rResult->fetch_assoc()) { 
        $rowQuery='SELECT * FROM row_vid WHERE rowid='.$row['rowid']; 
        $rowResult = $mysqli->query($rowQuery)or die($mysqli->error.__LINE__); 
    
        // init $json 
        $json = array(); 
    
        while($rowHome = $rowResult->fetch_assoc()) { 
         if($rowHome['showid']!=0) { 
          $json[] = array(....); 
         } 
         ... 
    
    相關問題