2012-04-11 24 views
1

我需要一些幫助,以便在此思考。我有這個代碼來計算正確的數量forecast_conditions這是4; (count($condition) -1) <= 4 ? ', ' : ', eller '如果計數(數據)低於或等於4,請打印一張或打印兩張 - 不起作用

count($condition)打印5所以我必須有負之一。 $condition是在foreach,所以我可以循環內容forecast_conditions; foreach($whome_answer[weather][forecast_conditions] AS $condition) {

上面的代碼打印onsdag, torsdag, fredag, lördag,這是錯誤的。以下是我想要的:onsdag, torsdag, fredag, eller lördag但我無法在代碼中正確使用!我測試了各種解決方案(== 4,> = 4,!= 4,< 4等等)。

我怎樣才能讓它正常工作?

在此先感謝!

編輯

這裏的循環:

foreach($whome_answer[weather][forecast_conditions] AS $condition) { 

    # KONTROLL 
    if(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'mån') { 
     $day = 'måndag'; 

    # KONTROLL 
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'tis') { 
     $day = 'tisdag'; 

    # KONTROLL 
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'ons') { 
     $day = 'onsdag'; 

    # KONTROLL 
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'tors') { 
     $day = 'torsdag'; 

    # KONTROLL 
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'fre') { 
     $day = 'fredag'; 

    # KONTROLL 
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'lör') { 
     $day = 'lördag'; 

    # KONTROLL 
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'sön') { 
     $day = 'söndag'; 
    } 



    echo '<a href="javascript:void(0)" class="forecast-link">'; 
     echo $day; 
    echo '</a>'; 

    echo (count($condition) -1) <= 4 ? ', ' : ', eller '; 

} 

這裏是我獲取從內容的鏈接:http://www.google.com/ig/api?weather=,,,59378217,13504219&hl=en

EDIT(解決方案,但多了一個問題) 代碼下面是解決方案,但它現在打印onsdag, torsdag, fredag, eller lördag,而不是我正在尋找。如何刪除「lördag」後的最後一個逗號?如果在「if tag」之後包含$ionsdag, 2torsdag, 3fredag, eller 4lördag, 5,看起來如何。

我該如何解決這個問題?

$i = 1; 
foreach($whome_answer[weather][forecast_conditions] AS $condition) { 
    $i++; 

    # KONTROLL 
    if(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'mån') { 
     $day = 'måndag'; 

    # KONTROLL 
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'tis') { 
     $day = 'tisdag'; 

    # KONTROLL 
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'ons') { 
     $day = 'onsdag'; 

    # KONTROLL 
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'tors') { 
     $day = 'torsdag'; 

    # KONTROLL 
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'fre') { 
     $day = 'fredag'; 

    # KONTROLL 
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'lör') { 
     $day = 'lördag'; 

    # KONTROLL 
    } elseif(utf8_decode($condition['day_of_week']['@attributes']['data']) == 'sön') { 
     $day = 'söndag'; 
    } 



    echo '<a href="javascript:void(0)" class="forecast-link">'; 
     echo $day; 
    echo '</a>'; 

    echo $i != 4 ? ', ' : ', eller '; 

} 
+0

不知道我明白這個問題。聽起來像是對數字的誤解。 「如果計數(數據)低於或等於4」只是計數($數據)<= 4。不知道-1來自哪裏。 – Corbin 2012-04-11 19:35:56

+0

請閱讀整個問題。 – Erik 2012-04-11 19:37:06

+0

<而不是<= – jack 2012-04-11 19:37:36

回答

3

在名爲$ i的foreach之外實例化一個變量,並將其設置爲0,併爲每個循環增加1,然後針對該變量進行檢查。

由於它是PHP的,我不認爲計數返回DOM對象的位置,這是別的計數的東西,你大概多少價值在$條件。

請考慮下面的代碼,程序員程序員,更容易閱讀:)

$i = 0; 
foreach($whome_answer[weather][forecast_conditions] AS $condition) { 
    $i++; 
    switch($condition['day_of_week']['@attributes']['data']) { 
     case 'mån': 
      $day = 'måndag'; 
     break; 
     case 'tis': 
      $day = 'tisdag'; 
     break; 
     case 'ons': 
      $day = 'onsdag'; 
     break; 
     case 'tors': 
      $day = 'torsdag'; 
     break; 
     case 'fre': 
      $day = 'fredag'; 
     break; 
     case 'lör': 
      $day = 'lördag'; 
     break; 
     case 'sön': 
      $day = 'söndag'; 
     break; 
    } 

echo '<a href="javascript:void(0)" class="forecast-link">'; 
    echo $day; 
echo '</a>'; 

if($i == 4) 
    echo ', eller'; 
else { 
     if($i < 4) 
     echo ','; 
    } 

} 

問候 托比亞斯

+0

作爲概念證明,他可以在循環的每次運行中嘗試回顯count($ condition)。如果計數看起來不正確,那麼這可能是問題的根源(s) – orourkek 2012-04-11 19:56:24

+0

謝謝!我把它用在你的解決方案上,但是我在字符串的末尾加了一個逗號(',')。請參閱我的編輯以獲取更多信息。 – Erik 2012-04-11 20:09:04

+1

我剛剛修復了代碼末尾的if語句錯誤:) – Tobias 2012-04-11 20:14:25

0

你錯過計數()條件語句,這將從根本上使整個語句的回聲盈什麼都不做無論什麼計數()的結果。如果這是一個複製/粘貼錯誤,那麼你能提供更多的源代碼嗎?

+0

當然,我的代碼之前有'echo'。這是必需的!查看我的編輯完整循環並鏈接到我從中獲取信息的網站。 – Erik 2012-04-11 19:41:32

+0

@Erik - 不得不確定:] 你有沒有嘗試在整個有條件的環繞包圍括號?例如:echo((count($ condition)-1)<= 4)? ',':',eller';' – orourkek 2012-04-11 19:47:36

+0

我還沒有到現在:)但它並沒有制定訣竅。 – Erik 2012-04-11 19:48:58

0

我看的代碼...然後在您的打印例子,我注意到......你正在打印',''','eller'之後每個$條件。這會使事情變得複雜,因爲在您要檢查的條件產生之前,您正在打印並進行篩選。

逗號對於正確可讀的語法是必不可少的,所以你不管。

另一方面,'eller'可以在打印到屏幕之前進行檢查。

所以我的代碼的解釋會更喜歡這個...

$counter = 1; 
$amount = count($whome_answer[weather][forecast_conditions]); 
foreach($whome_answer[weather][forecast_conditions] AS $condition) { 
    if ($counter == 4) echo 'eller '; 
    echo $condition['day']; 
    if ($counter != $amount) echo ', '; 
    $counter++; 
} 

我假設您可以判斷你已經擁有的數據的一天,由「天」的關鍵爲每個$條件。

同樣,我注意到,如果count($condition)等於5,那麼您的邏輯(count($condition) -1) <= 4 ? ', ' : ', eller '將始終測試爲真,因此只會返回逗號。

編輯:作出更改以迴應新問題。