2014-09-13 33 views
-3

有人可以幫助我試圖獲得循環的總數。在php中獲得循環總數

繼承人我的代碼

echo '<form name="fproduct" action="product.php" method="post"> 
<br/><textarea name="item" rows="10" cols="20"></textarea> <br/> 
<input type="submit" value="Submit" /> 
</form>'; 

if(isset($_POST['item'])) { 
    $j = 0; 
    $arry=explode("\\r\\n", $_POST['item']); 
    for ($i = 0; $i <= count($arry); $i++) { 
     if((trim($arry[$i])) != null) { 
      $j++; 
     } 
    } 
    Print $j; 
} 

哪些錯誤與此有關。

+1

嘗試'回聲$焦耳;'打印工作像'打印($ j)的;' – 2014-09-13 08:01:17

+0

什麼這樣的背景下?你試圖用換行符來計算內部項目的數量?但忽略空的空間? – Ghost 2014-09-13 08:13:38

+1

@CodeBaba [「print實際上並不是一個真正的函數(它是一種語言結構),所以你不需要在它的參數列表中使用圓括號。」](http://php.net/manual/en/function.print .php) – 2014-09-13 08:41:24

回答

0

嘗試單獨使用\n

if(isset($_POST['item'])){ 
    $j = 0; 
    $arry=explode("\n", $_POST['item']); 
    for ($i = 0; $i < count($arry); $i++) 
    { 
     if((trim($arry[$i]))!= null){ 
      $j++; 
     } 
    } 
    echo $j; 
} 

我建議使用foreach

if(isset($_POST['item'])){ 
    $count = 0; 
    $item = explode("\n", $_POST['item']); 
    foreach($item as $line) { 
     if(trim($line) != '') $count++; 
    } 
    echo $count; 
} 
+0

謝謝你的傢伙在foreach – 2014-09-13 09:37:50

+0

@JuliusMalundras確信老兄沒有問題很高興它幫助 – Ghost 2014-09-13 09:38:24