在html PHP循环中模拟a4页面

在html PHP循环中模拟a4页面,php,loops,Php,Loops,我想创建一个循环,当它达到20时,只需创建一个新页面,然后再创建一个新页面,直到循环完成 我每页有5行4列5x4=20个标签 其特点是我有四种价格,标签的数量取决于总数量。我举个例子。 我有broi、price1、price2、price3、price4列 使用示例数据: 布罗伊:90 价格1:4.85 价格2:7.90 价格3:9.30 价格4:11.10 我们除以broi90/4,从1的价格中得到22.5,例如: 价格1:4,85-22件 价格2:7.90-24件 价格3:9.30-22件

我想创建一个循环,当它达到20时,只需创建一个新页面,然后再创建一个新页面,直到循环完成

我每页有5行4列5x4=20个标签

其特点是我有四种价格,标签的数量取决于总数量。我举个例子。 我有broi、price1、price2、price3、price4列 使用示例数据:

布罗伊:90

价格1:4.85

价格2:7.90

价格3:9.30

价格4:11.10

我们除以broi90/4,从1的价格中得到22.5,例如:

价格1:4,85-22件

价格2:7.90-24件

价格3:9.30-22件

价格4:11.10-22件 总pcs 90=broi

下面是一个示例,通过快照,

我想成为:

我的php代码是:

<?php
$mysqli = new mysqli('localhost', 'user', 'pass', 'db');
/* check connection */
if ($mysqli->connect_errno) {
   printf("Connect failed: %s\n", $mysqli->connect_error);
   exit();
}
$mysqli->set_charset("utf8");

/* разделяме на 4 и правиме второто число да компенсира остатъка */
function calculate_columns(int $total, int $size, int $prefer = 1): array {
    $columns = [];
    for ($i = 0; $i < $size; $i++) {
        $columns[$i] = floor($total / $size);
    }
    $columns[$prefer] += $total - $columns[$prefer] * $size;
    return $columns;
    /*
     Array ( 
         [0] => 22 
         [1] => 24 
         [2] => 22 
         [3] => 22 
     ) 
     */
}
?><html>
    <head>
        <style>body {
  background: rgb(204,204,204); 
}
page {
  background: white;
  display: block;
  margin: 0 auto;
  margin-bottom: 0.5cm;
  //box-shadow: 0 0 0.5cm rgba(0,0,0,0.5);
}
page[size="A4"] {  
  width: 21cm;
  height: 29.7cm; 
}
page[size="A4"][layout="landscape"] {
  width: 29.7cm;
  height: 21cm;  
}
page[size="A3"] {
  width: 29.7cm;
  height: 42cm;
}
page[size="A3"][layout="landscape"] {
  width: 42cm;
  height: 29.7cm;  
}
page[size="A5"] {
  width: 14.8cm;
  height: 21cm;
}
page[size="A5"][layout="landscape"] {
  width: 21cm;
  height: 14.8cm;  
}
@media print {
  body, page {
    margin: 0;
    box-shadow: 0;
  }
}

/* Container holding the image and the text */
.container {
  position: relative;
  text-align: center;
  margin-left:10px;
  color: #000 ;
  font-size:19px !important;
  font-weight: bold; font: arial;
}

/* Centered text */
.centered {
  position: absolute;
  top: 70%;
  left: 50%;
  transform: translate(-50%, -50%);
  margin-left:6px;
  
}


.A4 {
  background: white;
  width: 21cm;
  height: 29.7cm;
  display: block;
  margin: 0 auto;
  padding: 10px 25px;
  margin-bottom: 0.5cm;
  box-shadow: 0 0 0.5cm rgba(0, 0, 0, 0.5);
  overflow-y: scroll;
  box-sizing: border-box;
  font-size: 12pt;
}

@media print {
  .page-break {
    display: block;
    page-break-before: always;
  }
  size: A4 portrait;
}

@media print {
  body {
    margin: 0;
    padding: 0;
  }
  .A4 {
    box-shadow: none;
    margin: 0;
    width: auto;
    height: auto;
  }
  .noprint {
    display: none;
  }
  .enable-print {
    display: block;
  }
}


</style>
<script>
  //window.print();
</script>
</head>
<body>
<?php
$lstoutput = array();
$sqlquery = $mysqli->query("SELECT * FROM items WHERE id=1");
while($row = $sqlquery->fetch_array()) {
    $lstoutput[] = $row;
}
    $labels = calculate_columns($lstoutput[0]['broi'], 4);
        $page_much = $lstoutput[0]['broi']/20; //$labels[0]
        $page_number = '0';
        for(; $page_number < $page_much ; $page_number++){
            echo '<page size="A4"><table cellpadding="6" style="padding-top: 30px"><tbody>';
            if($lstoutput[0]['price1'] != null) {
                $labels_number = 0;
                for ($labels_number = 0; $labels_number <= $labels[0]; $labels_number++) {
                    if ($labels_number %4 === 0) {
                        echo("</tr>\n<tr style='margin:1px'>");
                    }
                    echo '<td style="margin:1px" class="container"><img src="label.png" alt="label" style="border:1px solid #333;width:184px;height:184px" /><div class="centered">'.$lstoutput[0]['price1'].'</div></td>';
                }
            }
            if($lstoutput[0]['price2'] != null) {
                $labels_number = 0;
                for ($labels_number = 0; $labels_number <= $labels[0]; $labels_number++) {
                    if ($labels_number %4 === 0) {
                        echo("</tr>\n<tr style='margin:1px'>");
                    }
                    echo '<td style="margin:1px" class="container"><img src="label.png" alt="label" style="border:1px solid #333;width:184px;height:184px" /><div class="centered">'.$lstoutput[0]['price2'].'</div></td>';
                }
            }
            echo '</tbody></table></page>';
        }
?>
    </body>
</html>

@DinoCoderSaurus我设置代码后,我删除了创建页面的循环次数,在css函数之后添加了分页符,但没有再次获得它,设置了photo+代码

   table { page-break-inside:auto }
   tr    { page-break-inside:avoid; page-break-after:auto }

如果使用css display:inline块将标签设置为div,而不是使用表,则浏览器本身将回流元素以适合每个页面

.标签{ 显示:内联块; 宽度:200px; 高度:100px; 边框:1px纯黑; 填充物:5px; 保证金:5px; } 标签 标签 标签 标签 标签
…您可能需要查看css属性。不,css没有解决此问题。我认为错误在我的逻辑中,但我不知道确切的位置。基于1做了类似的事情,但没有那么复杂,2对代码理解不太好,建议:不要在php中处理看起来像for page_number循环的目的的分页。相反,让@media print样式处理它。您可能需要删除该表并使用divs。实际上,如果只使用div,就不需要做任何特殊的事情。我将用这种方法写一个答案。标签计数器不应该是每种价格,而应该是每种标签。在每个标签后向计数器添加1,当计数器%4==0时,关闭表行。谢谢@solarc,但不要使用我的代码。我不明白为什么我的代码不起作用:/
<?php
$mysqli = new mysqli('localhost', 'USER', 'PASS', 'DATABASE');
/* check connection */
if ($mysqli->connect_errno) {
   printf("Connect failed: %s\n", $mysqli->connect_error);
   exit();
}
$mysqli->set_charset("utf8");

/* разделяме на 4 и правиме второто число да компенсира остатъка */
function calculate_columns(int $total, int $size, int $prefer = 1): array {
    $columns = [];
    for ($i = 0; $i < $size; $i++) {
        $columns[$i] = floor($total / $size);
    }
    $columns[$prefer] += $total - $columns[$prefer] * $size;
    return $columns;
    /*
     Array ( 
         [0] => 22 
         [1] => 24 
         [2] => 22 
         [3] => 22 
     ) 
     */
}
?><html>
    <head>
        <style>body {
  background: rgb(204,204,204); 
}
page {
  background: white;
  display: block;
  margin: 0 auto;
  margin-bottom: 0.5cm;
  //box-shadow: 0 0 0.5cm rgba(0,0,0,0.5);
}
page[size="A4"] {  
  width: 21cm;
  height: 29.7cm; 
}
page[size="A4"][layout="landscape"] {
  width: 29.7cm;
  height: 21cm;  
}
page[size="A3"] {
  width: 29.7cm;
  height: 42cm;
}
page[size="A3"][layout="landscape"] {
  width: 42cm;
  height: 29.7cm;  
}
page[size="A5"] {
  width: 14.8cm;
  height: 21cm;
}
page[size="A5"][layout="landscape"] {
  width: 21cm;
  height: 14.8cm;  
}
@media print {
  body, page {
    margin: 0;
    box-shadow: 0;
  }
}

/* Container holding the image and the text */
.container {
  position: relative;
  text-align: center;
  margin-left:10px;
  color: #000 ;
  font-size:19px !important;
  font-weight: bold; font: arial;
}

/* Centered text */
.centered {
  position: absolute;
  top: 70%;
  left: 50%;
  transform: translate(-50%, -50%);
  margin-left:6px;

}


.A4 {
  background: white;
  width: 21cm;
  height: 29.7cm;
  display: block;
  margin: 0 auto;
  padding: 10px 25px;
  margin-bottom: 0.5cm;
  box-shadow: 0 0 0.5cm rgba(0, 0, 0, 0.5);
  overflow-y: scroll;
  box-sizing: border-box;
  font-size: 12pt;
}

@media print {
  .page-break {
    display: block;
    page-break-before: always;
  }
  size: A4 portrait;
}

@media print {
  body {
    margin: 0;
    padding: 0;
  }
  .A4 {
    box-shadow: none;
    margin: 0;
    width: auto;
    height: auto;
  }
  .noprint {
    display: none;
  }
  .enable-print {
    display: block;
  }
}

   table { page-break-inside:auto }
   tr    { page-break-inside:avoid; page-break-after:auto }

</style>
<script>
  //window.print();
</script>
</head>
<body>
<?php
$lstoutput = array();
$sqlquery = $mysqli->query("SELECT * FROM items WHERE id=1");
while($row = $sqlquery->fetch_array()) {
    $lstoutput[] = $row;
}
    $labels = calculate_columns($lstoutput[0]['broi'], 4);
            echo '<page size="A4"><table cellpadding="5" style="padding-top: 10px"><tbody>';
            if($lstoutput[0]['price1'] != null) {
                $labels_number = 0;
                for ($labels_number = 0; $labels_number <= $labels[0]; $labels_number++) {
                    if ($labels_number %4 === 0) {
                        echo("</tr>\n<tr style='margin:1px'>");
                    }
                    echo '<td style="margin:1px" class="container"><img src="label.png" alt="label" style="border:1px solid #000;width:90%;height:90%" /><div class="centered">'.$lstoutput[0]['price1'].'</div></td>';
                }
            }
            if($lstoutput[0]['price2'] != null) {
                $labels_number = 0;
                for ($labels_number = 0; $labels_number <= $labels[1]; $labels_number++) {
                    if ($labels_number %4 === 0) {
                        echo("</tr>\n<tr style='margin:1px'>");
                    }
                    echo '<td style="margin:1px" class="container"><img src="label.png" alt="label" style="border:1px solid #000;width:90%;height:90%" /><div class="centered">'.$lstoutput[0]['price2'].'</div></td>';
                }
            }
            echo '</tbody></table></page>';
?>
    </body>
</html>