将php分为两列循环

时间:2010-11-20 13:19:37

标签: php html

我有一个代码:

<?php for ($i = 0; $i < sizeof($categories); $i = $i + 4) { ?>
<?php for ($j = $i; $j < ($i + 4); $j++) { ?>
<?php if (isset($categories[$j])) { ?>
  <a href="<?php echo $categories[$j]['href']; ?>"><img src="<?php echo $categories[$j]['thumb']; ?>" title="<?php echo $categories[$j]['name']; ?>" alt="<?php echo $categories[$j]['name']; ?>" style="margin-bottom: 3px;" /></a><br />
  <a href="<?php echo $categories[$j]['href']; ?>"><?php echo $categories[$j]['name']; ?></a>
  <?php } ?>
<?php } ?>
<?php } ?>

我想将这些类别放在2列中,如下所示:

<div class="span-8">
 <div class="product">
 product1
 </div>
</div>
<div class="span-8 last">
 <div class="product">
 product2
 </div>
</div>

我该怎么做?

2 个答案:

答案 0 :(得分:0)

我没有真正看到php和html代码之间的相关性,但我认为你只需要为每个奇数类别输出不同的css类:

$last = false;
foreach($categories as $c) {
  //Output category html
  ?>
    <div class="span <?=($last)?'last':''?>">.....</div>
  <?

  $last != $last;

}

答案 1 :(得分:0)

<?php 
for ($i = 0; $i < sizeof($categories); $i = $i + 4) { 
for ($j = $i; $j < ($i + 4); $j++) { 
if (isset($categories[$j])) { 
if($colcount % 2){
$col1+="<div class='product'><a href='".$categories[$j]['href']."'><img src='".$categories[$j]['thumb']."' title='".$categories[$j]['name']."' alt='".$categories[$j]['name']."' style='margin-bottom: 3px;' /></a></div><a href='".$categories[$j]['href']."'>".$categories[$j]['name']."</a>";
}else{
$col2+="<div class='product'><a href='".$categories[$j]['href']."'><img src='".$categories[$j]['thumb']."' title='".$categories[$j]['name']."' alt='".$categories[$j]['name']."' style='margin-bottom: 3px;' /></a></div><a href='".$categories[$j]['href']."'>".$categories[$j]['name']."</a>";
}
$colcount++;
}
}
}
echo "<div class='span-8'>".$col1."</div><div class='span-8 last'>".$col2."</div>";

或者你可以通过简单的方式做到这一点并使它成为一种流畅的<ul>,就像他们自动完成它一样