03-12-2008, 01:15 PM
|
#15 (permalink)
|
|
The Contributor
Join Date: Feb 2008
Location: Croatia
Posts: 90
Thanks: 4
|
Just an addition.
I wanted to loop horizontaly - and add empty colums where needed.
so -
Code:
1 2 3 4
5 6 7 8
9 e e e
(e stands for empty column)
So here is a simple code for that :)
PHP Code:
<?php
$data = range( 1, 17 );
$out = array_chunk($data, 4) ;
// Check the data
function addEmptyColumn( $array ) {
foreach ( $array as $a => $r ) {
$c = count( $r );
if ( $c < 4) {
$loop = 4-$c;
for( $i=1; $i<=$loop; $i++) {
$r[] = '';
}
}
$kr[] = $r;
}
return $kr;
}
// Final output
$final = addEmptyColumn( $out );
?>
The table loop:
PHP Code:
<table width="300">
<?php foreach( $final as $row => $data ) { ?>
<tr>
<?php foreach( $data as $column ) {
if ( empty($column) ) $column = " ";
?>
<td><?php echo $column; ?></td>
<?php } ?>
</tr>
<?php }?>
</table>
Output:
HTML Code:
<table width="300">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
</tr>
<tr>
<td>13</td>
<td>14</td>
<td>15</td>
<td>16</td>
</tr>
<tr>
<td>17</td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
Hope this helps anybody. :D
__________________
Back from sysadmins to the programmers.
|
|
|