如何将一行中的图像数量限制为三个?

uda*_*aya 10 html css php html-table

我在表格中填充数据库中的图像,如何将图像限制为每行三个?

<table border="0" align="center" height="25%" width="25%"  >
<tr><td align="center" width="50px"  bgcolor="#4b2d0e"><strong><font color="#FFFFFF">Friend List</font></strong></td></tr>
 <? foreach($Selected as $row)
 {   
     $value = $row['dPath'];
     $imgp =  base_url()."images"."/".$value;
 ?>
 <tr><td bgcolor="#999999">
 <strong ><?=$row['dFrindName'].'</br>';?></strong>
 <? if($value!="") { ?>
 <a class="Tab_Link" href="<? echo site_url();?>/friends/View_FProfile/<?=$row['dMember_Id']?>"><img src="<?=$imgp ?>" name="b1" width="90" height="80" border="0"/></a><br>
 <? } else { ?>
 <a class="Tab_Link" href="<? echo site_url();?>/friends/View_FProfile/<?=$row['dMember_Id']?>"><img src="../images/us.png" width="90px" height="80px"></a>
 <? }?>
 </td></tr>
 <? }}?>
</table>
Run Code Online (Sandbox Code Playgroud)

这是我的桌子

Aar*_*run 8

<?php 

$x=0;

    foreach($Selected as $row){

        if($x%3 == 0)
            echo '<tr>';

        $value = $row['dPath'];
        $imgp =  base_url()."images"."/".$value;
?>

        <td style="background-color:#999999;">
            <strong ><?=$row['dFrindName'].'</br>';?></strong>

            <?php if($value!="") {?>
                <a class="Tab_Link" href="<? echo site_url();?>/friends/View_FProfile/<?=$row['dMember_Id']?>"><img src="<?=$imgp ?>" name="b1" width="90" height="80" border="0"/></a><br>
            <?php } else { ?>
                <a class="Tab_Link" href="<? echo site_url();?>/friends/View_FProfile/<?=$row['dMember_Id']?>"><img src="../images/us.png" width="90px" height="80px"></a>
            <?php }?>
        </td>
<?php
        if($x%3 == 0)
            echo '</tr>';
        x++; 
    }

    if(($x-1)%3 != 0)
        echo '</tr>'; //Prints out the last '<tr>' if one isn't printed.
?>
Run Code Online (Sandbox Code Playgroud)

您需要使用if带模数运算符.

  • 这个答案有无效的HTML,简单的`$ x%3`实际上无法正常工作以何时关闭`</ tr> (2认同)

phi*_*reo 7

在这里,我清理了无效的HTML,使用了CSS,并使用了更推荐的PHP编码风格.

请注意:您需要注意,如果$Selected包含用户输入的(或其他非HTML安全的)数据,则需要将输出包装在htmlspecialcharsXSS漏洞中或对XSS漏洞开放.

考虑到目前每行仅显示1个图像,想要"将图像限制为每行三个",这有点不清楚.如果我假设你想要每行显示3而不是1,那么你需要使用模数运算符并且只<tr>在每第三个元素之后打开一个新的,然后在正确的时间关闭它.像这样:

<style type="text/css">
    a img { border: none; }
    .friend-list { border: none; width: 25%; height: 25%; margin: 0 auto; }
    .friend-list th { text-align: center; background-color: #4b2d0e; color: #fff; font-weight: bold; }
    .friend-list td { background-color: #999999; }
</style>

<?php 
$numCols = 3;
$colCount = -1;
?>
<table class="friend-list">
    <tr>
        <th colspan="<?php echo $numCols; ?>">Friend List</th>
    </tr>
    <?php
    foreach($Selected as $row) {

        $value = $row['dPath'];
        $imgp = ($value) ? base_url().'images/'.$value : '../images/us.png';

        if(++$colCount % $numCols == 0) {
            echo '<tr>';
        }
        ?>
            <td>
                <strong><?php echo $row['dFriendName']; ?></strong><br />
                <a class="Tab_Link" href="<?php echo site_url(); ?>/friends/View_FProfile/<?php echo $row['dMember_Id']; ?>">
                    <img src="<?php echo $imgp; ?>" width="90" height="80" />
                </a>
            </td>
        <?php 
        if(($colCount + 1) % $numCols == 0) {
            echo '</tr>';
        } elseif (($colCount + 1) == count($Selected)) {
            // if 16 elements are to fit in 3 columns, print 2 empty <td>s before closing <tr>
            $extraTDs = $numCols - (($colCount + 1) % $numCols);
            for ($i = 0; $i < $extraTDs; $i++) {
                echo '<td>&nbsp;</td>';
            }
            echo '</tr>';
        }
    }
    ?>
</table>
Run Code Online (Sandbox Code Playgroud)


gro*_*gel 5

应该为列和行有意义的情况保留表...更好的解决方案是使用浮动块元素而不是表格单元格.当你浮动一堆相似的块时,它们会自动换行,因此关键是使它们的父容器足够宽以容纳其中的3个.

你不需要用php做任何特殊的事情来创建新行,所以我只是显示html和css,让你编写php来实现它.

HTML:

<div id="replacesTable">
    <div class="replacesTableCell">
        <div class="name">Name</div>
        <img src="http://stackoverflow.com/favicon.ico" />
    </div>
    <div class="replacesTableCell">
        <div class="name">Name</div>
        <img src="http://stackoverflow.com/favicon.ico" />
    </div>
    <div class="replacesTableCell">
        <div class="name">Name</div>
        <img src="http://stackoverflow.com/favicon.ico" />
    </div>
    <div class="replacesTableCell">
        <div class="name">Name</div>
        <img src="http://stackoverflow.com/favicon.ico" />
    </div>
    <div class="clear">&nbsp;</div>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

#replacesTable{
    width: 300px;
}
div.replacesTableCell{
    float:left;
    width: 100px;

    /* styles below are just to make it easier to see what's happening */
    text-align:center;
    font-size: 10px;
    margin: 20px 0;
}
/* this just stretches the parent container #replacesTable around the entries*/
.clear{
    clear:both;
    height:1px;
    overflow:hidden;
}
Run Code Online (Sandbox Code Playgroud)