php
  • arrays
  • 2016-07-30 53 views -1 likes 
    -1

    之後添加
    這是我的代碼:PHP數組和循環 - 5個resultss

    $sm2 = array("angry", "cool", "cry", "happy", "heart", "kiss", "mute", "sad", "smile"); 
    
    for($j=0;$j<count($sm2); $j++) { 
        $data=$data . "<img id='". $sm2[$j] ."' src='images/emotions/" . $sm2[$j] . ".png' data-toggle='tooltip' title=". $sm2[$j] ." width='32' height='32' style='margin:5px;' 
        onclick='insertEmoticons(this.id);'/>"; 
    } 
    

    我怎樣才能插入後5個結果<br>標籤,因爲我不想一切是在一行。

    +0

    模數'($ i%5 == 0)' - 按照http://stackoverflow.com/questions/8135404/php-modulus-in-a-loop這也是一個可能的重複 –

    回答

    2

    使用Modulus operator

    <?php 
    $sm2 = array("angry", "cool", "cry", "happy", "heart", "kiss", "mute", "sad", "smile"); 
    for($j=0;$j<count($sm2); $j++) { 
        if(!empty($j) && $j % 5 == 0) { 
          echo '<br>'; 
        } 
        echo $sm2[$j]; 
    } 
    

    輸出:

    angrycoolcryhappyheart<br>kissmutesadsmile 
    

    演示:https://eval.in/614166

    或與您的實際代碼:

    for($j=0;$j<count($sm2); $j++) { 
        if(!empty($j) && $j % 5 == 0) { 
          $data .= '<br>'; 
        } 
        $data=$data . "<img id='". $sm2[$j] ."' src='images/emotions/" . $sm2[$j] . ".png' data-toggle='tooltip' title=". $sm2[$j] ." width='32' height='32' style='margin:5px;' 
        onclick='insertEmoticons(this.id);'/>"; 
    } 
    

    還要注意$data=$data .相同$data .= ...

    +0

    工程就像一個魅力!謝謝 :) – Mensur

    0
    $sm2 = array("angry", "cool", "cry", "happy", "heart", "kiss", "mute", "sad", "smile"); 
    
    for($j=0;$j<count($sm2); $j++) { 
        $data=$data . "<img id='". $sm2[$j] ."' src='images/emotions/" . $sm2[$j] . ".png' data-toggle='tooltip' title=". $sm2[$j] ." width='32' height='32' style='margin:5px;' onclick='insertEmoticons(this.id);'/>"; 
        if ((($j+1) % 5) == 0) 
        { 
         $data = $data . "<br>"; 
        } 
    
    } 
    
    +0

    該代碼隊友給語法錯誤:) – Mensur

    相關問題