Monday, 21 March 2016

PHP - Alternating rows & column output

<?php

$items[334] = array( 'name' => 'L. Dalton', 'id' => 3456921 ); 
$items[234] = array( 'name' => 'B. Harris', 'id' => 563456 ); 
$items[212] = array( 'name' => 'R. Fox', 'id' => 6879879 ); 
$items[654] = array( 'name' => 'T. Marcus', 'id' => 14234 ); 
$items[865] = array( 'name' => 'M. Kitsap', 'id' => 79087 ); 
$items[875] = array( 'name' => 'P. McDonald', 'id' => 2454662 ); 
$items[675] = array( 'name' => 'J. Harvey', 'id' => 234545 ); 
$items[343] = array( 'name' => 'G. Steinmetz', 'id' => 8578768 ); 
$items[167] = array( 'name' => 'R. Stanley', 'id' => 34234 ); 
$items[899] = array( 'name' => 'C. Morris', 'id' => 546378 ); 
$items[932] = array( 'name' => 'S. Tan', 'id' => 578758 ); 


$counter = 1;
$limit = 4;
foreach($items as $item)
{
    if($limit == 4 && $counter == 1)
    {
        ?><div class="four-up"><?php echo "\n";
    }
    else if($limit == 3 && $counter == 1)
    {
        ?><div class="three-up"><?php echo "\n";
    }

    ?><div class="col col-<?php echo $counter; ?>"> <?php echo $item['name']; ?> <br /><?php echo $item['id']; ?></div><?php echo "\n";

    if($limit == $counter)
    {
        ?></div><?php echo "\n";
        $counter = 1;
        if($limit == 3) $limit = 4;
        else if($limit == 4) $limit = 3;
    }
    else
    {
        $counter++;
    }
} ?>

1 comment: