dongying9756 2013-09-11 14:16
浏览 25
已采纳

PHP:列出了两个列列表

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!

  • 写回答

2条回答 默认 最新

  • dsz7121 2013-09-11 14:32
    关注

    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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型