2011-12-28 32 views
0

請任何人都可以解決我的問題。它在drupal 7的意見,我不知道爲什麼if條件不滿意(如果($ mycount == 1))在下​​面的代碼。Views全局計數器IF條件不起作用?

foreach ($fields as $id => $field){ 
    if($field->class == "counter") 
    { 
     $mycount = $field->content; 
     echo $mycount; 
     echo "<br>"; 
    } 
    if($field->class == "field-logo") 
    { 
     var_dump($mycount); // output 
     echo "<br>"; 
     if($mycount == 1) // but here 1 == 1 for the first time. see the output. 
     { 
      echo "worked"; // its not coming here... 
     } 
    } 
} 

OUTPUT:

1 
string(36) "1" 
2 
string(36) "2" 
3 
string(36) "3" 
4 
string(36) "4" 
5 
string(36) "5" 

感謝。

回答

0

我懷疑問題被轉儲字符串大小 - 串(36) - 這表明$mycount內容不是一個字符,但36(數字「1」和35垃圾字符)。你應該試試這個:

$mycount = trim($field->content); 

echo '<pre>as string: ' . var_export($mycount, true) 
    . ', is_numeric: ' . var_export(is_numeric($mycount), true) 
    . ', as integer: ' . var_export((int) $mycount, true) . '</pre><br />'; 

你應該看到像這樣的輸出:

as string: '1', is_numeric: true, as integer: 1 
0

相信查看包裹在一些HTML的字段值,所以你的實際產出可能是<span class="field-content">1</span>,但在瀏覽器你只會看到「1」。

爲了避免在HTML中獲得結果,您需要將「查看結果計數器」字段樣式設置更改爲「自定義字段HTML」並將「 - 無 - 」設置爲下拉值。

現在$field->content將返回值沒有任何HTML和$mycount == 1在你的if語句將工作正常。