Printf with conditional data

Apologies if this is something I should know. I am editing some PHP code that I inherited from a predecessor. I have added the following printf statment:

printf("

CFT: ".$row[‘ClientID’]."
CRISSP: ".$row[‘CRISSPID’]."
Resolve: ".$row[‘ResolveID’]."
iCase: ".$row[‘iCaseID’]."
SHIP: “.$row[‘SHIPID’].” ");

The code works and gives the following output:
CFT: 24532
CRISSP: 915107965
Resolve: GM2/yyy/zzzz
iCase:
SHIP:

In this case, the client does not have a iCase number or a SHIP number. I want to make this statement conditional so that it only displays a line for the number if the field is not null or empty. So I want it to look like this:
CFT: 24532
CRISSP: 915107965
Resolve: GM2/yyy/zzzz

Is there an easy way to do this?
Thanks!

Just cut in three or four pieces and add an if statement:

printf("
    CFT: ".$row[‘ClientID’]."
    CRISSP: ".$row[‘CRISSPID’]."
    Resolve: ".$row[‘ResolveID’]
);

if(!empty($row[‘iCaseID’]) {
    printf("\niCase: ".$row[‘iCaseID’]);
}
if(!empty($row[‘SHIPID’]) {
    printf("\nSHIP: ".$row[‘SHIPID’]);
}

notes:

  1. “\n” is used for a newline
  2. Don’t use empty() if you do not want to display textual zeros (“0”). You could use if(strlen($var) == 0)
  3. Be aware that you don’t have values with only spaces. To remove spaces you can use the trim() function. (You should do that before you store values to the database)

Thanks but unfortunately that didn’t work. You see I need all of these values in the same cell, but on separate lines, which is why the row begins with printf("

. I also tried using /n for new line instead, but it didn’t work, probably because I’m only using PHP version5. Any other ideas?

The point of printf() is to format output, with format specifiers. Since you are not doing that, this is just unnecessary clutter. Outputting br tags doesn’t require anything special. The \n only affects the html source code, unless you are also applying nl2br(), so unless you have a lot of html that you want formatted when you do a ‘view source’ of the page in your browser, this is unnecessary too.

Lost among all the clutter in posted reply are some missing closing ), which should result in a php syntax error.

You should use the simplest code that accomplishes a task. Just use an echo statement for the first three lines and use two conditional statements to echo the last two lines.

1 Like

I have added the closing brackets

Hi phdr, Are you able to provide an example of what you mean about using the echo statements and the conditional statements? Thanks!

Hi all,
I figured it out. Here is what the code needs to be to do what I wanted:

printf("

CFT: ".$row[‘ClientID’]);
if(!empty($row[‘CRISSPID’])) {printf("
CRISSP: ".$row[‘CRISSPID’]);}
if(!empty($row[‘ResolveID’])) {printf("
Resolve: ".$row[‘ResolveID’]);}
if(!empty($row[‘iCaseID’])) {printf("
iCase: ".$row[‘iCaseID’]);}
if(!empty($row[‘SHIPID’])) {printf("
SHIP: “.$row[‘SHIPID’]);}
printf(” ");

Thank you frankbeen and phdr for your help.

Just posting my final code after further testing to close this off if anyone else is interested. Neither !empty or isset worked how I wanted because one would ignore NULL and the other would ignore empty fields not set as NULL. The best solution ended up being using strlen. So here is my final code that worked for my situation:

printf("

CFT: ".$row[‘ClientID’]);
if(strlen($row[‘CRISSPID’]) >1) {printf("
CRISSP: ".$row[‘CRISSPID’]);}
if(strlen($row[‘ResolveID’]) >1) {printf("
Resolve: ".$row[‘ResolveID’]);}
if(strlen($row[‘iCaseID’]) >1) {printf("
iCase: ".$row[‘iCaseID’]);}
if(strlen($row[‘SHIPID’]) >1) {printf("
SHIP: “.$row[‘SHIPID’]);}
printf(” ");

This assumes that the codes entered into the fields that I am reporting are more than 2 characters long, which they are.
Again, thank you to those that contributed, you set me on the right path and I got there eventually.
Adrian.

I would recommend to use a more data-driven approach, like putting the key/values in an array, using array_filter to skip all empty fields and show that stuff via template.

Sponsor our Newsletter | Privacy Policy | Terms of Service