Need little PHP help
I need little PHP help, here is a code which generate a product list:
<table class="list">
<?php for ($i = 0; $i < sizeof($products); $i = $i + 1) { ?>
<tr>
<?php for ($j = $i; $j < ($i + 1); $j++) { ?>
<td width="100%"><?php if (isset($products[$j])) { ?>
<a class="prod_snimka" href="<?php echo $products[$开发者_JAVA技巧j]['href']; ?>"><img width="200" src="<?php echo $products[$j]['thumb']; ?>" title="<?php echo $products[$j]['name']; ?>" alt="<?php echo $products[$j]['name']; ?>" /></a>
<?php } ?></td>
<?php } ?>
</tr>
<?php } ?>
</table>
Output: http://d.pr/PPVq+
What I need to modify to produce output one TR and TD's inside, after 4 TD (products) make another TR. In one row I want 4 TD, in that case too, if there is only 3 product then the last one will be an empty TD. Huhh.. It is possible?
Thanks
It does not look like you need that inner loop. It will only execute once anyway.
Try this out:
<?php
$columns = 4;
$i = 0;
?>
<table class="list">
<?php foreach ($products as $product) {
if (!($i % $columns)) {
echo '<tr>';
} ?>
<td width="25%">
<a class="prod_snimka" href="<?php echo $product['href']; ?>"><img width="200" src="<?php echo $product['thumb']; ?>" title="<?php echo $product['name']; ?>" alt="<?php echo $product['name']; ?>" /></a>
</td>
<?php
$i++;
if (!($i % $columns)) {
echo '</tr>';
}
?>
<?php } ?>
<?php
if (!($i % $columns)) {
while (!($i % $columns)) {
echo '<td width="25%"></td>';
$i++;
}
echo '</tr>';
}
?>
</table>
<table class="list">
<?php $i = 0;
$cols = 4;
for each $products as $product {
if (!($i % $cols)) {
echo "<tr width='25%'>";
}
echo '<td><a class="prod_snimka" href="'.$product['href'].'"><img width="200" src="'.$product['thumb'].'" title="'.$product['name'].'" alt="'.$product['name'].'" /></a></td>';
if !($i % $cols) {
echo "</tr>";
}
$i++;
}
if ($i-1 % $cols) {
echo "</tr>";
}
</table>
精彩评论