如何生成超过5个条件的表

时间:2012-01-31 21:43:00

标签: php algorithm mysqli html-table

使用条形码生成表格。每个项目在数据库表中都有确切的数量。表中的字段是有限的:65,如果超过65则构建第二个表,然后是第三个表...如何生成具有此条件的表?

enter image description here

详细

假设我们要生成一个包含65个可用字段的表(5x13)。

我的计划如下

  1. 用户选择项目复选框
  2. 当用户提交表单时,PHP获取已选中复选框的值
  3. PHP从数据库中获取每个项目的数量
  4. 生成表格
  5. 对于前。商品ID 55的数量为2,而56为4则表格必须为

    enter image description here

    我的代码看起来像那样(我知道这是错的,但我无法弄清楚它是如何必须的。必须有超过5个计数器:行计数器,列计数器,$ _POST ['id']计数器,物品的数量计数器,表计数器(如果总和超过65))

    更新

        <?php
                $items = array();
                foreach ($_POST['checkbox'] as $id) {
                    $stmt = $db->prepare("SELECT `qt` FROM `items` WHERE `id`=?");
                    $stmt->bind_param('i', $id);
                    $stmt->execute();
                    $stmt->bind_result($qt);
                    $stmt->fetch();
                    $stmt->close();
                    for ($cnt = 1; $cnt <= $qt; $cnt++)
                        $items[] = $id;
                }
                $i = 0;
                foreach ($items as $item) {
                    for ($j = 0; $j < $item['quantity']; $j++) {
    
                        // check if it's the beginning of a new table
                        if ($i % 65 == 0)
                            echo '<table>';
    
                        // check if it's the beginning of a new row
                        if ($i % 5 == 0)
                            echo '<tr>';
    
                        echo '<td><img src="bc.php?id=' . $item['id'] . '" alt="' . $item['name'] . '" /></td>';
    
                        // check if it's the end of a row
                        if (($i - 1) % 5 == 0)
                            echo '</tr>';
    
                        // check if it's the end of a table
                        if (($i - 1) % 65 == 0)
                            echo '</tr></table>';
    
                        $i++;
                    }
                }
    
    // if the last row wasn't closed, close it
                if ($i % 5 != 0)
                    echo '</tr>';
    
    // if the last table wasn't closed, close it
                if ($i % 65 != 0)
                    echo '</table>';
                ?>
    

    有什么建议吗?

3 个答案:

答案 0 :(得分:1)

编辑4

<?php

$i = 0;
foreach ($_POST['checkbox'] as $id) {
    $stmt = $db->prepare("SELECT `qt` FROM `items` WHERE `id`=?");
    $stmt->bind_param('i', $id);
    $stmt->execute();
    $stmt->bind_result($qt);
    $stmt->fetch();
    $stmt->close();

    for ($cnt = 1; $cnt <= $qt; $cnt++) {
        // check if it's the beginning of a new table
        if ($i % 65 == 0)
            echo '<table>';

        // check if it's the beginning of a new row
        if ($i % 5 == 0)
            echo '<tr>';

        echo sprintf('<td><img src="bc.php?id=%1$d" alt="%1$d" /></td>', $id);

        // check if it's the end of a row
        if (($i + 1) % 5 == 0)
            echo '</tr>';

        // check if it's the end of a table
        if (($i + 1) % 65 == 0)
            echo '</table>';

        $i++;
    }
}

// if the last table isn't full, print the remaining cells
if ($i % 65 != 0) {
    for ($j = $i%65; $j < 65; $j++) {
        if ($j % 65 == 0) echo '<table>';
        if ($j %  5 == 0) echo '<tr>';
        echo '<td></td>';
        if (($j + 1) %  5 == 0) echo '</tr>';
        if (($j + 1) % 65 == 0) echo '</table>';
    }
}

答案 1 :(得分:1)

你需要重复制作65个单元格表格,直到完成为止。 我要去伪代码:

data rows = resultOfMyQuery();
int index = 0;
while(index < rows.count()) {
 index = createASheet(rows, index);
}
END;

int createASheet(data rows, int index) {
  int availableCells = 65;
  int column = 0;
  print("<table>");
  while (availableCells > 0) {
    if (index < rows.count()) {
      data row = rows.get(index);
      int quantity = row.getQuantity();
      if (quantity > availableCells) {
        // Stay on this item with reduced quantity for next sheet.
        row.setQuantity(quantity - availableCells);
        rows.set(index, row);
      } else {
        // Move on to next item on this (or next) sheet.
        index++;
      }
      for (i=0; i<quantity && availableCells>0; quantity--) {
        column = makeACell(column, StringFormat("<TAG ATTRIB='%d'></TAG>",row.getId()));
        availableCells--;
      }
    } else {
      // fill in empty cells
      column = makeACell(column, "");
      availableCells--;
    }
  }
  print("</table>");
  return index;
}

int makeACell(int column, String filling) {
  String cell = "";
  if (column == 0) {
    cell.append("<tr>");
  }
  cell.append("<td>").append(filling).append("</td>");
  if (column == 4) {
    cell.append("</tr>");
  }
  print cell;
  return (column+1) % 5;
}

答案 2 :(得分:0)

为什么不使用像QR码这样的现有条形码系统?这些标准提供了构建代码的库,并且可以被各种设备读取。

如果您真的想要构建自己的条形码,我强烈建议您使用二进制系统来存储您的值。考虑一下支票金额。