2014-03-28 59 views
-1

環路我曾嘗試下面的PHP代碼中prevous指數替代指數空值:通過同時使用PHP

$i=1; 
foreach(getTop5($pid) as $da){ 
    $i++     
    if($da['index_enjeu_percu'] != NULL){ 
     foreach(getDataChart($pid,$da['index_enjeu_percu']) as $data){ 
     $avg = $data["avg_index_importance"].', '; 

     $poin_important = substr($avg,0,-2); 
     $enjeu .= $data["index_enjeu"].', '; 
     $res_enjeu = substr($enjeu,0,-1); 
     echo $avg; 
     } 
    } 
    else{ 
     // I want to do the foreach again in here but my data $da['index_enjeu_percu'] == NUll 
     //I don want to take it as NULL value, I want to take the prevous value of $da['index_enjeu_percu'] 
     foreach(getDataChart($pid,$da['index_enjeu_percu']) as $data){ 
      $avg = $data["avg_index_importance"].', '; 

      $poin_important = substr($avg,0,-2); 
      $enjeu .= $data["index_enjeu"].', '; 
      $res_enjeu = substr($enjeu,0,-1); 
      echo $avg; 
     } 
    } 
} 

我想要的是:我有循環的數據與上述 例如:如果我的結果是:1 2 3 4 NULL萬一它等於NULL我想用4替換NULL 我沒有解決這個問題。任何一個請幫助我,謝謝

+0

使用'is_null()'。 –

+0

你爲什麼不在任何地方使用'$ i'? –

回答

2

保存前值到會話嘗試這樣的:

session_start(); 
$i=1; 
foreach(getTop5($pid) as $da){ 
    $i++;     
    if($da['index_enjeu_percu'] != NULL){ 
     $_SESSION['prev_value']=$da['index_enjeu_percu']; // This will save the value of da['index_enjeu_percu'] to session 
     foreach(getDataChart($pid,$da['index_enjeu_percu']) as $data){ 
     $avg = $data["avg_index_importance"].', '; 

     $poin_important = substr($avg,0,-2); 
     $enjeu .= $data["index_enjeu"].', '; 
     $res_enjeu = substr($enjeu,0,-1); 
     echo $avg; 
     } 
    } 
    else{ 
     $da['index_enjeu_percu'] = $_SESSION['prev_value']; // This will get the previous value of index 
     foreach(getDataChart($pid,$da['index_enjeu_percu']) as $data){ 
      $avg = $data["avg_index_importance"].', '; 

      $poin_important = substr($avg,0,-2); 
      $enjeu .= $data["index_enjeu"].', '; 
      $res_enjeu = substr($enjeu,0,-1); 
      echo $avg; 
     } 
    } 
} 
0

保留最後的值在一個變量。檢查$ avg是否爲null,然後輸出前面的數字else print avg。

$i=1; 
$prev_avg = NULL; 
foreach(getTop5($pid) as $da){ 
    $i++     
    if($da['index_enjeu_percu'] != NULL){ 
     foreach(getDataChart($pid,$da['index_enjeu_percu']) as $data){ 
     $avg = $data["avg_index_importance"].', '; 

     $poin_important = substr($avg,0,-2); 
     $enjeu .= $data["index_enjeu"].', '; 
     $res_enjeu = substr($enjeu,0,-1); 
     if ($avg == NULL) 
      echo $prev_avg; 
     else 
      echo $avg; 
     $prev_avg = $avg; 
     } 
    } 
    else{ 
     // I want to do the foreach again in here but my data $da['index_enjeu_percu'] == NUll 
     //I don want to take it as NULL value, I want to take the prevous value of $da['index_enjeu_percu'] 
     foreach(getDataChart($pid,$da['index_enjeu_percu']) as $data){ 
      $avg = $data["avg_index_importance"].', '; 

      $poin_important = substr($avg,0,-2); 
      $enjeu .= $data["index_enjeu"].', '; 
      $res_enjeu = substr($enjeu,0,-1); 
     if ($avg == NULL) 
      echo $prev_avg; 
     else 
      echo $avg; 
     $prev_avg = $avg; 

     } 
    } 
}