ADM
ADM

Reputation: 1610

PHP: two column lists lay out

I'm listing some movies in my web so the user can choose the best movie.

I’m classifying them by genre.

But the lists under each genre are too long and I want to make two columns of titles.

The number of movies in each column will depend of the genre.

So my code is like this now:

$i = 1;
echo "<ul>";
foreach($arrayMovies  as $k=>$v) 
{
echo  "<li><input type=\"checkbox\" id=\"flat-$i\" name=\"$genre-$i\" value=\"$k\">
 <label for=\"flat-$i\">$v</label></li>";
 $i++;
} 
echo "</ul>";

This code is showing the long list, something like this for drama:

Forest Gump

The Hours

Mullholand Drive

Titanic

.

.

Let’s say that for the drama genre I need two titles per column:

<ul>
<li> Forest Gump</li>
<li> The Hours</li>
</ul>
 <ul>
<li> Mullholand Drive</li>
<li> Titanic</li>
</ul>

How can achieve this with my code??

The second column I can achieve by using css, I just need a new <ul> after two titles.

Please notice that the number two in -two titles per column- is a dynamic number*

*I’ll count how many rows per title each genre has to get a total number and then I’ll do half of it to get the number of titles per column (the number of titles in each genre is even)

Thanks very much!

Upvotes: 0

Views: 159

Answers (2)

briosheje
briosheje

Reputation: 7446

Sorry for the previous answer, I didn't understand what you wanted to exactly do.

If the problem is: "I want to categorize movies", then use an associative array instead.

Else, if you just want to display two movies in two coloumns, I would suggest you this:

$i = 1;
$flagCounter = 0;
echo "<ul>";
foreach($arrayMovies  as $k=>$v) 
{
if ($flagCounter != 2) {
   echo  "<li><input type=\"checkbox\" id=\"flat-$i\" name=\"$genre-$i\" value=\"$k\">
 <label for=\"flat-$i\">$v</label></li>";
 $i++;
   $flagCounter++;
}
else {
   echo "</ul><ul>";
   echo  "<li><input type=\"checkbox\" id=\"flat-$i\" name=\"$genre-$i\" value=\"$k\">
 <label for=\"flat-$i\">$v</label></li>";
 $i++;
   $flagCounter = 1;
}

} 
echo "</ul>";

To be honest at all, I don't really trust this solution, this is just a workaround for your case. If you're interested in a better one, try using an associative array with a structure like such:

$yourArray = array ( "Category" => array ("movie1","movie2") [and so on...] );

In this way, you will be able to display everything more clearly, being able to access to both movies and categories!

Also, why did you choose to use a list instead of a table for displaying these?

And.. Where does the variable $genre come from? Is this all your code or what? If not, can you please provide us the structure of your array?

Upvotes: 0

feeela
feeela

Reputation: 29932

Just insert those UL tags at the half of the iterations:

$arrayMovies = array( 'Forest Gump', 'The Hours', 'Mullholand Drive', 'Titanic', 'The Intouchables' );
$arrayMoviesCount = count( $arrayMovies );

echo '<ul>';
for( $i = 0; $i < $arrayMoviesCount; $i++ )
{
    if( ceil( $arrayMoviesCount / 2 ) == $i )
    {
        echo '</ul><ul>';
    }
    echo sprintf( '<li>%s</li>', $arrayMovies[$i] );
}
echo '</ul>';

Output of the above code:

<ul>
    <li>Forest Gump</li>
    <li>The Hours</li>
    <li>Mullholand Drive</li>
</ul>
<ul>
    <li>Titanic</li>
    <li>The Intouchables</li>
</ul>

The ceil is required for lists with an odd number of entries; using ceil will result in the first list having one entry more. If the additional (not-even) entry should be displayed in the second list, you could replace ceil by floor.

Upvotes: 1

Related Questions