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

初めての場合を除き</tr>、a をエコーする直前にa をエコーし​​たいもの。<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 に答える
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 に答える