-1

テーブルの行ごとに 4 つの列を生成する PHP 配列があります。行ごとに 4 列ではなく 8 列になるように、PHP を変更する必要があります。

例えば。現在の出力は次のようになります。

COL 1 COL 2 COL 3 COL 4
COL 5 COL 6 COL 7 COL 8
COL 9 COL 10 COL 11 COL 12
COL 13 COL 14 COL 15 COL 16

しかし、次のようにする必要があります。

COL 1 COL 2 COL 3 COL 4      COL 5 COL 6 COL 7 COL 8
COL 9 COL 10 COL 11 COL 12   COL 13 COL 14 COL 15 COL 16          

以前の質問で、CSS/HTML を介してこれを行うことができるかどうかを尋ねました。代わりに、PHP コードを変更するよう強く提案されました。これを行うための疑似コードを受け取りましたが、うまく動作しません。ロジックは次のとおりです。

if($counter % 2 == 0)
    output startting <tr> tag

output the four <td> tags with data

if(($counter +1) % 2 == 0)
    output closing <tr> tag

これが私の現在のコードです:

            <form>
            <table>
                <?php $counter = 0;
                while ($counter < 20) : 
                $item = $set[$counter]
                                    if($counter % 2 == 0) ?>
                    <tr class="q<?php echo $counter; ?>">                   
                        <td><a class="question" href="<?php echo $item['qurl'] ?>');"><?php echo $item['q'] ?></a></td>
                        <td><input class="a1" type="text" name="a1" maxlength="3" size="3" /></td>
                        <td><input class="a2" type="text" name="a2" maxlength="3" size="3" /></td>
                        <td><input class="submit submit-<?php echo $counter; ?>" type="submit" value="Submit" /></td>
                                    <?php if(($counter +1) % 2 == 0) ?>
                    </tr>
                <?php $counter++; ?>
                <?php endwhile; ?>
            </table>
                            </form>

初心者ですので初歩的なことをお許しください。前もって感謝します。

4

2 に答える 2

3

PHP の if ステートメントの構文を実際に確認する必要があります。

$counter = 0;
while ($counter < count($set)) : 
    $item = $set[$counter];
    if($counter % 2 == 0): ?>
        <tr class="q<?php echo $counter; ?>">                   
    <?php endif;?>
            <td><a class="question" href="javascript:soundManager.play('<?php echo $item['qurl'] ?>','audio/part2/type1/type1_<?php echo $item['qurl'] ?>.mp3');"><?php echo $item['q'] ?></a></td>
            <td><input class="a1" type="text" name="a1" maxlength="3" size="3" /></td>
            <td><input class="a2" type="text" name="a2" maxlength="3" size="3" /></td>
            <td><input class="submit submit-<?php echo $counter; ?>" type="submit" value="Submit" /></td>
    <?php if(($counter +1) % 2 == 0): ?>
        </tr>
    <?php endif;?>
    <?php $counter++; ?>
<?php endwhile; ?>
<?php if(($counter +1) % 2 == 0): ?>
    </tr>
<?php endif; ?>
于 2012-07-20T23:31:43.823 に答える
2

with を使用しarray_chunk()て、配列を小さな配列に分割し、これを印刷できます。

<table>
<?php foreach (array_chunk($set, 2) as $row_set): ?>
  <tr>
  <?php foreach ($row_set as $item_set): ?>
    <td><?php echo $item_set['qurl']; ?></td>
    <td><?php echo $item_set['q']; ?></td>
  <?php endforeach; ?>
  </tr>
<?php endforeach;?>
</table>

幸運を :)

于 2012-10-24T20:25:56.873 に答える