[英]Is it possible to put switch case inside while loop?
The scenario is I am going to pull some data from the database and output it through pdf. 场景是我将从数据库中提取一些数据并通过pdf输出。 I use
FPDF
to make it happen and I am on the part where I need to getthe height from the database and display it according to color. 我使用
FPDF
来实现它,而我需要从数据库中获取高度并根据颜色显示它。
For example: 例如:
MY PROBLEM IS I'm trying to insert switch case
inside a while loop
to put the height on the table where some other data are being displayed. 我的问题是我正在尝试将
switch case
插入while loop
以将高度放在要显示其他数据的表上。 The picture below shows where I want to implement the colored cells . 下图显示了要在其中实现彩色单元的位置 。 It should be implemented at the "SS Height" column.
应该在“ SS Height”列中实现。
But it doesn't work and gave me this error. 但这不起作用,并给了我这个错误。
This is my codes. 这是我的代码。
<?php
require('connection.php');
$sql="SELECT * FROM table ORDER BY ssh REGEXP '^[^A-Za-z0-9]' ASC, ssh DESC";
$records=mysql_query($sql);
$fetch = mysql_fetch_array($records);
require("library/fpdf.php");
$pdf = new PDF('p', 'mm', 'Legal');
$title = 'Storm Surge Warning';
$pdf->SetTitle($title);
$pdf->AliasNbPages('{pages}');
$pdf->SetAutoPageBreak(true,25);
$pdf->AddPage();
$pdf->SetBorders(array('LT', 'LT', 'LT', 'LT', 'TLR'));
$pdf->SetWidths(array(30, 27, 35, 51, 51));
$pdf->SetAligns(array('C', 'C', 'C', 'L', 'L'));
$pdf->SetFont('Arial', 'B', 10);
$pdf->Row(array("SS Height",
"Provinces",
"Low Lying Coastal Areas in the Municipalities of:",
"IMPACTS",
"ADVICE/Actions to Take"), 1);
$pdf->SetFont('Arial', '', 11);
while($row = mysql_fetch_array($records)){
$pdf->Row(array(
switch ($row['ssh']) {
case '2-3Meters' :
$pdf->SetFillColor(204, 153, 0);
$pdf->Cell($row['ssh'], 1, 1, 'L', TRUE);
break;
case '1-2Meters' :
$pdf->SetFillColor(255, 255, 0);
$pdf->Cell($row['ssh'], 1, 1, 'L', TRUE);
break;
case '<1Meter' :
$pdf->SetFillColor(51, 153, 255);
$pdf->Cell($row['ssh'], 1, 1, 'L', TRUE);
break;
default:
$pdf->Cell($row['ssh'], 1, 1, 'L', FALSE);
break;
}
$row['provi'],
$row['muni'],
$row['impact'],
$row['advice']), 1);
}
$pdf->SetBorders(array('T', 'T', 'T', 'T', 'T'));
$pdf->Row(array('','','','',''), 1, false, 1);
$pdf->OutPut();
?>
Your switch (TRUE) should use the field with the height probably something like the code below: 您的开关(TRUE)应该使用高度如下的代码:
switch ($row['ssh'] ) {
case '2-3Meters' : {
$pdf->SetFillColor(204, 153, 0);
$pdf->Cell($row['ssh'], 1, 1, 'L', TRUE);
break;
}
case '1-2Meters' : {
$pdf->SetFillColor(255, 255, 0);
$pdf->Cell($row['ssh'], 1, 1, 'L', TRUE);
break;
}
case '<1Meter' : {
$pdf->SetFillColor(51, 153, 255);
$pdf->Cell($row['ssh'], 1, 1, 'L', TRUE);
break;
}
default:
...
Anyway, without knowing how the FPDF API works, it seems it just retains global states. 无论如何,在不了解FPDF API如何工作的情况下,似乎它只是保留了全局状态。
So call, the switch + colorization first (an array map would be simpler), then add the ->Cell, and lastly the ->Row, as in: 因此,首先调用switch +着色(数组映射会更简单),然后添加-> Cell,最后添加-> Row,如下所示:
while($row = mysql_fetch_array($records)){
switch ($row['ssh']) {
case '2-3Meters' :
$pdf->SetFillColor(204, 153, 0);
…
}
$pdf->Cell($row['ssh'], 1, 1, 'L', TRUE);
$pdf->Row(array(1,2,3,4,1));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.