2017-08-24 3 views
0

を左にどのように多くの要素を数える:PHPのforeachのエコーX要素と私はPHPコードを持って

if($stmt->rowCount() > 0) : 
    $otherProfiles = array(); 
    foreach($playerOtherProfiles as $otherProfile) : 
     $otherProfiles[] = "<li>$otherProfile[Name]</li>"; 
    endforeach; 

    //echo implode(", ", $otherProfiles).. 

これはall otherProfilesを印刷します。 のみを印刷して、残りのプロファイル数を印刷できますか?例えば

プロファイルが4である場合 - プリント3及び1つの左エコー、

5であれば - プリント3,2左エコー..予め

答えて

2
インクリメンタ使用
if($stmt->rowCount() > 0) : 
    $otherProfiles = array(); 
    $i = 0; 
    $left = 0; 
    foreach($playerOtherProfiles as $otherProfile) { 
     if ($i==3){ 
     $left++; 
     } 
     else{ 
     $otherProfiles[] = "<li>$otherProfile[Name]</li>"; 
     $i++;} 
     } 
    echo $left . "left"; 
    endforeach; 
+1

ありがとうございます! – EvaldasL

1

のおかげでちょうど可変

$total_row=$stmt->rowCount(); 
$i=0; 
foreach($playerOtherProfiles as $otherProfile) : 
    if($i<3){ 
    $otherProfiles[] = "<li>$otherProfile[Name]</li>"; 
    $i++; 
    }else{ break; } 
endforeach; 
echo "Left - :".$toal_row-$i; 
+0

ありがとうございました! ;)) – EvaldasL

関連する問題