here i am using fpdf class to generate pdf document, how to print serial numbers in SL.NO. column in sequential order i.e. 1 2 3 4 5 ... so on.

<?php
include_once('include/dbConnect.inc.php');
require('fpdf.php');
$d=date('d_m_Y');
$agentid=mysql_real_escape_string($_GET['agent']);
$agent_name=mysql_real_escape_string($_GET['agentname']);

// Query to select an int column
$query = "select count(*) as cnt from custreg where magentid = '$agentid'";
$result = mysql_query($query);
$cntrow = mysql_fetch_row($result);

class PDF extends FPDF
{
// Page header
function Header()
{
    global $agent_name;
    global $agentid;
    global $number;
    // Logo
    $this->Image('images/logo.png',10,6,30);
    // Arial bold 15
    $this->SetFont('Arial','B',15);
    // Move to the right
    $this->Cell(80);
    // Title
    $this->Cell(5,1,'Customer Payment Details',0,0,'C');
    $this->Cell(10,15,'Agent ID: '.$agentid.'  '.'Agent Name: '.$agent_name,0,0,'C');
    $this->Cell(1,30,'Total Cards: '.$cntrow[0],0,0,'C');   
       // Line break
    $this->Ln(20);
}

// Page footer
function Footer()
{
    // Position at 1.5 cm from bottom
    $this->SetY(-15);
    // Arial italic 8
    $this->SetFont('Arial','I',8);
    // Page number
    $this->Cell(0,10,'Page '.$this->PageNo(),0,0,'C');
}


//Load data
function LoadData($file)
{
    //Read file lines
    $lines=file($file);
    $data=array();
    foreach($lines as $line)
        $data[]=explode(';',chop($line));
    return $data;
}

// Better table
function ImprovedTable($header, $data)
{
    global $cntrow; 
    // Column widths
    $w = array(15, 40, 40, 180);
    // Header
    for($i=0;$i<count($header);$i++)
        $this->Cell($w[$i],7,$header[$i],1,0,'C');
    $this->Ln();
    // Data
    foreach($data as $eachResult)
    {

        $this->Cell($w[0],6,'SL.NO',1,'LR');      
        $this->Cell($w[1],6,$eachResult["ac_no"],1,'LR');
        $this->Cell($w[2],6,$eachResult["cust_name"],1,'LR');       
        $this->Cell($w[3],6,$eachResult["installamt"],1,'LR');      
        $this->Ln();

    } 
    // Closing line
    $this->Cell(array_sum($w),0,'','T');
}
} 

$pdf=new PDF();
$header=array('SL NO','CARD NO','CUSTOMER NAME','INSTALLMENT AMOUNT');

$strSQL = "SELECT ac_no, cust_name, install_no, GROUP_CONCAT(`install_amt` ORDER BY `install_no` SEPARATOR ' | ') as installamt FROM slabpay where agent_id=$agentid GROUP BY ac_no";
$objQuery = mysql_query($strSQL);
$resultData = array();
for ($i=0;$i<mysql_num_rows($objQuery);$i++) {
    $result = mysql_fetch_array($objQuery);
    array_push($resultData,$result);
}
//************************//


function forme()

{
$d=date('d_m_Y');
echo "PDF generated successfully. To download document click on the link >> <a href=".$d.".pdf>DOWNLOAD</a>";
}


//*** Table 1 ***//
$pdf->AddPage('L');
$pdf->SetFont('Arial','B',8);
$pdf->Ln(35);
$pdf->ImprovedTable($header,$resultData);
forme();
$pdf->Output("$d.pdf","F");

?>

Recommended Answers

All 2 Replies

Member Avatar for diafol

Is this a question or a code snippet? You do not state where the problem lies (if there if one), so are we to assume that this is a snippet?

Change this

foreach($data as $eachResult)
    {

        $this->Cell($w[0],6,'SL.NO',1,'LR');      
        $this->Cell($w[1],6,$eachResult["ac_no"],1,'LR');
        $this->Cell($w[2],6,$eachResult["cust_name"],1,'LR');       
        $this->Cell($w[3],6,$eachResult["installamt"],1,'LR');      
        $this->Ln();

    } 

To

foreach($data as $key=>$eachResult)
    {
        $key++;
        $this->Cell($w[0],6,$key,1,'LR');      
        $this->Cell($w[1],6,$eachResult["ac_no"],1,'LR');
        $this->Cell($w[2],6,$eachResult["cust_name"],1,'LR');       
        $this->Cell($w[3],6,$eachResult["installamt"],1,'LR');      
        $this->Ln();

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