【问题标题】:Creating two primary columns from PHP loop从 PHP 循环创建两个主列
【发布时间】:2012-07-19 03:58:16
【问题描述】:

我有六列输出。内容是从数组中获取的(创建为 php 数组,然后作为 js 数组发送)。包含数组 $set 的示例行如下:

 $set[] = array('q'=>'墨水','qurl'=>'001','a1'=>'o','a2'=>'ui','f'=>'m','m'=>'4sh','e'=>'3');

假设我有四行内容。我的代码目前生成这个:

Col 1 Col 2 Col 3 Col 4 Col 5 Col 6
Col 1 Col 2 Col 3 Col 4 Col 5 Col 6
Col 1 Col 2 Col 3 Col 4 Col 5 Col 6
Col 1 Col 2 Col 3 Col 4 Col 5 Col 6

我需要将这四行放入两组主要的列中。像这样:

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 1 Col 2 Col 3 Col 4 Col 5 Col 6        Col 7 Col 8 Col 9 Col 10 Col 11 Col 12

什么是执行此操作的有效方法?我当前的代码如下所示:

…
$js_array = json_encode($set);
…


<table>
    <?php $counter = 0;
    while ($counter < 20) :
    $item = $set[$counter]?>
        <tr class="q<?php echo $counter; ?>">                   
            <form>
            <td><h5><?php echo $counter+1; ?></h5></td>
            <td><a class="question" href="javascript:soundManager.play('<?php echo $item['qurl'] ?>','audio/part2/type3/type3_<?php echo $item['qurl'] ?>.mp3');"><?php echo $item['q'] ?></a></td>
            <td><?php echo $item['f'] ?></td>
            <td><input class="a1" type="text" name="a1" maxlength="4" size="2" /></td>
            <td><?php echo $item['m'] ?>&nbsp;<input class="a2" type="text" name="a2" maxlength="4" size="2" /><?php echo $item['e'] ?></td>
            <td><input class="submit submit-<?php echo $counter; ?>" type="submit" value="Submit" /></td></form>
        </tr>
    <?php $counter++; ?>
    <?php endwhile; ?>
</table>

【问题讨论】:

    标签: php html css loops html-table


    【解决方案1】:

    你可以这样做:

    $total = 24;
    
    //loop through rows     
    for($i = 0; $i < $total; $i++) {
        //start new table?
        if($i == 0 || $i == ($total/2)) {
            if($i > 0) { //second table cell values
                echo '</table>';
                $j_val = 7;
                $j = $j_val;
            } else { //first table cell values
                $j_val = 1;
                $j = $j_val;
            }
    
            echo '<table>';
        }
    
        echo '<tr>';
    
        $total_cells = ($j + 6);
    
        for(; $j < $total_cells; $j++) {
            echo '<td>' . $j . '</td>';
        }
    
        //reset j to default
        $j = $j_val;
    
        echo '</tr>';
    }
    
    echo '</table>';
    

    【讨论】:

    • 这创建了两列,但不适用于 JS 我必须检查用户输入的内容。但是,我选择这个作为答案,以防其他人遇到类似情况并且不使用 JS 进行验证。
    • 您要求从一个数组中取出两个表,这回答了这个问题。你可以做剩下的事情。 ;)
    猜你喜欢
    • 1970-01-01
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多