表格单元格行跨PHP

时间:2013-07-20 10:31:41

标签: php html-table

我想在数组中计算重复的order_id,并根据表格单元格上的结果打印rowspan。我不确定如何用PHP实现这一点,任何帮助都表示赞赏。

我有这样的php数组:

Array (
[0] => Array
    (
        [order_id] => 1374282003
        [status] => 1
        [mark] => Casio
        [model] => W-81
        [price] => 10.00
    )

[1] => Array
    (
        [order_id] => 1374282003
        [status] => 1
        [mark] => Casio
        [model] => W-81
        [price] => 10.00
    )

[2] => Array
    (
        [order_id] => 1374282573
        [status] => 1
        [mark] => Casio
        [model] => W-81
        [price] => 10.00
    )

[3] => Array
    (
        [order_id] => 1374282573
        [status] => 1
        [mark] => Casio
        [model] => W-81
        [price] => 10.00
    )

[4] => Array
    (
        [order_id] => 1374282573
        [status] => 1
        [mark] => Casio
        [model] => W-81
        [price] => 10.00
    )

我想在表格单元格上使用rowspan

<table>
<tr>
<td>Order</td>
<td>Product</td>
<td>Price</td>
<td>Status</td>
</tr>
<tr>
  <td rowspan="2">1374282003</td>
  <td>Casio</td>
  <td>10</td>
  <td>1</td>
</tr>
<tr>
  <td>Casio</td>
  <td>10</td>
  <td>1</td>
</tr>
<tr>
<td rowspan="3">1374282573</td>
  <td>Casio</td>
  <td>10</td>
  <td>1</td>
</tr>
<tr>
  <td>Casio</td>
  <td>10</td>
  <td>1</td>
</tr>
<tr>
  <td>Casio</td>
  <td>10</td>
  <td>1</td>
</tr>
</table>

这是数组循环的代码

<?php
$rowspan = array_count_values(array_map(function($item) { return $item['order_id']; }, $result));
foreach( $result as $row) {
?>
<tr>
  <td rowspan="<?php echo $rowspan[$row['order_id']]"><?php echo $row['order_id']; ?></td>
  <td><?php echo $row['mark']. " " .$row['model']; ?></td>
  <td>1</td>
  <td><?php echo number_format($row['price'], 2, '.', ','); ?> kuna</td>
  <td><?php echo $row['status']; ?></td>
</tr>
<?php } ?>

4 个答案:

答案 0 :(得分:2)

它将起作用

function check_next_rowspan($sql_values, $checkkey, $rowspan) {
    $current_id = $sql_values[$checkkey]['id'];
    if(count($sql_values) != $checkkey+1) {
        if($sql_values[$checkkey+1]['id'] == $current_id) {
            $rowspan++;
            return check_next_rowspan($sql_values, $checkkey+1, $rowspan);
        }
    }                   
    return $rowspan;
}

function check_previous_rowspan($sql_values, $checkprevkey) {
    if($checkprevkey != 0) {
        if($sql_values[$checkprevkey]['id'] == $sql_values[$checkprevkey-1]['id']) {
            return true;
        }
    }
    return false;
}   

$sql_values = array(
                    array('id'=>'1', 'value'=>'test1'),
                    array('id'=>'1', 'value'=>'test2'),
                    array('id'=>'1', 'value'=>'test3'),
                    array('id'=>'2', 'value'=>'test4'),
                    array('id'=>'3', 'value'=>'test5'),
                    array('id'=>'3', 'value'=>'test6'),
                    array('id'=>'3', 'value'=>'test7'),
                    array('id'=>'4', 'value'=>'test8'),
                    array('id'=>'4', 'value'=>'test9'),
                );
$rowspan = 1;
$tablehtml = '';
foreach($sql_values as $key => $sql_value){ 
    $tablehtml .= '<tr>';
    $current_id = $sql_value['id'];
    $rowspancount = check_next_rowspan($sql_values, $key, $rowspan);
    if($rowspancount > 1) {
        $checkprevid = check_previous_rowspan($sql_values, $key);
        if($checkprevid) {
            $tablehtml .= '<td>'. $sql_value['value'] .'</td>';
        } else {
            $tablehtml .= '<td rowspan="'.$rowspancount.'">'. $sql_value['id'] .'</td><td>'. $sql_value['value'] .'</td>';
            $rowspancount = 1;
        }
    } else {
        $checkprevid1 = check_previous_rowspan($sql_values, $key);
        if($checkprevid1) {
            $tablehtml .= '<td>'. $sql_value['value'] .'</td>';
        } else {
            $tablehtml .= '<td>'. $sql_value['id'] .'</td><td>'. $sql_value['value'] .'</td>';
        }                   
    }
    $tablehtml .= '</tr>';
}

$html = '<!DOCTYPE html>
        <html>
        <head>
            <title></title>
        </head>
        <body>
            <table border="1">
                '.$tablehtml.'
            </table>
        </body>
        </html>';
echo $html;

答案 1 :(得分:0)

在用于填充表数据的循环中尝试使用:

if(in_array($item['order_id'], $tempArray))
{
    $rowspan += count($tempArray);
}
$tempArray[] = $item['order_id'];

答案 2 :(得分:0)

    <?php
$rowspan = array_count_values(array_map(function($item) { return $item['order_id']; }, $result));
foreach( $result as $row) {
?>
<tr>
  <td rowspan="<?php echo $rowspan[$row['order_id']]"><?php echo $row['order_id']; ?></td>
  <td><?php echo $row['mark']. " " .$row['model']; ?></td>
  <td>1</td>
  <td><?php echo number_format($row['price'], 2, '.', ','); ?> kuna</td>
  <td><?php echo $row['status']; ?></td>
</tr>
<?php } ?>

好的,在这里,你有一些PHP,然后它围绕着一些HTML,然后它进入一些PHP,并且和。最终,这会让你发疯。

首先将PHP标记减少到每个文件一个或两个。它并不意味着通过跳入和跳出PHP引擎来进行字符串处理。所以切换到这样的事情:

<?php
$rowspan = array_count_values(array_map(function($item) { return $item['order_id']; }, $result));
foreach( $result as $row){
    echo "<tr>\n";
    echo "<td rowspan=" . $rowspan[$row['order_id']] .$row['order_id'] ."></td>\n" ;
    echo "<td><" .$row['mark']. " " .$row['model'] . "></td>\n";
    echo "<td>1</td>";
    echo "<td>" . $row['order_id']. "</td>";
    echo "<td><" . number_format($row['price'], 2, '.', ',') . "> kuna</td>\n";
    echo "<td><" .$row['status'] ."></td>\n";
    echo "</tr>\n";
    } 
?>

答案 3 :(得分:0)

<?php

$results = [
0 => [
        'order_id'=> '1374282003',
        'status'=> 1,
        'mark'=> 'Casio',
        'model'=> 'W-81',
        'price'=> 10.00,
    ],
1=> [
        'order_id'=> '1374282003',
        'status'=> 1,
        'mark'=> 'Casio',
        'model'=> 'W-81',
        'price'=> 10.00,
    ],
2=> [
        'order_id'=> '1374282573',
        'status'=> 1,
        'mark'=> 'Casio',
        'model'=> 'W-81',
        'price'=> '10.00',
    ],
3=> [
        'order_id'=> '1374282573',
        'status'=> 1,
        'mark'=> 'Casio',
        'model'=> 'W-81',
        'price'=> 10.00,
    ],
4=> [
        'order_id'=> '1374282573',
        'status'=> 1,
        'mark'=> 'Casio',
        'model'=> 'W-81',
        'price'=> 10.00,
    ],
];

$pedidos = [];

foreach($results as $key =>$pedido){
  $pedidos[$pedido['order_id']][] = $pedido;
  $count = count($pedidos[$pedido['order_id']]);
  $pedidos[$pedido['order_id']][0]['rowspan'] = $count;
}
?>

<table border="1">
<tr>
    <th>Order id</th>
    <th>Produto</th>
    <th>valor</th>
    <th>Status</tr>
</tr>
 <?php
    foreach( $pedidos as $pedido) {
        foreach($pedido as $row){
?>
<tr>
<?php 
    if(isset($row['rowspan']) && $row['rowspan']>1){
             echo  '<td rowspan=" ' . $row['rowspan'] .'">'.$row['order_id'].'</td>';     
    }
    if(isset($row['rowspan']) && $row['rowspan'] === 1){
            echo  '<td>'.$row['order_id'].'</td>';     
        }
   ?>
  <td><?php echo $row['mark']. " " .$row['model']; ?></td>
  <td>1</td>
  <td><?php echo number_format($row['price'], 2, '.', ','); ?> </td>
  <td><?php echo $row['status']; ?></td>
</tr>
<?php }
        } ?>
</table>