Francisco
Francisco

Reputation: 61

Count elements in a loop

I'm trying to count elements in a loop to break each number of elements and show in groups

My little script

$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";

$exp_filter = explode("*", trim($data));
for ($x = 0; $x <= count($exp_filter); $x++)
{
    print "".$exp_filter[$x]."";
    if ($x%5 == 0)
    {
        print "<br>";   
    }

}

As you can see in the little script each 5 rounds I want show the tag
for break and show as in groups of elements.

The problem it´s always show in the first line one element and after this the rest, and no works fine.

Upvotes: 1

Views: 83

Answers (4)

ekad
ekad

Reputation: 14614

The index of $exp_filter starts at 0, so this block of code

if ($x % 5 == 0)
{
    print "<br>";   
}

should be

if (($x+1) % 5 == 0)
{
    print "<br>";   
}

Here's the complete modified code

$data = "house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";

$exp_filter = explode("*", trim($data));
for ($x = 0; $x <= count($exp_filter); $x++)
{
    print "".$exp_filter[$x]."";
    if (($x + 1) % 5 == 0)
    {
        print "<br>";   
    }
}

Working example: http://codepad.org/iEsKK98M

Upvotes: 1

Steve
Steve

Reputation: 20469

I would use array chunk and implode instead:

$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";

foreach(array_chunk(explode('*', $data), 5) as $chunk){
    echo implode(' ', $chunk) . '<br>';
}

Live example: http://codepad.viper-7.com/ED2wHR

Upvotes: 1

ummahusla
ummahusla

Reputation: 2063

Quickfix:

Demo

<?php

$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";

$exp_filter=explode("*",trim($data));
for($x=1;$x<=count($exp_filter);$x++)
{
print "".$exp_filter[$x]."";
if($x > 0 && $x%5==0) 
{
echo "<br />";   
}

}
?>

Upvotes: 1

Ares Draguna
Ares Draguna

Reputation: 1661

$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";

$exp_filter=explode("*",trim($data));
for($x=1;$x<=count($exp_filter);$x++)
{
  print "".$exp_filter[$x]."";
  if($x%5==0) 
  {
    print "<br>";   
  }
}

Try this. The problem is you started at 0 in for, you should start from 1 ;)

Upvotes: 1

Related Questions