SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    FPFD If condition

    I need to test a condition while printing:

    PHP Code:
    <?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?
    Last edited by Dan Grossman; Mar 12, 2010 at 23:59. Reason: Added code tags

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Can you explain what you want help with more clearly?

  3. #3
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  4. #4
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Where do you you want to do this Jose... Pardon me if I missed it in your code.
    but:
    PHP Code:
    if(empty($data)){
    //do something if $data IS empty
    }else{ // do something if it isn't 

    Ryan B | My Blog | Twitter

  5. #5
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    after
    $pdf=new PDF();
    $pdf->AddPage('P','A4');
    $data=$pdf->LoadData();
    if ($data)etc
    $pdf->Output();
    ?>

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    }

  7. #7
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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).

  9. #9
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, that makes 'if else' and 'if not' a dyslexic blockade. Thank you for your time.

  10. #10
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by josemariagomes View Post
    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.

    Code php:
    $hasData =!empty($data);
    if (!$hasData) {
        ...
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  11. #11
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •