【问题标题】:Simulate a4 page in html PHP LOOP在 html PHP LOOP 中模拟 a4 页面
【发布时间】:2019-03-06 17:55:41
【问题描述】:

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

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

特殊之处在于我有四个价格,标签的数量取决于总数。我举个例子。 我有列 broi、price1、price2、price3、price4 有样本数据:

兄弟:90

价格1:4.85

价格2:7.90

价格3:9.30

价格4:11.10

我们将 (broi)90/4 除以 1 的价格,我们必须得到 22.5,例如:

价格1:4,85 - 22 件

价格2:7.90 - 24pcs

价格3:9.30 - 22pcs

price4: 11.10 - 22pcs (总 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>

【问题讨论】:

  • 您可能想查看break-after css 属性。
  • 不,CSS 没有解决问题。我认为错误在我的逻辑中,但我不知道确切的位置。
  • 基于 1) 做过类似的事情(虽然没有那么复杂)和 2) 不是特别了解代码,建议:不要处理 php 中的分页(这看起来像目的for page_number 循环)。相反,让@media print 样式处理它。

标签: php loops


【解决方案1】:

如果您使用 css display: inline-block 将标签设置为 div 而不是使用表格,浏览器本身将重新排列元素以适应每个页面。

.label {
  display: inline-block;
  width: 200px;
  height: 100px;
  border: 1px solid black;
  padding: 5px;
  margin: 5px;
}
<div class="label">label</div>
<div class="label">label</div>
<div class="label">label</div>
<div class="label">label</div>
<div class="label">label</div>
...

这在我的预览窗口中看起来像这样:

【讨论】:

  • 感谢@solarc,但不要为我的代码工作。我不明白为什么我的代码不起作用:/
【解决方案2】:

@DinoCoderSaurus 我设置了我去掉循环创建页数后的代码,添加了page-break-after函数(css)但是又没拿到,设置照片+代码。

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

<?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>

【讨论】:

  • 您可能需要删除表格并改用 div。实际上,如果你只使用 div,你真的不需要做任何特别的事情。我会用这种方法写一个答案。
  • 标签计数器不应按价格计算,而应按标签计算。每个标签后的计数器加1,当counter % 4 == 0时,关闭表格行。
猜你喜欢
  • 2014-01-22
  • 2011-09-28
  • 2017-10-08
  • 2010-10-11
  • 1970-01-01
  • 2017-01-22
  • 2011-03-21
相关资源
最近更新 更多