简体   繁体   中英

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

For example:

  • If the height is 2-3Meters then it should be color brown
  • If the height is 1-2Meters then it should be color yellow
  • If the height is <1Meter then it should be color blue

在此处输入图片说明

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. The picture below shows where I want to implement the colored cells . It should be implemented at the "SS Height" column.

在此处输入图片说明

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:

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.

So call, the switch + colorization first (an array map would be simpler), then add the ->Cell, and lastly the ->Row, as in:

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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM