Hi someone helped me write some of this code last night through another question: Transform html table
I have made some changes to it and I’ma little stuck again.
Basically $results is an array of films. I have formatted it to contain a separate entry for whenever the first letter changes in the array, containing the first letter.
This is the code which does that:
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++;
}
This results in an array like so:
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)
)
So basically I want to produce a table which gives each index (0,1,A,C,etc) a different CSS class and give it a rowspan of 2. This I have done. I produce a table which looks correct. The code below increments $indexRow when it adds a cell with rowspan=2 and then the next row has number_of_cols – $indexRow printed. As the rowspan=2 cell from the above row occupies one of the cells on the current row.
$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>";
My actual problem is that the movies are not ordered correctly now with the rowspan=2 cells. The movies should be ordered vertically, but get thrown off by the index cells. I’m not sure how I can circumvent this, any ideas?
This is the table I get:
<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>
This is what I want:
<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>
If any more explanation is needed please just ask and I will edit.
Basically what I’m asking is how do I order the cells vertically, correctly?
Maybe this will help you to understand what I’m trying to do:
http://imageshack.us/f/163/exampletable.png/
As I said in my comment, as a dirty solution, you could add a placeholder in your array after your indexes and don’t display them in your code.
Which will result in an array like this:
If you modify your code to this: