hi everyone,
i write data from database to the csv file,but i face problem how to print the csv file with multiple colors background for example first line is green then second line must be blue,
please help me how can i do this in php

Dani AI

Generated

For : CSV is plain text — it carries values and delimiters only, not fonts, colors, or other presentation. To get colored backgrounds you must produce a styled output (HTML/CSS), a real spreadsheet file (.xlsx) with cell styles, or a PDF — you cannot embed colors into a .csv file itself. (rfc-editor.org)

As noted, rendering the data as an HTML table and printing from the browser is the simplest route if the target is a paper printout. Keep in mind many browsers (and some users) disable background printing by default, and browsers may adjust colors for print — use print-oriented CSS and the -webkit-print-color-adjust / print-color-adjust strategies or ask users to enable “print backgrounds” when necessary. (developer.mozilla.org)

If the goal is a reusable spreadsheet with persistent row colors (so users can open, edit, and still keep the colors), generate an .xlsx on the server. The PHPOffice PhpSpreadsheet library supports setting cell fill styles so you can alternate row backgrounds in PHP and save a styled workbook that Excel/LibreOffice will honor. Example (minimal):

require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Fill;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$row = 1;
foreach ($rows as $i => $data) {
  $sheet->fromArray($data, null, "A{$row}");
  $color = ($i % 2) ? 'FFEEFF' : 'EEFFEE'; // ARGB
  $sheet->getStyle("A{$row}:Z{$row}")
        ->getFill()
        ->setFillType(Fill::FILL_SOLID)
        ->getStartColor()
        ->setARGB($color);
  $row++;
}
(new Xlsx($spreadsheet))->save('styled.xlsx');

See PhpSpreadsheet docs for the style/fill API and library details. (phpoffice.github.io)

If exact printed output is required across many client machines, produce a PDF on the server (headless Chrome / Puppeteer or an HTML-to-PDF tool). Server-side PDF generation avoids users' browser print-settings differences and yields predictable colored output. For quick choices: HTML+print-CSS for browser printing, PhpSpreadsheet for editable spreadsheets, or server-side PDF for fixed print output. (pptr.dev)

Troubleshooting notes: opening a CSV in Excel and adding formatting will be lost if you save back to CSV — save as .xlsx to preserve styles. Test printing with real printer settings (many printers/browsers strip backgrounds) and prefer light tints for ink economy. (support.microsoft.com)

Did you say print ? To print on paper ?

I am doing the following: display HTML code on the screen and use CSS media=print.
(Sorry for the poor layout below)

echo'<style type="text/css" media="print">';
echo "TABLE {
 border:1pt solid #000;
 border-collapse:collapse;
 font-family:arial,sans-serif;
 font-size:90%;
 background-color: red;
}
TD,TH{
 border:1pt solid #000;
 border-collapse:collapse;
 white-space: nowrap;
 font-size:90%;
 text-align:center;
}";
echo'</style>';

echo "<div STYLE=' height: 257pt; font-size: 12pt; overflow: auto;'>";
echo "<TABLE BORDER=1 WIDTH='100%'>";
.
.
.
  for ($i =  0; $i < $nrg88_num_rows; $i++)
     {
      $nrg88_row                       = mysql_fetch_row($nrg88_sql_result);

      $nrg88_color = '#ffffff';
      if ($i % 2 == 1)
        {
         $nrg88_color = '#eeeeee';
        }
      echo "<TR>";
      $nrg88_col0[$i]                  = $nrg88_row[0];
      echo "<TD style='width:7.5%; background-color:$nrg88_color;'>$nrg88_col0[$i]</TD>";
      $nrg88_col1[$i]                  = $nrg88_row[1];
      echo "<TD style='width:15%; background-color:$nrg88_color;'>$nrg88_col1[$i]</TD>"; 
      $nrg88_col2[$i]                  = $nrg88_row[2];
      echo "<TD style='width:7.5%; background-color:$nrg88_color;'>$nrg88_col2[$i]</TD>";
      $nrg88_col3[$i]                  = $nrg88_row[3];
      echo "<TD style='width:7.5%; background-color:$nrg88_color;'>$nrg88_col3[$i]</TD>";
      $nrg88_col4[$i]                  = $nrg88_row[4];
      echo "<TD style='width:7.5%; background-color:$nrg88_color;'>$nrg88_col4[$i]</TD>";
      $nrg88_col5[$i]                  = $nrg88_row[5];
      echo "<TD style='width:7.5%; background-color:$nrg88_color;'>$nrg88_col5[$i]</TD>";
      $nrg88_col6[$i]                  = $nrg88_row[6];
      echo "<TD style='width:7.5%; background-color:$nrg88_color;'>$nrg88_col6[$i]</TD>";
      $nrg88_col7[$i]                  = $nrg88_row[7];
      echo "<TD style='width:7.5%; background-color:$nrg88_color;'>$nrg88_col7[$i]</TD>";
      $nrg88_col8[$i]                  = $nrg88_row[8];
      echo "<TD style='width:7.5%; background-color:$nrg88_color;'>$nrg88_col8[$i]</TD>";
      $nrg88_col9[$i]                  = $nrg88_row[9];
      echo "<TD style='width:7.5%; background-color:$nrg88_color;'>$nrg88_col9[$i]</TD>";
      $nrg88_col10[$i]                 = $nrg88_row[10];
      echo "<TD style='width:7.5%; background-color:$nrg88_color;'>$nrg88_col10[$i]</TD>";
      echo "</TR>";
     
     }
     
      #echo "</TR>";
      echo "</TABLE>";
      echo "</div>";

Would be glad if this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.