Niraj Chauhan
Niraj Chauhan

Reputation: 7880

PHP for loop for rendering limited data

I am using a flow player jquery to show latest 20 hot items, these items will be called directly from database.

The twist is the flow player divs.

One div can carry 5 items, like that I have to add 4 divs.

Each div contains just 5 items from the database, but the problem is how do I split the database equally in those 4 divs.

I tried the following code, but it just displays me the starting 5, How do I display the next 5 in another div and so on.....

$i = 0;
foreach($data as $idx=>$project_name)
{
    echo '<div>';
    echo $project_name['ProjectImage'];
    echo '</div>';
    if (++$i == 5) break;

}

And logic that will help me?

Upvotes: 2

Views: 470

Answers (6)

Gedrox
Gedrox

Reputation: 3612

I would use array_chunk()function for splitting the array into chunks first.

Upvotes: 0

hakre
hakre

Reputation: 197757

To solve your problem you could aquire the number of segments from the total elements of your data with a defined size of items per segment. Then you can iterate over the segements and within each segment you do the output (Demo):

$data = range(1, 32); # fake data

foreach(array_chunk($data, 5) as $items)
{
    # output per each segment:    

    echo '<div>';

    foreach($items as $item)
    {
        echo $item , " ";
    }

    echo '</div>', "\n";
}

Or for a more self-made chunking (Demo):

<?php
$data = range(1, 15); # fake data

$size = 5; # elements per div
$segments = range(1, count($data), $size); # segments

# iterate over segments if there are segments
if ($segments) while(each($segments))
{        
    # output per each segment:    

    echo '<div>';

    # iterate over the next $data elements, with a maximum of $size
    for($c = $size; $c-- && list($key, $item) = each($data);)
    {
        echo $item , " ";
    }

    echo '</div>', "\n";

}

In case you have the problem more than once inside your application and you want to re-use some code, you can encapsulate the needed logic inside an iterator if it's own:

class ArraySegments extends ArrayIterator {
    private $s;
    /**
     * @param array $array 
     * @param int $size of segment
     */
    public function __construct(array $array, $size) {
        $this->s = max(1, $size);
        parent::__construct($array);
    }
    public function current() {
        $segment = array();
        $c = $this->s;
        while($c && $this->valid()) {
            $segment[] = parent::current();
            --$c && $this->next();
        }
        return $segment; 
    }
}

Used as such:

$data = range(1, 15); # fake data

foreach(new ArraySegments($data, 5) as $elements) {
    printf(":::\n");
    foreach($elements as $element)
        printf(" - %s\n", $element)
        ;
    printf(";;;\n");
}

Just pass the array you want to iterate over ($data) and the number of items per segment (5 in the example). Inside the outer foreach you then have at max 5 elements inside the $elements array. Example output:

:::
 - 1
 - 2
 - 3
 - 4
 - 5
;;;
:::
 - 6
 - 7
 - 8
 - 9
 - 10
;;;
:::
 - 11
 - 12
 - 13
 - 14
 - 15
;;;

Upvotes: 0

Rukmi Patel
Rukmi Patel

Reputation: 2561

$i = 0;
foreach($data as $var=>$val) {
   if ($i == 0) echo '<div>';
   echo $project_name['val'];
   $i++;
   if ($i == 5) { 
      echo '</div>';
      $i = 0;
   }
}

in your code BREAK is the only problem... break will stop execution of loop and get you out of the loop. this code will work fine for you

Upvotes: 1

Hamid
Hamid

Reputation: 1760

$i = 1;
foreach($data as $idx=>$project_name) {
   if ($i == 1 || $i % 6 ==0) echo '<div>';
   echo $project_name['ProjectImage'];
   $i++;
   if ( $i % 6 == 0 || sizeof($data) == ( $i -1) ) {
      echo '</div>';
}

Upvotes: 0

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76880

I'd do:

$i = 1;
foreach($data as $idx=>$project_name)
{
 if (($i = 1) || (($i % 5) == 0)){
    echo '<div>';
}
    echo $project_name['ProjectImage'];
 if (($i = 1) || (($i % 5) == 0)){
    echo '</div>';
}
$i++

}

Upvotes: 0

sdolgy
sdolgy

Reputation: 7001

$i = 0;
foreach($data as $idx=>$project_name) {
   if ($i == 0) echo '<div>';
   echo $project_name['ProjectImage'];
   $i++;
   if ($i == 5) { 
      echo '</div>';
      $i = 0;
   }
}

Upvotes: 0

Related Questions