FPFD If condition

I need to test a condition while printing:

<?php
require('fpdf.php');

$link=mysql_connect("DNS", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

class PDF extends FPDF
{
//Load data
function LoadData()
{
$query = "SELECT fields from table where condition order by id";
$Result = mysql_query( $query );
$size=mysql_num_rows($Result);
for($i=0;$i<$size;$i++)
$row[$i] = mysql_fetch_row($Result);
mysql_free_result($Result);
$data=array();
for($i=0;$i<$size;$i++)
{
$data[] = $row[$i];
}
return $data;
}

function tableResults($header,$data)
{
//Column widths
//Header
//for
//Data
{
//foreach
}
}

function Header()
{
    //header code

}

function Footer()
{
   //footercode
}
}

$pdf=new PDF();
$pdf->AddPage('P','A4');
$data=$pdf->LoadData();

if (test $data)
{
//if there is data print queryResult
}
else
{
//if there is no data resume next
}
$pdf->Output();
?>

Any suggestion other then !empty?

Can you explain what you want help with more clearly?

Yes!
Actual testing condition is:
!empty($data)
if true query result
if false resume next
It works in the first tests. I am not sure of this, because the result is inverted to the syntax:
!empty returns true if there is data, false if there isn’t.
Can I rely on !empty the way it is, or do you suggest another approach?

Where do you you want to do this Jose… Pardon me if I missed it in your code.
but:

if(empty($data)){
//do something if $data IS empty
}else{ // do something if it isn't 
}

after
$pdf=new PDF();
$pdf->AddPage(‘P’,‘A4’);
$data=$pdf->LoadData();
if ($data)etc
$pdf->Output();
?>

LoadData() returns an array, so if you want to know if it is empty:
if(empty($data)) {
//$data is not set or set to null/false/etc
}

Thank you. The thing is (!empty($data)) returns true if the array is full, else returns false.
My doubt was that it would be the correct way to always obtain the required result, true=-1, false=0.
So far what ever it returns, returns inversed to my expectation.
Thank you again.

Read that condition out loud:

“If data is not empty”

Does it make sense for you now?

“Data is not empty” is true when the array is full.

“Data is not empty” is false when the array has no elements (is empty).

Thank you, that makes ‘if else’ and ‘if not’ a dyslexic blockade. Thank you for your time.

You can make it less of a dyslexic blockade by the use of a well named variable.


$hasData =!empty($data);
if (!$hasData) {
    ...
}

Yes testing the condition. It is good practice. I will not go any further with it, since the result I am obtaining is quite enough, and occurrences have happened so far. Thank you for your assurances.