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!
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.
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?
Related
I'm trying to list all terms on a custom taxonomy but I wanted to at least group them into 3 columns including their children to have a visual balance. Here's what I've done so far. I'm stuck on creating the loop after it reached the maximum term. On what I have, It wrapped all the succeeding items with 'ul' instead of creating a second ul and list the next batch. After it reaches the x amount of term it should create another 'ul' element listing categories in it. There will be a total of 3 columns.
<?php
$get_cats = wp_list_categories( 'echo=0&title_li=&depth=2&hide_empty=0,&taxonomy=industries' );
// Split into array items
$cat_array = explode('</li>',$get_cats);
// Amount of categories (count of items in array)
$results_total = count($cat_array);
// How many categories to show per list (round up total divided by 3)
$cats_per_list = ceil($results_total / 3);
// Counter number for tagging onto each list
$list_number = 1;
// Set the category result counter to zero
$result_number = 0;
?>
<?php echo $cats_per_list ; ?>
<ul class="cat_col" id="cat-col-<?php echo $list_number; ?>">
<?php
foreach($cat_array as $category) {
$result_number++;
if($result_number >= $cats_per_list) {
$list_number++;
echo $category.'</li> </ul> <ul class="cat_col" id="cat-col-'.$list_number.'">';
}
else {
echo $category.'</li>';
}
}
?>
</ul>
The code is very buggy. Just a couple of observations:
Next in the
if($result_number >= $cats_per_list) {
Block you are going to have to reset result_number to 0 since the count starts over again. Your current code would only meet that condition once since $cats_per_list is defined as the average of the total amount. After that it would continue counting up and ALWAYS be >= $cats_per_list
Next: it's quibble but you probably don't need to ceil the result since you are using >=, that operation pretty much does the same thing since 1.5 will meet the criteria of >= 1 as a for instance.
Try this and see if it is any better:
<?php
$get_cats = wp_list_categories( 'echo=0&title_li=&depth=2&hide_empty=0,&taxonomy=industries' );
// Split into array items
$cat_array = explode('</li>',$get_cats);
// Amount of categories (count of items in array)
$results_total = count($cat_array);
// How many categories to show per list (round up total divided by 3)
$cats_per_list = ceil($results_total / 3);
// Counter number for tagging onto each list
$list_number = 1;
// Set the category result counter to zero
$result_number = 0;
?>
<?php echo $cats_per_list ; ?>
<ul class="cat_col" id="cat-col-<?php echo $list_number; ?>">
<?php
foreach($cat_array as $category) {
$result_number++;
if($result_number >= $cats_per_list) {
$result_number = 0;
$list_number++;
echo $category.'</li> </ul> <ul class="cat_col" id="cat-col-'.$list_number.'">';
}
else {
echo $category.'</li>';
}
}
?>
</ul>
This is my first question, i hope a lot of this site. My friends talk very good about it.
I have a code with categorie list in 3 columns, but i want add word "Asesor " before each category.
Example:
Asesor Category 1
Asesor Category 2
This is my code:
<?php
// Grab the categories - top level only (depth=1)
$get_cats = wp_list_categories( 'echo=0&title_li=&depth=1&hide_empty=0&exclude=1,762,899,951' );
// Split into array items
$cat_array = explode('</li>',$get_cats);
// Amount of categories (count of items in array)
$results_total = count($cat_array);
// How many categories to show per list (round up total divided by 3)
$cats_per_list = ceil($results_total / 3);
// Counter number for tagging onto each list
$list_number = 1;
// Set the category result counter to zero
$result_number = 0;
?>
<ul class="cat_col" id="cat-col-<?php echo $list_number; ?>">
<?php
foreach($cat_array as $category) {
$result_number++;
if($result_number % $cats_per_list == 0) {
$list_number++;
echo $category.'</li>
</ul>
<ul class="cat_col" id="cat-col-'.$list_number.'">';
}
else
echo $category.'</li>';
}
?>
</ul>
I try add this in one line, but don't work:
echo "Asesor" .$category.'</li>';
Anybody can help me please? Really thanks!
I suggest you to use
http://codex.wordpress.org/Function_Reference/get_categories instead.
It will retrieve array of objects by default so you can just loop through and echo what you want
Sorry if the title is a bit confusing, I tried my best!
So basically I have 4 column divs that each contains 5 links for example. The links come from a table in my database, so new links are added and some others deleted, hence why I need to do it via database instead of writing it by hand. Now my issue is to divide the array in order to show 5 links per column (so when 5 links have been placed, div is closed and a new one is opened, unless there is no more link.
for example:
<div class="column">
Link
Link
Link
Link
Link
</div>
<div class="column">
Link
Link
Link
Link
Link
</div>
etc.
Thanks to anyone!
The PHP function array_chunk is nice to distribute an array of links into columns:
$columns = array_chunk($rows, 5);
foreach ($columns as $links)
{
echo '<div class="column">', "\n";
foreach ($links as $link)
printf("Link", $link);
echo '</div>', "\n";
}
I don't know your column names nor how you query the database, so I don't have any array indexes here written out. But I think you'll get the idea.
How about using the modulo operator
<div class="column">
for ($i = 0; $i < $nRow; $i++) {
if ($i % 5 == 0 && $i) {
echo '</div><div class="column">';
}
echo "<a href='{$links[$i]}'>Link</a>";
}
</div>
how can a list of values that select of database, put in two columns together by PHP ?
EXAMPLE:
values select of database:
Internet
Game Notes
Internet
Pool
Coffee
Game Notes
i want like this:
Row-first order
<table>
<?php
$left = true;
foreach ($values as $value){
if ($left)
echo "<tr>";
echo "<td>$value</td>";
if (!$left)
echo "</tr>";
$left = !$left;
}
?>
</table>
With column-first order (as in your sample) you'll have to involve CSS and it's much complex. Something like
<div class='inline_div'>
<?php
$middle = count($values)/2+1;
$count = 0;
foreach ($values as $value){
if ($count==$middle)
echo "</div><div class='inline_div'>";
echo "$value<br/>";
++$count;
}
?>
</div>
inline_div is something like .inline_div {display:inline; float:left}. But that will definitely not work as expected, I'm no CSS master. IE does not support display:inline for sure.
If you want it exactly like you want in your example then you are going to have to read the whole table into memory, calculate the mid-point element and then build your table from that using a base point, an offset and a check to ensure that you've not repeated anything which involves a whole load of calculation.
A way around this would be to build two separate tables (each containing a single column) and then enclose them in a single table with two columns:
<?php
$list=array('a','b','c','d','e','f');
$midpoint=floor(count($list)/2);
$tableHeader='<table width="100%">';
$tableFooter='</table>';
$leftTable=$tableHeader;
$rightTable=$tableHeader;
for ($c=0; $c<$midpoint; $c++)
{
$leftTable.='<tr><td width="100%">'.$list[$c].'</td></tr>';
}
$leftTable.=$tableFooter;
for ($c=$midpoint; $c<count($list); $c++)
{
$rightTable.='<tr><td width="100%">'.$list[$c].'</td></tr>';
}
$rightTable.=$tableFooter;
$mainTable='<table><tr><td width="50%">'.$leftTable.'</td><td width="50%">'.$rightTable.'</td></tr></table>';
echo $mainTable;
?>
Or something along those lines. I haven't tested this code but it would be pretty close (may have to adjust the values in the "for" sections
The simple solution would be to use two divs. As a previous poster commented, you first need to count the elements. Suppose the items you want to display are in an array $items You can use this kind of code
<?php
$divItemCount = (count($items)%2) ? count($items)/2 + 1 : count($items)/2;
?>
<div id="leftdiv" style="width: 30%;">
<ul>
<?php
for($i=0; $i<$divItemCount; $i++) {
echo '<li>'. $items[$i] .'</li>';
}?>
</ul></div><div id="rightdiv" style="width: 30%; float:left"><ul>
<?php
for($j=$i; $j<count($items); $j++) {
echo '<li>'. $items[$j] .'</li>';
}?>
</ul></div>
I have not tested the code so there may be errors. You can use the border property in the divs to create a custom separator between the two lists.
So what I'm trying to do is select all the distinct months from my database and then print them in a list. That, I can accomplish. The problem lies in the fact that I need my list to be two column. The way that I achieve this with CSS is by using 2 different div's "left" and "right" which are floated next to each other. This poses a problem with PHP because it needs to echo a div close and a new div open after it echoes the sixth month. Then it needs to start again from where it left off and finish. I can't just list all of the months in the HTML, either because I don't want it to list a month if I don't have any records in the DB for that month, yet. Any ideas? I hope I was clear enough!
Thanks!
-williamg
Something like this should work (the basic idea being to just keep a count of the months an increment it as you loop through them):
<div class="left">
<?php
$x = 1;
foreach($months as $month) {
# switch to the right div on the 7th month
if ($x == 7) {
echo '</div><div class="right">';
}
echo "<div class=\"row\">{$month}</div>";
# increment x for each row
$x++;
}
</div>
<?php
$numberOfMonths = count($months);
$halfwayPoint = ceil($numberOfMonths / 2);
echo "<div class=\"left\">";
for($i=0; $i<$halfwayPoint; $i++){
echo $months[$i] . "<br />";
}
echo "</div><div class=\"right\">";
for($i=$halfwayPoint; $i<$numberOfMonths; $i++){
echo $months[$i] . "<br />";
}
echo "</div>";
?>
Rant: on
When displaying tabular data, use table instead of floating div. It will make sense when viewing the page with css disabled. If you use floated div, then you data will displayed all way down. Not all table usage is bad. People often hate table so much, so using floated div. Table only bad when used for page layout.
Rant: off
When I need to have certain content displayed with some open, close, and in-between extra character, I will make use of implode. This is the example:
$data = array('column 1', 'column 2');
$output = '<div>'.implode('</div><div>', $data).'</div>';
//result: <div>column 1</div><div>column 2</div>
You can extends this to almost anything. Array and implode is the power that php have for many years. You will never needed any if to check if it last element, then insert the closing character, or check if it first element, then insert opening character, or print the additional character between elements.
Hope this help.
Update:
My bad for misread the main problems asked. Sorry for the rant ;)
Here is my code to make a data displayed in 2 column:
//for example, I use array. This should be a result from database
$data = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
//should be 12 month, but this case there are only 9 of it
for ( $i = 0; $i <= 5; $i++)
{
//here I do a half loop, since there a fixed number of data and the item for first column
$output = '<div class="left">'.$data[$i].'</div>';
if ( isset($data[$i+6] )
{
$output = '<div class="right">'.$data[$i+6].'</div>';
}
echo $output."\n";
}
//the result should be
//<div class="left">1</div><div class="right">7</div>
//<div class="left">2</div><div class="right">8</div>
//<div class="left">3</div><div class="right">9</div>
//<div class="left">4</div>
//<div class="left">5</div>
//<div class="left">6</div>
Other solution is using CSS to format the output, so you just put the div top to down, then the css make the parent container only fit the 6 item vertically, and put the rest to the right of existing content. I don't know much about it, since it usually provided by fellow css designer or my client.
Example assumes you have an array of objects.
<div style="width:150px; float:left;">
<ul>
<?php
$c = count($categories);
$s = ($c / 3); // change 3 to the number of columns you want to have.
$i=1;
foreach($categories as $category)
{
echo '<li>' . $category->CategoryLabel . '</a></li>';
if($i != 0 && $i % $s == 0)
{
?>
</ul>
</div>
<div style="width:150px; float:left;">
<ul>
<?php
}
$i++;
}
?>
</ul>
</div>