php
  • arrays
  • 2016-07-30 54 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

    相关问题