Search code examples
phpmysqlnulltrim

PHP Output of NULL/Spaces


I'm trying to output from mysql using php. The SQL query that I'm outputting contains several joins and some of the values are NULL. When I echo the output I get empty spaces for the NULL values. How do I get rid of the empty space? I have line breaks between each value that I want to output, but the NULL values are just placing an empty space. So my output looks a bit like this.

What it looks like

Dave Smith
Subject 1

Subject 3

John Smith
Subject 1
Subject 2

Subject 4

What I'd like it to look like

Dave Smith
Subject 1
Subject 3

John Smith
Subject 1
Subject 2
Subject 4

I have tried a few of the php trim functions, but none seem to work. The relevant code is below.

$resultSet = $db->query ("...my query");  

echo $resultSet -> num_rows;

While($rows = $resultSet ->fetch_assoc())

{
  $FirstName = ($rows['First_Name']);
  $Surname =   ($rows['Surname']);
  $subject1 = ($rows['subject1']);
  $subject2 = ($rows['subject2']);
  $subject3 = ($rows['subject3']);
  $subject4 = ($rows['subject4']);

  $output .= "<p> $FirstName $Surname <br/> Chosen Subjects <br/> $subject1 <br/> $subject2 <br/> 
  $subject3 <br/>  $subject4 </p>";
 }

Solution

  • When I echo the output I get empty spaces for the NULL values

    $subject1 = ($rows['subject1']);
    $subject2 = ($rows['subject2']);
    $subject3 = ($rows['subject3']);
    $subject4 = ($rows['subject4']);
    
      $output .= "<p> $FirstName $Surname <br/> Chosen Subjects <br/> $subject1 <br/> 
      $subject2 <br/>  $subject3 <br/>  $subject4 </p>";
    

    The reason for your empty space is that you are putting in <br/> regardless as to if any PHP value is present.

    Suggested Alternative:

    • Put the values into an array.
    • Clean any falsey / null values from the array
    • implode the array into the final string.

    a) Put the values into an array:

    $subject[1] = $rows['subject1'];
    $subject[2] = $rows['subject2'];
    $subject[3] = $rows['subject3'];
    $subject[4] = $rows['subject4'];
    

    b) Clean out the null values reference

    $subjectOutput = array_filter($subject, 'strlen');
    

    Note: If you don't care about other falsey values such as 0 then you can simply do an even more basic array_filter() call such as = array_filter($subject);.

    c) Implode the array.

    $subjectString = implode($subjectOutput, '<br/>');
    

    Example:

    $subject[1] = "horses";
    $subject[2] = 12;
    $subject[3] = 0;
    $subject[4] = null;
    $subject[5] = "cats";
    
    $subjectOutput = array_filter($subject, 'strlen');
    
    //$subject[1] = "horses";
    //$subject[2] = 12;
    //$subject[3] = 0;
    //$subject[5] = "cats";
    
    /***
     * simply print the sting and voila!
     ***/
    $subjectString = implode($subjectOutput, '<br/>');
    
    // $subjectString = "horses<br/>12<br/>0<br/>cats";
    
     $output .= "<p> $FirstName $Surname <br/> Chosen Subjects <br/> ".$subjectString." </p>";