PHP表数据显示结果垂直不水平

时间:2015-11-05 07:49:26

标签: php html-table

我真的很沮丧..我真的需要正确显示这张表。如何将此代码设置为垂直显示表格,如下所示:

1 - 4 - 7

2 - 5 - 8

3 - 6 - 9

而不是这样:

1 - 2 - 3 - 4

5 - 6 - 7 - 8

9 - ...等等

<?php

$PNG_WEB_DIR = plugin_dir_path( __FILE__ ).'temp/';

wp_mkdir_p($PNG_WEB_DIR);

$cols = $this->settings['cols'];
$rows = $this->settings['rows'];
$label_number = 0;

// die($_GET['order_ids']);
$order_ids = explode(',',$_GET['ids']);
$order_count = count($order_ids);
$labels_per_page = $cols*$rows;
$page_count = ceil(($order_count)/$labels_per_page);

for ($page=0; $page < $page_count; $page++) {
    echo '<table class="address-labels" width="100%" height="100%" border="0" cellpadding="0">';
    $last_height = 0;
    $current_height = $current_width = 0;
    $current_row = 0;

    for ($label=0; $label < $labels_per_page; $label++) { 
        $label_number++;
        $current_col = (($label_number-1) % $cols)+1;

        if ($current_col == 1) {
            $last_height = $current_height;
            $last_width = 0;
            $current_row++;
            echo '<tr class="label-row">';
        }

        if ( $label_number > $this->offset && isset($order_ids[$label_number - $this->offset - 1]) ) {
            $order_id = $order_ids[$label_number - $this->offset - 1];
        } else {
            $order_id = '';         
        }

        $current_width = round( $current_col * (100/$cols) );
        $width = $current_width - $last_width;
        $last_width = $current_width;

        $current_height = round( $current_row * (100/$rows) );
        $height = $current_height - $last_height;

        printf('<td width="%s%%" height="%s%%" class="label"><div class="label-wrapper">', $width, $height);
        // because we are also looping through the empty labels,
        // we need to check if there's an order for this label
        if (!empty($order_id)) {
            // get label data from order
            $order = new WC_Order( $order_id );

            // replace placeholders
            $label_data = isset($this->settings['address_data'])? nl2br( $this->settings['address_data'] ): '[shipping_address]';
            $label_data = $this->make_replacements( $label_data, $order );

            echo '<div class="address-block">';
            echo '<div class="addrress_show">';

            // process label template to display content
            echo $label_data; 
            echo '</div>';
            echo '<div class="clearb"></div>';
            echo '</div>';
        } else {
            echo '&nbsp;';
        }
        echo '</div></td>';

        if ($current_col == $cols) {
            echo '</tr>';
        }

    }
    echo '</table>';
}
        // shpt_after_page hook
                do_action( 'shpt_after_page' );
        ?>

2 个答案:

答案 0 :(得分:0)

试试这个。这是纯粹的PHP

<?php 

$cols = 3;
$data = range(1,29);


// classic 
$index = 1;
foreach($data as $el)
{
    echo $el.' ';  if ($index % $cols ==0) echo '<br/>';
    $index++;
}

echo '<br/><br/>';

// special
$rows = $cols ;
$cols = floor(count($data)/$cols);

for($row = 0 ; $row < $rows; $row++){

    for ($index = $row; $index < count($data); $index += $rows)
    echo $data[$index].' ';
    echo '<br/>';

}

答案 1 :(得分:0)

由于我无法知道你用来遍历它并生成表的原始数据结构,我把它假定为一维数组,你想把表打印为3列,如你所说,动作这里是一个PHP Fiddle - 命中运行来执行它 - 只要列为3,行就是动态的,这适用于任何数组长度

PHP: 已更新为命名目的

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.14.0/jquery.validate.min.js"></script>
<form>
    value 1
    <input name="value1"
           data-rule-smaller_than="[name='value2']"
           data-msg-smaller_than="value 1 must be smaller than value 2" /><br/>
    value 2
    <input name="value2"
           data-rule-not_smaller_than="[name='value1']"
           data-msg-not_smaller_than="value 1 must be smaller than value 2"/><br/>
    <input type="submit"/>
</form>

修改 现在,如果要更改列号,例如将其设置为5,则需要执行以下操作:

1st - 设置<?php $table = '<table class="address-labels" width="30%" border="0" cellpadding="0">'; $dataArr = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16, 17); $colNum = 3; $rowNum = ceil(count($dataArr)/$colNum); for($i=0; $i < $rowNum; $i++){ $x1 = isset( $dataArr[$i] ) ? $dataArr[$i] : '-'; $x2 = isset( $dataArr[$i + $rowNum] ) ? $dataArr[$i + $rowNum] : '-'; $x3 = isset( $dataArr[$i + 2 * $rowNum] ) ? $dataArr[$i + 2 * $rowNum] : '-'; $table .= '<tr><td>' . $x1 . '</td><td>' . $x2 . '</td><td>' . $x3 . '</td></tr>'; } $table .= '</table>'; echo $table; ?> 第二 - 添加更多变量来保存值,例如,*考虑它是5列:

$colNum = 5;

并且不要忘记增加乘数 $x4 = isset( $dataArr[$i + 3 * $rowNum] ) ? $dataArr[$i + 3 * $rowNum] : '-'; $x5 = isset( $dataArr[$i + 4 * $rowNum] ) ? $dataArr[$i + 4 * $rowNum] : '-';

然后,对于您添加的每个额外列,您需要添加* $rowNum,如:

<td></td>

这是另一个显示上述5列的PHP Fiddle