SitePoint Sponsor

# Thread: line breaks

1. ## line breaks in an array

I do I make an array print out with line breaks after every five items. So I can get something like

A B C D E
F G H I J

instead of:

A B C D E F G H I J

2. The thread below offers some insight into this:

http://www.sitepointforums.com/showt...hreadid=118283

I'd recommend using the modulus (sp?), which is the %, operator. It had slipped my mind while I was coming up with a solution

3. PHP Code:
``` for (\$i = 0; \$i < count(\$array); \$i++) {      echo \$array[\$i];      if (\$i%5 == 4)           echo "\n"; }  ```

4. thank you so much

5. Originally Posted by Andy Tomaka
I'd recommend using the modulus (sp?), which is the %, operator. It had slipped my mind while I was coming up with a solution
Modulo operator - singular
Modulus operator - plural
(You had it right )

Bonk's code has the general idea -- I think you should be checking for '0' after the modulo operation instead of 4, however. 4 may work, however, but the general idea of modulo is to return a remainder -- If the remainder is 0, then you know you've got a number that's divisible by 5.

6. Yeah, usually I do it that way, but since your going through an array, you need \$i to be 0 etc. And since your starting with 0 and not 1, the fifth one would be 4.

The modulus works like this: it divides the two numbers and returns the remainder, so 9%5 would be 4, 10%5 would be 0.

7. I can honestly say I have never heard it referred to as modulo. Thanks for the tip

8. okay i tried implementing this into existing code and it's giving me a problem.

it's printing AAAAAAAA BBBBBBBB CCCCCC DDDDD and so forth. What am I doing wrong?

Code:
```  <?php
\$quicklink_array = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
while (list(\$ql_pointer, \$quicklink) = each(\$quicklink_array)) {
\$get_title = \$mm->dbQuery("SELECT movie_title FROM movies_list WHERE movie_title LIKE '\$quicklink%' ORDER BY movie_title");
for(\$i = 0; \$i < count(\$quicklink_array); \$i++) {
if (! \$mm->dbCountRows(\$get_title)){

print ("\$quicklink&nbsp;");

} else {

print ("<a href=""\"index.php?module=movies&do=search&ql=\$quicklink\">\$quicklink</a>&nbsp;");
}
if((\$i % 5) == 4)
{
echo "<br>";

};
};
};
?>```

9. try this to cut the str \$str each 6th.letter:

Code:
```    \$str = "AAAAAABBBBBBCCCCCCDDDDDDEEEEEE";
\$newstr = wordwrap(\$str,6,"<bR>",1);
echo \$newstr;```
this will print out:

Code:
```AAAAAA
BBBBBB
CCCCCC
DDDDDD
EEEEEE```

10. PHP Code:
``` \$quicklink_array = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ); for(\$i = 0; \$i < count(\$quicklink_array); \$i++) {      \$current = \$quicklink_array[\$i];      \$get_title = \$mm->dbQuery("SELECT movie_title FROM movies_list WHERE movie_title LIKE '\$current%' ORDER BY movie_title" );      if (!\$mm->dbCountRows(\$get_title))           echo \$quicklink_array[\$i] . '&nbsp;';      else           print '<a href="index.php?module=movies&do=search&ql=' . \$quicklink_array[\$i] . '">' . \$quicklink_array[\$i] . '</a>&nbsp;';      if (\$i%5 == 4)            echo "<br>";  }  ```
You had the loop twice for every letter.

11. perfect, thanks

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•