2014-06-14 2 views
-2

컨트롤러에서 heredoc 구문과 관련된 문제가 있습니다.foreach 루프에서 php heredoc 구문을 사용하는 방법

function active() { 
    $userlist = $this->input->post('userlist'); 
    $userlist = explode(',',$userlist[0]); 

    $items = ''; 

    if(!empty($userlist)){ 
     foreach($userlist as $buddy) 
     { 

      $actv = $this->user_model->check_active_users($buddy);//returns 0 if no results found 
      if ($online_buddies == 0) { 
       $items .= <<<EOD 
      { 
       "fl": "0", 
       "fid": "{$buddy}" 
      }, 
EOD; 
      }//if returned 0 inactive 

     }//foreach 
    }//if nt empty $mybuddies 

    if ($items != '') 
    { 
     $items = substr($items, 0, -1); 
    } 
    header('Content-type: application/json'); 
?> 
{ 
    "items": [ 
     <?php echo $items;?> 
    ] 
} 

<?php 
     exit(0); 

}//end of func active 

$userlistuser-ids 보유 : 내 컨트롤러 기능은 다음과 같이한다.

$this->user_model->check_active_users($buddy) 결과가없는 경우 0을 반환합니다.

각 사용자 ID뿐만 아니라 데이터베이스에 결과가없는 경우 플래그 0을 얻고 싶습니다.

그러나, 여기

$items .= <<<EOD 
{ 
"fl": "0", 
"fid": "{$buddy}" 
} 
EOD; 

, fl 반환 0하지만 fid 아무것도 반환하지 않습니다. 내가 잘못한 일을 했습니까? "fid": "{$buddy}"

+0

을 대신 문자열을 조작, 당신은'()로 json_encode'에 의해 네이티브 PHP 배열의 출력 최종 결과를 사용하여 시도해야한다; –

+0

탱크가 많습니다 ..... – user3177114

답변

0

heredoc은 제작하는 내용에 비해 약간의 것 같습니다. 왜이 대신 같은 것을하지 :

$items .= '{ 
      "fl": "0", 
      "fid": "' . $buddy . '" 
      },' 
1
$html = <<<HTML // Set a variable for your dashboard HTML 

HTML here... 

HTML; // This ends the heredoc for now and concludes the HTML to open the PHP back up 

// The follow resumes your pure PHP code 
$query = "SELECT * FROM `some_table_in_database`"; 

$results = mysqli_query($query); 


foreach ($results as $record) { 

    // Bellow you define the records as variables 

    $variable1   = $record->one; 
    $variable2   = $record->two; 
    // as many variables as you would like 

    $html .= <<<HTML // Now, you have to concatenate to your $html variable (adding on to it) some more HTML 

    HTML here.... 

HTML; // Now we need to switch back over to PHP, so we close the HTML for now 

    // Begin more PHP code here for further use down the page 

    $html .= <<<HTML // We open back up the HTML variable to put more HTML in our page 

    HTML here... 

HTML; // and this concludes the HTML. You can keep going on for ever like this alternating between PHP and HTML code in order to get for and foreach loops and such to cooperate. 
+0

위의 코드는 스택 오버플로 코드 하이 라이터에서 조금 분해되었지만 <<< SUPPLIERDASHBOARD heredoc opener 및 SUPPLIERDASHBOARD; 가까이에주의를 기울이고 싶은 부분이 있습니다. 연결 방법을 주목하십시오. PHP 스크립트 주위에서 신중하게 그들을 깨고 HTML 무거운 청크 주위에 그들을 사용합니다. – mrwpress

관련 문제