2012-03-20 93 views
4

我试着让这个代码中的最后一个td与之前的td相同,但是我不能和td打印在一个新行中。如何让它们彼此相邻,问题在于第一个5 td处于foreach循环,而最后的td并不遵循此foreach,因为它的值是函数而不是foreach中的键或值。彼此相邻的表格行

<?php foreach($downloads as $dl) { ?> 
<tr id="this"> 
<td ><img src="images/<?=$dl['type']?>.png"/></td> 
<td id="no3"><?=$dl['type']?></td> 
<td> 
    <a target="_blank" style="margin-right:3px" href="download.php?id=<?=$dl['id']?>"> 
    <?=$dl['title']?> 
    </a> 
</td> 
<td> 
    <center> 
    <a href="http://<?=urlencode($dl['surl'])?>"><?=$dl['sname']?></a> 
    </center> 
</td> 
<td align="center"><?=$dl['views']?></td> 
</tr> 
<?php } ?> 


    <td align="center"><?=$core->use_love(); ?></td> 

http://advphp.com/imgup/images/41553286204776144664.jpg http://advphp.com/imgup/images/86639621977227017216.jpg

最后TD

public function use_love(){ 

    $sql=mysql_query("select * from wcddl_downloads ORDER BY id DESC LIMIT ".$this->pg.",".$this->limit.""); 

    while($row=mysql_fetch_array($sql)) 
    { 
    $down_id=$row['id']; 
    $love=$row['love']; 
    ?> 
    <div class="box" align="center"> 
    <a href="#" class="love" id="<?php echo $down_id; ?>"> 
    <span class="on_img" align="left"> <?php echo $love; ?> </span> 
    </a> 
    </div> 
    <? 
    }    
} 
+1

您仍然可以将它放置在每个循环中,它仍然会运行并将值添加到最后一行。或者你可以在循环外部运行该函数,存储它的值并将该值插入到td中(这又将在每个循环中)。 – Davos555 2012-03-20 14:44:05

+0

我不希望该函数在foreach循环中,因为它将在单个td中迭代,我只是想让两个tr彼此相邻,只要函数的td在循环之外 – 2012-03-20 14:46:20

+1

想要最后一列显示在每一行上,还是只显示最后一行?为什么你不能将它存储在一个字符串中,并将其作为'@ Davos555'建议的内容添加到foreach中? – mellamokb 2012-03-20 14:49:40

回答

1

最后<td>(在foreach环外的一个)的功能是一个新行,因为这是最后<tr>标签外。解决方法之一是始终关闭</tr>标签的最后<td>后,像这样:

<?php 
$first_time = True; 
foreach($downloads as $dl) { 
    // If this is the first time through the loop, don't echo a </tr> tag: 
    if ($first_time) { 
     $first_time = False; 
    } else { 
     echo "</tr>"; 
    } 

    // Now print the new row, but don't close it yet: 
?> 

<tr id="this"> 
    <td><img src="images/<?=$dl['type']?>.png"/></td> 
    <td id="no3"><?=$dl['type']?></td> 
    <td><a target="_blank" style="margin-right:3px" href="download.php?id=<?=$dl['id']?>"><?=$dl['title']?></a></td> 
    <td><center><a href="http://<?=urlencode($dl['surl'])?>"><?=$dl['sname']?></a></center></td> 
    <td align="center"><?=$dl['views']?></td> 

<?php 
} 
?> 

<td align="center"><?=$core->use_love(); ?></td> 
</tr> 

这将始终把最后<td>的最后一行。

+0

我想你还需要在你的''中有一个'colspan =“2”'属性,你可以在其中显示除最后一个之外的视图。为了获得最后的下载,你需要一个'count($ downloads)',每次循环时增加它并相应地检查它以插入最后的''。 – Maxime 2012-03-20 15:00:15

+0

我用线图更新了线索来描述我想要的东西 – 2012-03-20 15:01:56

+0

好主意史蒂夫,但最后的td印在一个新的线上,但在我想要的相同位置,只是我们想要的方式是在同一行........ – 2012-03-20 15:09:02

1

更新:我刚才看到你添加了一个图。现在这个答案是没有道理的,因为之前给出的文本描述与您想要在图中实现的内容无关。

我将修改提交给Steve Nay的答案,因为您需要在所有TR中使用相同数量的TD。如果您没有相同的计数,则需要使用colspan来实现它。我已经添加了一个计数器来检查这是否是最后一次循环。这里是:

<?php 
$downloads_count = count($downloads); 
$counter = 0; 
foreach($downloads as $dl) : 
    $counter++; 
    // If this is the first time through the loop, don't echo a </tr> tag: 
    if ($counter > 1) { 
     echo "</tr>"; 
    } 

    // Now print the new row, but don't close it yet: 
?> 

<tr id="this"> 
    <td><img src="images/<?=$dl['type']?>.png"/></td> 
    <td id="no3"><?=$dl['type']?></td> 
    <td><a target="_blank" style="margin-right:3px" href="download.php?id=<?=$dl['id']?>"><?=$dl['title']?></a></td> 
    <td><center><a href="http://<?=urlencode($dl['surl'])?>"><?=$dl['sname']?></a></center></td> 
    <td align="center"<?php if ($downloads_count != $counter) echo ' colspan="2"'; ?>><?=$dl['views']?></td> 

<?php endforeach; ?> 

    <td align="center"><?=$core->use_love(); ?></td> 
</tr> 
+0

感谢maxiem,我希望如果达到了我想要的,简而言之要多描述一下, 这是上一个td函数的代码 – 2012-03-20 15:17:34