【发布时间】:2012-02-23 15:13:56
【问题描述】:
短
生成带有商品条码的表格。每个项目在数据库表中都有确切的数量。表中的字段是有限的:65个,如果超过65则构建第二个表,然后第三个...如何生成具有这种条件的表?
详细
假设我们要生成一个包含 65 个可用字段 (5x13) 的表。
我的计划如下
- 用户选择项目的复选框
- 当用户提交表单时,PHP 获取选中复选框的值
- PHP 从数据库中获取每个项目的数量
- 生成表
例如。项目 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