在自动生成的表中的每个列大小更改之前添加标题行

时间:2018-02-22 19:22:03

标签: php

我有以下代码。它循环遍历数组中的数据,并将所有具有相同大小的数据相加,并自动将rowspan数添加到表中。

自动添加新标题行

现在我需要做的是能够在大小更改之前添加新的“标题行”(echo "<th>Name</th><th>Size</th><th>Thickness</th><th>Price</th>";),然后在其他标题行之后显示新的大小。一旦将大量数据添加到表中,这将有助于查看表。

我会添加什么才能让它自动发生?我会在哪里添加它? 我认为它可以用$output[$sizeStartRow][1],但我不确定在哪里添加代码。

代码

//dummy data array instead of your database output, expanded a little to help with testing:
$data = array(
  array("name" => "item1", "size" => "2 x 2", "thickness" => ".020", "price" => "$25"),
  array("name" => "item2", "size" => "2 x 2", "thickness" => ".025", "price" => "$28"),
  array("name" => "item6", "size" => "2 x 2", "thickness" => ".080", "price" => "$50"),
  array("name" => "item3", "size" => "2 x 2", "thickness" => ".030", "price" => "$30"),
  array("name" => "item4", "size" => "2 x 2", "thickness" => ".040", "price" => "$40"),
  array("name" => "item5", "size" => "3 x 2", "thickness" => ".050", "price" => "$43"),
  array("name" => "item6", "size" => "3 x 2", "thickness" => ".050", "price" => "$43"),
  array("name" => "item7", "size" => "3 x 2", "thickness" => ".050", "price" => "$43"),
  array("name" => "item8", "size" => "3 x 2", "thickness" => ".050", "price" => "$43"),
  array("name" => "item9", "size" => "4 x 2", "thickness" => ".050", "price" => "$43"),
  array("name" => "item10", "size" => "4 x 2", "thickness" => ".050", "price" => "$43"),
  array("name" => "item11", "size" => "4 x 2", "thickness" => ".050", "price" => "$43"),
);

$output = array(); //this is ready to hold the table cells
$prevSizeVal = null; //holds the size value from the previous row
$sizeStartRow = 0; //keep track of the row where a rowspanned size cell started

//this is in place of your while loop, you won't need totalRows because the fetch_assoc function takes care of this for you. This is just for the mockup.
$counter = 0; 
$totalRows = count($data);

while ($counter < $totalRows) {
  $cp = $data[$counter]; //to get an equivalent row variable that the fetch_assoc loop gives you
  $row = array(); //create a new output row

  //get the boring ones out of the way
  $row[0] = "<td>".$cp["name"]."</td>";
  $row[2] = "<td>".$cp["thickness"]."</td>";
  $row[3] = "<td>".$cp["price"]."</td>";

  $sizeCol = "";

  //now for the fun part with the size column
  if ($prevSizeVal != $cp["size"]) {
      $sizeStartRow = $counter;
      $sizeCol = '<td rowspan="1">'.$cp["size"].'</td>';
  }
  else
  {
      //change the rowspan value at the start position, as we know it's increased
      $output[$sizeStartRow][1] = preg_replace('/rowspan="[\d]+"/', 'rowspan="'.($counter-$sizeStartRow +1).'"', $output[$sizeStartRow][1]);
  }

  $row[1] = $sizeCol;

  $output[$counter] = $row; //add the row to the output. We may change the rowspan of the size cell later.
  $prevSizeVal = $cp["size"]; //update the previous size value
  $counter++;
}

//now we've built the output array of cells, we can echo them into a table.
echo "<table>";
echo "<th>Name</th><th>Size</th><th>Thickness</th><th>Price</th>";
//loop the rows
for ($i = 0; $i < count($output); $i++) {
  echo "<tr>";
  //loop the cells within the current row
  for ($j = 0; $j < count($output[$i]); $j++)
  {
    $cell = $output[$i][$j];
    echo $cell; //some variables will be empty (because we didn't create them because we knew there was a row-spanned cell above them, so nothing will be echoed in those cases
  }
  echo "</tr>";
}
echo "</table>";

代码链接

https://eval.in/961231

1 个答案:

答案 0 :(得分:0)

我认为如果重新组织原始数组和循环,可以很容易地插入标题行。试试这个:

$data = array(
  array("name" => "item1", "size" => "2 x 2", "thickness" => ".020", "price" => "$25"),
  array("name" => "item2", "size" => "2 x 2", "thickness" => ".025", "price" => "$28"),
  array("name" => "item6", "size" => "2 x 2", "thickness" => ".080", "price" => "$50"),
  array("name" => "item3", "size" => "2 x 2", "thickness" => ".030", "price" => "$30"),
  array("name" => "item4", "size" => "2 x 2", "thickness" => ".040", "price" => "$40"),
  array("name" => "item5", "size" => "3 x 2", "thickness" => ".050", "price" => "$43"),
  array("name" => "item6", "size" => "3 x 2", "thickness" => ".050", "price" => "$43"),
  array("name" => "item7", "size" => "3 x 2", "thickness" => ".050", "price" => "$43"),
  array("name" => "item8", "size" => "3 x 2", "thickness" => ".050", "price" => "$43"),
  array("name" => "item9", "size" => "4 x 2", "thickness" => ".050", "price" => "$43"),
  array("name" => "item10", "size" => "4 x 2", "thickness" => ".050", "price" => "$43"),
  array("name" => "item11", "size" => "4 x 2", "thickness" => ".050", "price" => "$43"),
);
# Create storage array
$transfer   =   [];
# Loop $data
foreach($data as $row) {
    # Make the size the key value
    $transfer[$row['size']][]   =   $row;
}
# Now just loop over the first array which is the grouping
?>
<table>
    <?php foreach($transfer as $block => $rows): ?>
    <tr>
        <th>Name</th>
        <th>Size</th>
        <th>Thickness</th>
        <th>Price</th>
    </tr>
    <?php
    # Set the default rowspan as empty
    $rowspan    =   false;
    # This indicates an easy way to note that you need a new header row
    $start      =   true;
    # Loop each rows now
    foreach($rows as $row):
        if(!empty($start)) {
            # Assign rowspan
            $rowspan    =   count($rows);
            # This is the first row, so make sure the start is set false
            # for subsequent rows
            $start      =   false;
        }
    ?>
    <tr>
        <td><?php echo $row['name'] ?></td>
        <?php if(!empty($rowspan)): ?>
        <td rowspan="<?php echo $rowspan; $rowspan = false; ?>"><?php echo $row['size'] ?></td>
        <?php endif ?>
        <td><?php echo $row['thickness'] ?></td>
        <td><?php echo $row['price'] ?></td>
    </tr>
    <?php endforeach ?>
    <?php endforeach ?>
</table>

给你:

<table>
    <tr>
        <th>Name</th>
        <th>Size</th>
        <th>Thickness</th>
        <th>Price</th>
    </tr>
    <tr>
        <td>item1</td>
        <td rowspan="5">2 x 2</td>
        <td>.020</td>
        <td>$25</td>
    </tr>
    <tr>
        <td>item2</td>
        <td>.025</td>
        <td>$28</td>
    </tr>
    <tr>
        <td>item6</td>
        <td>.080</td>
        <td>$50</td>
    </tr>
    <tr>
        <td>item3</td>
        <td>.030</td>
        <td>$30</td>
    </tr>
    <tr>
        <td>item4</td>
        <td>.040</td>
        <td>$40</td>
    </tr>
    <tr>
        <th>Name</th>
        <th>Size</th>
        <th>Thickness</th>
        <th>Price</th>
    </tr>
    <tr>
        <td>item5</td>
        <td rowspan="4">3 x 2</td>
        <td>.050</td>
        <td>$43</td>
    </tr>
    <tr>
        <td>item6</td>
        <td>.050</td>
        <td>$43</td>
    </tr>
    <tr>
        <td>item7</td>
        <td>.050</td>
        <td>$43</td>
    </tr>
    <tr>
        <td>item8</td>
        <td>.050</td>
        <td>$43</td>
    </tr>
    <tr>
        <th>Name</th>
        <th>Size</th>
        <th>Thickness</th>
        <th>Price</th>
    </tr>
    <tr>
        <td>item9</td>
        <td rowspan="3">4 x 2</td>
        <td>.050</td>
        <td>$43</td>
    </tr>
    <tr>
        <td>item10</td>
        <td>.050</td>
        <td>$43</td>
    </tr>
    <tr>
        <td>item11</td>
        <td>.050</td>
        <td>$43</td>
    </tr>
</table>

修改

根据您关于如何生成$data数组的问题,我将首先执行此部分(此处以PDO为例):

$data = [];
while($result = $query->fetch(PDO::FETCH_ASSOC)){
    $data[$result['size']][] = $result;
}

然后你可以删除这部分:

#Create storage array
$transfer  =   [];
# Loop $data
foreach($data as $row) {
    # Make the size the key value
    $transfer[$row['size']][] =   $row;
}

更改此部分:

<?php foreach($transfer as $block => $rows): ?>

再次使用$data

<?php foreach($data as $block => $rows): ?>