简体   繁体   中英

PHP - create dynamic html table with spacer columns

I am trying to create a dynamic grid (HTML table) using php.

I need to make a table with 5 columns per row and all the odd columns will have a width of 32% whereas the even columns will be empty but have a width of 2% which is used as spacers.

I also need it so if any row doesn't have 3 odd columns, it must generate the rest to make 5 even if they are empty.

I've managed to do it so it creates 3 columns of 32% width and adds empty TDs if it needs to be I haven't been able to create the smaller 2% columns.

This is going to be used as a table grid for an HTML email

This is what I have working at the minute but only 3 columns

<?php 
    $test = array(1,2,3,4,5);
    $count = 0;
?>
<html>
<head>
<style>
.test {border: 1px solid #ccc;}
</style>
</head>
<body>
<table cellspacing="0" cellpadding="0" border="0" width="650">
    <?php foreach($test as $t): ?>
    <?php if ($count % 3 == 0) echo "<tr>";  # new row ?>
    <td width="32%" class="test">
        <p>ddfhfdhfgh</p>
        <p>sdgdfgdgd</p>
    </td>
    <?php $count++; ?>
    <?php if ($count % 3 == 0) echo "</tr>\n";  # end row ?>
    <?php endforeach; ?>
    <?php if ($count % 3 != 0): ?>
    <?php while ($count++ % 3): ?>
    <td width="32%">&nbsp;</td>
    <?php endwhile; ?>
    </tr>
    <?php endif; ?>
</table>
</body>
</html>

and I've tried adding this but it makes a mess.

<?php if ($count % 3 == 0) echo "<tr>";  # new row ?>

    <?php if ( $count & 1 ): ?>
        <td width="32%" class="test">
            <p>ddfhfdhfgh</p>
            <p>sdgdfgdgd</p>
        </td>
    <?php else: ?>
        <td width="2%">&nbsp;</td>
    <?php endif; ?>

<?php $count++; ?>

All i need is a table with 3 large columns and 2 spacer columns in between

I think this should do the trick.

You were making it a bit more complex than it needs to be.

First you can make use of the foreach syntax like this foreach ($array as $index => $value) so you dont need to keep your own counter.

Second, you basically want every other column to take a different size so use % 2 and not % 3 and a simple if then else construct.

It also makes code easier to read if you dont use the php start and stop tags <?php ... ?> around every line of PHP, if you have more than one line of consecutive php code just use one to start the interpreter above the first line and one after the last line of PHP code. Otherwise it tends to Fry the brain trying to read and understand the code.

<?php 
    $test = array(1,2,3,4,5);
?>
<html>
<head>
<style>
.test {border: 1px solid #ccc;}
</style>
</head>
<body>
<table cellspacing="0" cellpadding="0" border="0" width="650">
<?php 
    echo '<tr>';
    foreach($test as $i => $t): 

        if ( $i % 2 == 0 ) :
            echo '<td width="32%" class="test">';
                echo '<p>ddfhfdhfgh</p>';
                echo '<p>sdgdfgdgd</p>';
            echo '</td>';
        else :
            echo '<td width="2%" class="test">&nbsp;</td>';
        endif;

    endforeach;

    // if array has less than the expected 5 occ add blank columns
    if ( $i < 5 ) :
        $i++;
        for ( $i ; $i < 5; $i++ ) :
            if ( $i % 2 == 0 ) :
                echo '<td width="32%" class="test">Filler</td>';
            else :
                echo '<td width="2%" class="test">&nbsp;</td>';
            endif;
        endfor;
    endif;
    echo '</tr>';
?>

</table>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM