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 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚