0

如何使每 4 列在表中成为自己的行?请不要告诉我我应该将它们作为 div 来做,这需要是一张桌子......

这是我的代码:

        $tmpRet = array_filter($ret, function($e){
            return ($e['categoryID'] == 6) ? $e : null;
            });
        $tmpRet = array_values($tmpRet);
        $tmpCt = count($tmpRet);
        $counter = 0;
        echo '<table width="100%" cellpadding="10" cellspacing="5">' . PHP_EOL;
        for($i = 0; $i < $tmpCt; ++$i){
            $counter ++;
            echo ($i == 0 || $i % 4 === 0) ? '  <tr>' . PHP_EOL : null;

            echo '<td width="25%" align="center" valign="middle"><a href="' . $tmpRet[$i]['sponUrl'] . '" target="_blank"><img border="0" src="http://sparkusa.org/' . GetLogoPath($tmpRet[$i]['showType']) . '/images/sponsors/' . $tmpRet[$i]['sponFileName'] . '" alt="' . $tmpRet[$i]['sponName'] . '" title="' . $tmpRet[$i]['sponName'] . '" style="max-height:85px;" /></a></td>' . PHP_EOL;

            echo ($i == 0 || $i % 4 === 0) ? '  </tr>' . PHP_EOL : null;
        }
        echo '</table>';

它吐了出来:

<table width="100%" cellpadding="10" cellspacing="5">
    <tr>
        <td width="25%" align="center" valign="middle"><a href="http://www.adp.com/" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/ADP_logo_tag_R_rgb 4-11-13.jpg" alt="ADP" title="ADP" style="max-height:85px;" /></a></td>
    </tr>
    <td width="25%" align="center" valign="middle"><a href="http://ascensus.com/RetirementPlanSolutions/index.htm" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Ascensus - color.jpg" alt="Ascensus" title="Ascensus" style="max-height:85px;" /></a></td>
        <td width="25%" align="center" valign="middle"><a href="http://www.getbridgepoint.com/" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Bridgepoint 2012.JPG" alt="Bridgepoint" title="Bridgepoint" style="max-height:85px;" /></a></td>
        <td width="25%" align="center" valign="middle"><a href="http://www.brightscope.com/" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/BrightScope_Logo JPEG 10-10-12 approved.jpg" alt="BrightScope" title="BrightScope" style="max-height:85px;" /></a></td>
    <tr>
        <td width="25%" align="center" valign="middle"><a href="http://www.corporateinsight.com/" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Corporate Insight Logo JPEG 9-26-12.JPG" alt="Corporate Insight" title="Corporate Insight" style="max-height:85px;" /></a></td>
    </tr>
    <td width="25%" align="center" valign="middle"><a href="http://www.guardianlife.com/" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Guardian Logo from 2011 OK.JPG" alt="Guardian" title="Guardian" style="max-height:85px;" /></a></td>
        <td width="25%" align="center" valign="middle"><a href="http://www.infosysbpo.com" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Infosys McCamish_natural_vert_nt jpeg.JPG" alt="Infosys McCamish" title="Infosys McCamish" style="max-height:85px;" /></a></td>
        <td width="25%" align="center" valign="middle"><a href="http://ing.us/" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/INGC300.JPG" alt="ING" title="ING" style="max-height:85px;" /></a></td>
    <tr>
        <td width="25%" align="center" valign="middle"><a href="https://www.janus.com/" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/JANUS logoJPEG created 9-20-12.jpg" alt="Janus Capital Group" title="Janus Capital Group" style="max-height:85px;" /></a></td>
    </tr>
    <td width="25%" align="center" valign="middle"><a href="http://www.jpmorgan.com/pages/jpmorgan" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/JP Morgan Logo for infotree.JPG" alt="JP Morgan" title="JP Morgan" style="max-height:85px;" /></a></td>
        <td width="25%" align="center" valign="middle"><a href="http://www.newkirk.com/" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Newkirk a DST Co 4-23-13.JPG" alt="Newkirk" title="Newkirk" style="max-height:85px;" /></a></td>
        <td width="25%" align="center" valign="middle"><a href="http://www.oculuspartners.com/" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Oculus_logo_v4-vert.jpg" alt="Oculus" title="Oculus" style="max-height:85px;" /></a></td>
    <tr>
        <td width="25%" align="center" valign="middle"><a href="http://us.pioneerinvestments.com" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Pioneer Investments Jpeg ogo.JPG" alt="Pioneer Investments" title="Pioneer Investments" style="max-height:85px;" /></a></td>
    </tr>
    <td width="25%" align="center" valign="middle"><a href="http://www.processunity.com/" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/ProcessUnity logo JPEG created 9-20-12.jpg" alt="ProcessUnity" title="ProcessUnity" style="max-height:85px;" /></a></td>
        <td width="25%" align="center" valign="middle"><a href="http://www.prudential.com/view/page/public" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/Prudential Blue Gray for Lanyards.JPG" alt="Prudential" title="Prudential" style="max-height:85px;" /></a></td>
        <td width="25%" align="center" valign="middle"><a href="http://www.rrdonnelley.com" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/RR Donnelley Logo for Mints.JPG" alt="RR Donnelley" title="RR Donnelley" style="max-height:85px;" /></a></td>
    <tr>
        <td width="25%" align="center" valign="middle"><a href="http://fc.standardandpoors.com/sites/client/us/crpsite/" target="_blank"><img border="0" src="http://sparkusa.org/spark-forum/images/sponsors/sp_capitaliq_fc_logo.jpg" alt="S&P Capital IQ" title="S&P Capital IQ" style="max-height:85px;" /></a></td>
    </tr>
</table>
4

5 回答 5

5

尝试用这些替换代码中的相关行:

// At column 0 you create a new row <tr>
echo $i % 4 == 0 ? "<tr>\n" : "";

// At column 3 you end the row </tr>
echo $i % 4 == 3 ? "</tr>\n" : "";

这将创建每行 4 列。

编辑以适应可能存在不是 4 倍数的列的情况

echo "<table>\n";

$colSpan = 4;
$rows = 0;

for($i = 0; $i < 5; $i++) {
    // At column 0 you create a new row <tr>
    if($i % $colSpan == 0) {
        $rows++;
        echo "<tr>\n";
    }

    echo "<td>" . ($i + 1) . "</td>\n";

    // At column 3 you end the row </tr>
    echo $i % $colSpan == 3 ? "</tr>\n" : "";
}

// Say you have 5 columns, you need to create 3 empty <td> to validate your table!
for($j = $i; $j < ($colSpan * $rows); $j++) {
    echo "<td></td>\n";
}

// Add the final <tr>
if(($colSpan * $rows) > $i) {
    echo "</tr>\n";
}

echo "</table>\n";

这应该创造出足够完美的<table></table>结构!享受。

于 2013-08-19T15:49:32.163 回答
3

除非是第一次通过,否则在回显a</tr>之前要回显的内容。<tr>

echo "<tr>";
for($i = 0; $i < $tmpCt; ++$i){
    $counter ++;
    // echo a row close right before you row open unless it's the first one
    echo ($i != 0 || $i % 4 === 0) ? '  </tr><tr>' . PHP_EOL : null;
    echo '<td width="25%" align="center" valign="middle"><a href="' . $tmpRet[$i]['sponUrl'] . '" target="_blank"><img border="0" src="http://sparkusa.org/' . GetLogoPath($tmpRet[$i]['showType']) . '/images/sponsors/' . $tmpRet[$i]['sponFileName'] . '" alt="' . $tmpRet[$i]['sponName'] . '" title="' . $tmpRet[$i]['sponName'] . '" style="max-height:85px;" /></a></td>' . PHP_EOL;
}
// close out the last row
echo "</tr>";
于 2013-08-19T15:54:16.923 回答
1

尝试使用嵌套for循环:

for($i = 0; $i < $tmpCt; ++$i){
    $counter ++;
    echo '  <tr>' . PHP_EOL;
    for($j = 0; $j < 4 && $i < $tmpCt; ++$j, ++$i){
        echo '<td width="25%" align="center" valign="middle"><a href="' . $tmpRet[$i]['sponUrl'] . '" target="_blank"><img border="0" src="http://sparkusa.org/' . GetLogoPath($tmpRet[$i]['showType']) . '/images/sponsors/' . $tmpRet[$i]['sponFileName'] . '" alt="' . $tmpRet[$i]['sponName'] . '" title="' . $tmpRet[$i]['sponName'] . '" style="max-height:85px;" /></a></td>' . PHP_EOL;
    }
    echo '  </tr>' . PHP_EOL;
}

可能出错的一件事是,也许 PHP 不支持for发生两次增量的循环(尚未测试过),所以如果不支持,只需将 放在内部循环++$i的末尾。for

这将经过 4 列或直到找到最后一列。这样 $j 是 4 的简单计数器,$i 仍然是数组的引用。现在在伪代码中,它是

for(every column){
    start row;
    for(4 iterations){
        print or use the contents of a column;
    }
    end row;
}

编辑 正如 OrangePill 建议的那样,这也可以

for($i = 0; $i < $tmpCt; $i+=4){
    $counter ++;
    echo '  <tr>' . PHP_EOL;
    for($j = 0; $j < 4 && $i+$j < $tmpCt; ++$j){
        echo '<td width="25%" align="center" valign="middle"><a href="' . $tmpRet[$i+$j]['sponUrl'] . '" target="_blank"><img border="0" src="http://sparkusa.org/' . GetLogoPath($tmpRet[$i+$j]['showType']) . '/images/sponsors/' . $tmpRet[$i+$j]['sponFileName'] . '" alt="' . $tmpRet[$i+$j]['sponName'] . '" title="' . $tmpRet[$i+$j]['sponName'] . '" style="max-height:85px;" /></a></td>' . PHP_EOL;
    }
    echo '  </tr>' . PHP_EOL;
}
于 2013-08-19T15:58:29.570 回答
0
    echo '<table width="100%" cellpadding="10" cellspacing="5">' . PHP_EOL . '<tr>' . PHP_EOL;
        for($i = 1; $i <= $tmpCt; ++$i){

            echo '<td width="25%" align="center" valign="middle"><a href="' . $tmpRet[$i]['sponUrl'] . '" target="_blank"><img border="0" src="http://sparkusa.org/' . GetLogoPath($tmpRet[$i]['showType']) . '/images/sponsors/' . $tmpRet[$i]['sponFileName'] . '" alt="' . $tmpRet[$i]['sponName'] . '" title="' . $tmpRet[$i]['sponName'] . '" style="max-height:85px;" /></a></td>' . PHP_EOL;

            echo ($i % 4 === 0) ? '  <tr>' . PHP_EOL .'</tr>' . PHP_EOL : null;
        }

        for($j = 1; $j <= (4 - $tmpCT % 4); $j++){
            echo '<td width="25%">&nbsp;</td>' . PHP_EOL;
        }

    echo '</tr>' . PHP_EOL . '</table>';
于 2013-08-19T15:51:14.610 回答
0

你有几个问题。

首先,您将第一个和每四个单元格包装在一个 tr 中。

尝试改变这个:

    echo '<table width="100%" cellpadding="10" cellspacing="5">' . PHP_EOL;
    for($i = 0; $i < $tmpCt; ++$i){
        $counter ++;
        echo ($i == 0 || $i % 4 === 0) ? '  <tr>' . PHP_EOL : null;

        echo '<td width="25%" align="center" valign="middle"><a href="' . $tmpRet[$i]['sponUrl'] . '" target="_blank"><img border="0" src="http://sparkusa.org/' . GetLogoPath($tmpRet[$i]['showType']) . '/images/sponsors/' . $tmpRet[$i]['sponFileName'] . '" alt="' . $tmpRet[$i]['sponName'] . '" title="' . $tmpRet[$i]['sponName'] . '" style="max-height:85px;" /></a></td>' . PHP_EOL;

        echo ($i == 0 || $i % 4 === 0) ? '  </tr>' . PHP_EOL : null;
    }
    echo '</table>';

对此:

    echo '<table width="100%" cellpadding="10" cellspacing="5">' . PHP_EOL;
    echo '<tr>';//start first row
    for($i = 0; $i < $tmpCt; ++$i){
        $counter ++;
        echo '<td width="25%" align="center" valign="middle"><a href="' . $tmpRet[$i]['sponUrl'] . '" target="_blank"><img border="0" src="http://sparkusa.org/' . GetLogoPath($tmpRet[$i]['showType']) . '/images/sponsors/' . $tmpRet[$i]['sponFileName'] . '" alt="' . $tmpRet[$i]['sponName'] . '" title="' . $tmpRet[$i]['sponName'] . '" style="max-height:85px;" /></a></td>' . PHP_EOL;

        echo ($i == 0 || $i % 4 === 0) ? '</tr><tr>' . PHP_EOL : null;//end row after every fourth cell and start a new one
    }
    echo '</tr>';//close out last row
    echo '</table>';

根据您的页面布局方式,您可能必须在最后一行插入一些空表格单元格,以防您的记录数不能被 4 整除。

于 2013-08-19T16:00:04.267 回答