Search code examples
phpfpdf

printing array values into a single cell through while loop in fpdf


$pendReasonNA="select cmpID from onlinecmp where year(cmpDate)='2014'
and  rectstatus=0   
and rectremarks='Not Available in store' 
order by cmpID asc";

$query3=mysql_query($pendReasonNA);
$num_rows=mysql_num_rows($query3);


while($result=mysql_fetch_array($query3))
{
$pdf->Cell(55, 6, $result[0], 1, 0, 'L', 1);
}

In the above code what i am getting is value of $result in multiple cells one beside the other. i would like to print all the values in array $result with a comma separating each array value in a single cell(above cell) in fpdf. How do i achieve this in fpdf?Plz help.


Solution

  • This should work:

    while($result=mysql_fetch_array($query3))
    {
       $queryResult = $queryResult . ', ' .$result[0];
    }
    $pdf->Cell(55, 6, $queryResult, 1, 0, 'L', 1);
    

    The above is adding each result to $queryResult and adding a comma after it, then when all the results are printed it will print the $queryResult to the cell.

    Also you should avoid using mysql_* as this answer:

    The MySQL extension is:

    • Not under active development
    • Officially deprecated (as of PHP 5.5. It's likely to be removed in the next major release.)
    • Lacks an OO interface
    • Doesn't support:
      • Non-blocking, asynchronous queries
      • Prepared statements or parameterized queries
      • Stored procedures
      • Multiple Statements
      • Transactions
      • All of the functionality in MySQL 5.1

    Since it is deprecated, using it makes your code less future proof.

    Lack of support for prepared statements is particularly important as they provide a clearer, less error prone method of escaping and quoting external data than manually escaping it with a separate function call.

    See the comparison of SQL extensions.