使用不同的行span';它正在使用php进行动态更新
嗨,昨晚有人通过另一个问题帮我写了一些代码: 我对它做了一些修改,我又有点卡住了 基本上,这是一系列电影。我已将其格式化为包含一个单独的条目,以便在数组中的第一个字母发生变化时使用,其中包含第一个字母 这是执行以下操作的代码:使用不同的行span';它正在使用php进行动态更新,php,html,Php,Html,嗨,昨晚有人通过另一个问题帮我写了一些代码: 我对它做了一些修改,我又有点卡住了 基本上,这是一系列电影。我已将其格式化为包含一个单独的条目,以便在数组中的第一个字母发生变化时使用,其中包含第一个字母 这是执行以下操作的代码: while (false !== ($entry = readdir($handle)) ) { if($entry == "." or $entry == "..") continue; if($i == 0) { a
while (false !== ($entry = readdir($handle)) ) {
if($entry == "." or $entry == "..")
continue;
if($i == 0) {
array_push($results,substr($entry, 0,1));
} else if (substr($entry, 0,1) != substr($lastEntry, 0,1)) {
array_push($results,substr($entry, 0,1));
}
$lastEntry = $entry;
array_push($results,$entry);
$i++;
}
这将产生如下数组:
Array (
[0] => 0
[1] => 007, A View to a Kill (1985)
[2] => 1
[3] => 127 Hours (2010)
[4] => A
[5] => A Clockwork Orange (1971)
[6] => B
[7] => Back to the Future (1985)
[8] => Butterfly on a Wheel (2007)
[9] => C
[10] => Carnage (2011)
[11] => Casino (1995)
[12] => D
[13] => Defiance (2008)
)
所以基本上我想生成一个表,给每个索引(0,1,a,C等)一个不同的CSS类,并给它一个2的行跨度。这是我做的。我制作了一张看起来正确的桌子。下面的代码在添加一个rowspan=2的单元格时,会增加$indexRow,然后下一行打印了数量为\u的列-$indexRow。由于上述行中的rowspan=2单元格占用了当前行中的一个单元格
$NUM_COLUMNS = 3;
$numRows = count($results) / $NUM_COLUMNS;
if (count($results) % $NUM_COLUMNS > 0) {
$numRows += 1;
}
echo "<div align=\"center\"><table>";
$i=0;
$indexRow=0;
for ($i = 0; $i < $numRows; $i++) {
echo "<tr>\n";
$index = $i;
$j=0;
if($indexRow > 0) {
$j+=$indexRow;
$indexRow=0;
}
while ($j < $NUM_COLUMNS) {
$entry = '';
if ($index < count($results)) {
$entry = $results[$index];
}
if(strlen($entry) < 2) {
echo "\t<td rowspan=\"2\" class=\"movieindex\">" . $entry . "</td>\n";
$indexRow++;
} else {
echo "\t<td>" . $entry . "</td>\n";
}
$index += $numRows;
$j++;
}
echo "</tr>\n";
}
echo "</table></div>";
$NUM_COLUMNS=3;
$numRows=计数($results)/$NUM_列;
如果(计数($results)%$NUM_列>0){
$numRows+=1;
}
回声“;
$i=0;
$indexRow=0;
对于($i=0;$i<$numRows;$i++){
回音“\n”;
$index=$i;
$j=0;
如果($indexRow>0){
$j+=$indexRow;
$indexRow=0;
}
而($j<$NUM_列){
$entry='';
如果($index
我的实际问题是,rowspan=2单元格的电影现在没有正确排序。电影应该垂直排列,但会被索引单元格丢弃。我不知道我怎么才能避开这个,有什么想法吗
这是我得到的表格:
<div align="center">
<table>
<tr>
<td rowspan="2" class="movieindex">0</td>
<td>Hancock (2008)</td>
<td>Sin City (2005)</td>
</tr>
<tr>
<td>007, A View to a Kill (1985)</td>
<td>Hangover (2009)</td>
</tr>
<tr>
<td>007, Diamonds Are Forever (1971)</td>
<td>Happy Feet Two (2011)</td>
<td>Snow White (1987)</td>
</tr>
0
汉考克(2008)
罪恶之城(2005)
007,《杀戮的视角》(1985)
宿醉(2009)
007,钻石是永恒的(1971)
快乐双足(2011)
白雪公主(1987)
这就是我想要的:
<div align="center">
<table>
<tr>
<td rowspan="2" class="movieindex">0</td>
<td>Hancock (2008)</td>
<td>Sin City (2005)</td>
</tr>
<tr>
<td>Hangover (2009)</td>
<td>Snow White (1987)</td>
</tr>
<tr>
<td>007, A View to a Kill (1985)</td>
<td>Happy Feet Two (2011)</td>
<td>Step Brothers (2008)</td>
</tr>
0
汉考克(2008)
罪恶之城(2005)
宿醉(2009)
白雪公主(1987)
007,《杀戮的视角》(1985)
快乐双足(2011)
继兄弟(2008)
如果需要更多的解释,请询问,我会编辑。
基本上,我要问的是,如何正确地垂直排列单元格
也许这会帮助你理解我想做什么:
正如我在评论中所说,作为一种脏解决方案,您可以在索引后的数组中添加占位符,而不在代码中显示它们
} else if (substr($entry, 0,1) != substr($lastEntry, 0,1)) {
array_push($results,substr($entry, 0,1));
array_push($results,"");
}
这将产生如下数组:
Array (
[0] => 0
[1] =>
[2] => 007, A View to a Kill (1985)
[3] => 1
[4] =>
[5] => 127 Hours (2010)
);
如果将代码修改为:
if(strlen($entry) == 1) {
echo "\t<td rowspan=\"2\" class=\"movieindex\">" . $entry . "</td>\n";
$indexRow++; // This might be unneeded now, did not test this myself
} else if(strlen($entry) == 0){
// Do nothing, just a placeholder
} else {
echo "\t<td>" . $entry . "</td>\n";
}
if(strlen($entry)==1){
回显“\t”。$entry.\n”;
$indexRow++;//现在可能不需要这个,我自己没有测试过
}else if(strlen($entry)==0){
//什么也不做,只是一个占位符
}否则{
回显“\t”。$entry.\n”;
}
很抱歉,我的帖子有一半被删了。我现在就编辑它,可能需要5-10分钟。一个肮脏的解决方案是在每个字母后面的数组中添加一个占位符,而不是在制作表格时打印出来。我不明白你的意思。我希望索引显示在表中,$indexRow++
可能必须删除,但没有测试此代码。这不太有效,我认为这与计算行数有关,因为现在数组中填充了空元素。也许我可以计算非空数组元素。不抱歉,我忘了将if(strlen($entry)<2){更改为if(strlen($entry)==1){。谢谢,效果很好!