【问题标题】:How to generate table with more than 5 conditions如何生成超过5个条件的表
【发布时间】:2012-02-23 15:13:56
【问题描述】:

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

详细

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

我的计划如下

  1. 用户选择项目的复选框
  2. 当用户提交表单时,PHP 获取选中复选框的值
  3. PHP 从数据库中获取每个项目的数量
  4. 生成表

例如。项目 id 55 的数量是 2,而 56 的数量是 4,那么表格必须是这样的

我的代码看起来是这样的(我知道它错了,但我不知道它是怎么回事。必须有 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>';
            ?>

有什么建议吗?

【问题讨论】:

  • 这是一个非常奇怪的问题 :) 我想我明白你在问什么,但为什么要那样做呢?为什么不这样说: Record1=ItemID(55),ItemCount(2) ; Record2=ItemID(56),ItemCount(4)?
  • +1 用于清楚地解释您想要什么的图形

标签: php algorithm mysqli html-table


【解决方案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>';
    }
}

【讨论】:

  • 每件商品的数量如何?
  • 我首先将所有项目放入 $items 数组中 - 与所需数量一样多。
  • 我的意思是,比方说,在 db 表中,我们发现 item id 55 的数量是 2,而 56 的数量是 4。接下来要做什么?
  • @trl13,当然,你也可以用两个循环来做 - 请参阅我编辑答案的第二部分。
  • 请检查更新的问题(代码)。我认为这是真的,对吧?
【解决方案2】:

您只需重复制作 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;
}

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      相关资源
      最近更新 更多