-1

This is my code:

$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);'/>";
}

How can I insert a <br> tag after 5 results because i don't want everything to be on one row.

1

2 Answers 2

2

Use the 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];
}

Output:

angrycoolcryhappyheart<br>kissmutesadsmile

Demo: https://eval.in/614166

Or with your actual code:

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);'/>";
}

Also note $data=$data . is the same as $data .= ....

Sign up to request clarification or add additional context in comments.

Comments

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>";
    }

}

1 Comment

That code mate is giving syntax error :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.