0

I am looking to offset a group of PHP array results to create multiple rows of data.

For example, the first row will contain the first four array results, with the second row displaying results 5-8 and the third row containing results 9-12.

Currently, I am printing out all 12 in one list, however I'd like a bit more display control (i.e. ordering the results into columns which I can style independent of each), hence why I'd like to offset the results.

My current PHP is below:

<?php    
if (empty($tmdb_cast['cast'])) {

} else {?>
    <section class="cast">
        <p class="title">Cast</p>
        <ul class="section_body"><?php
            foreach($tmdb_cast['cast'] as $key => $castMember){
                if ($key < 12) {?>
                    <li class="actor_instance clearfix"><?php
                        if ($castMember['profile_path'] != '') {?>
                            <img src="http://cf2.imgobject.com/t/p/w45<?php echo $castMember['profile_path']; ?>" class="actor_image pull-left" alt="<?php echo $castMember['name']; ?>" title="<?php echo $castMember['name']; ?>" /><?php
                        } else {?>
                            <img src="assets/images/castpic_unavailable.png" class="actor_image pull-left" alt="No image available" title="No image available" /><?php
                        }?>
                    <p class="actor"><?php echo $castMember['character']; ?> - <a href="actor.php?id=<?php echo $castMember['id']; ?>"><?php echo $castMember['name']; ?></a></p>
                    </li><?php
                }
            }?>

            <div class="morecast pull-right"><a href="http://www.imdb.com/title/<?php echo $imdb_id; ?>/fullcredits" title="View full cast list on IMDb">[...view all cast]</a></div>
        </ul>
    </section><?php
}?>

P.S. Apologies for how I've formatted the code in the above block - I'm still not 100% sure how to make it look "nice" on StackOverflow.

2
  • it would be much easier to simply list all then to set your lis as floating elements with a fixed width = total available space / 4. Commented Feb 24, 2013 at 12:30
  • Note that div elements inside ul elements are invalid HTML. Also, you might find the alternative syntax for control structures easier to handle with you mix PHP with HTML: php.net/manual/en/control-structures.alternative-syntax.php. Commented Feb 24, 2013 at 12:32

3 Answers 3

2

Use array_chunk to break a single dimension array into a 2D array. Then you can loop through each chunk, and then each result, to get that "offset" effect between them.

$chunks = array_chunk($tmdb_cast['cast'], 4); // 4 here, is the number you want each group to have 

foreach($chunks as $key => $chunk)
{
    foreach($chunk as $castMember) 
    {
         //display castMember here
    }
    // add code between groups of 4 cast members here
}
Sign up to request clarification or add additional context in comments.

Comments

0

First: mixing php and html this way is a very bad habbit... one day you will have to maintain your code and that day you will vomit all over your desk because you mixed different languages in one file... That being said.. and without creating a new array:

foreach($tmdb_cast['cast'] as $key => $castMember)
{
    if ($key < 12)
    {
        // your code goes here

        // if key is 3, or 7 the code below will close the listcontainer and open a new one...
        if( ($key+1)%4 == 0 AND $key <11)
            echo '</ul> <ul class="section_body">';
    }
}

Also replace this:

if (empty($tmdb_cast['cast']))
{
}
else {

with this:

if (!empty($tmdb_cast['cast']))
{

Comments

0

I'm not entirely sure what you're looking for but here's how I would format your code. Alternative syntax for 'if' is a little more readable, and the use of for loops instead of a foreach will give you the control over row numbers you say you're looking for.

<?php   if( ! empty($tmdb_cast['cast'])): ?>
    <section class="cast">
        <p class="title">Cast</p>
        <ul class="section_body">
<?php
            for($i = 0; $i < 12; $i++):
            $castMember = $tmdb_cast['cast'][$i];
?>
            <li class="actor_instance clearfix">
<?php           if($castMember['profile_path'] != ''): ?>
                <img src="http://cf2.imgobject.com/t/p/w45<?php echo $castMember['profile_path']; ?>" class="actor_image pull-left" alt="<?php echo $castMember['name']; ?>" title="<?php echo $castMember['name']; ?>" />
<?php           else: ?>
                <img src="assets/images/castpic_unavailable.png" class="actor_image pull-left" alt="No image available" title="No image available" />
<?php           endif; ?>
                <p class="actor"><?php echo $castMember['character']; ?> - <a href="actor.php?id=<?php echo $castMember['id']; ?>"><?php echo $castMember['name']; ?></a></p>
            </li>
<?php       endfor; ?>

            <div class="morecast pull-right"><a href="http://www.imdb.com/title/<?php echo $imdb_id; ?>/fullcredits" title="View full cast list on IMDb">[...view all cast]</a></div>
        </ul>
    </section>
<?php   endif; ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.