-1

I have two php files to bring up a database in a particular order, but what I need is for the columns that are empty to be omitted.

the file on the web page is:

   <?php
  if(is_array($fetchData)){      
  $id=1;
  foreach($fetchData as $data){
   
     ?>
      

     <p></p>
  <div><strong>Section:</strong> <?php echo $data['Section']??''; ?></div>
  <div>Lot Number: <?php echo $data['LotNumber']??''; ?></div>
  <div>Last Name: <?php echo $data['LastName']??''; ?></div> 
  <div>First Name: <?php echo $data['FirstName']??''; ?></div>
  <div>Middle Initial: <?php echo $data['MiddleInitial']??''; ?></div>
<div>Maiden Name:<?php echo $data['MaidenName']??''; ?></div>
  <div>Suffix: <?php echo $data['Suffix']??''; ?></div>
  <div>Born: <?php echo $data['Born']??''; ?></div>
  <div>Died: <?php echo $data['Died']??''; ?></div>
  <div>Veteran Info:<?php echo $data['VeteranInfo']??''; ?></div>
  <div>Misc. Info: <?php echo $data['MiscInfo']??''; ?></div>   
 </div></p>
 <?php
  $Section;}}else{ ?>
  <div>
    
<?php echo $fetchData; ?>
</div>
<div>
<?php
}?>
</div>

The second file is:

 $db= $conn;
$tableName="Burials";
$columns= ['id', 'EndowmentNumber','Letter','LotSize','Section', 
'LotNumber','LotOwner','LastName', 'FirstName','MiddleInitial','MaidenName','Suffix', 
'Born','Died','VeteranInfo','MiscInfo'];
$fetchData = fetch_data($db, $tableName, $columns);
function fetch_data($db, $tableName, $columns){
 if(empty($db)){
  $msg= "Database connection error";
 }elseif (empty($columns) || !is_array($columns)) {
  $msg="columns Name must be defined in an indexed array";
 }elseif(empty($tableName)){
  $msg= "Table Name is empty";
 }else{
$columnName = implode(", ", $columns);
$query = "SELECT ".$columnName." FROM $tableName"." ORDER BY Section, LotNumber";
$result = $db->query($query);
if($result== true){ 
 if ($result->num_rows > 0) {
$row= mysqli_fetch_all($result, MYSQLI_ASSOC);
$msg= $row;

 } else {
    $msg= "No Data Found"; 
 }
}else{
  $msg= mysqli_error($db);
}
}
return $msg;
}
?>

The database comes up ok, but shows everything. I reviewed multiple queries on this type of problem and tried to implement some of the suggestions, but all just throw 500 errors. What do I need to do?

I have tried to insert the following codes as suggested from a previous post:

 if($data['VeteranInfo'] != '' || $data['VeteranInfo'] != NULL) {
 echo "<div id='VeteranInfo'>";
 echo $data['VeteranInfo'];
 echo "</div>";   

}

Another code I have tried:

if(isset($data['VeteranInfo']) && $data['VeteranInfo'] != '' && 
$data['VeteranInfo'] != NULL)

I have also tried:

if (array_key_exists("VeteranInfo", $data) && 
!empty($data["VeteranInfo"])) 
{
 // do something
}

I've tried placing these codes in both php files, but have gotten nowhere. Clearly I am doing something wrong somewhere.

273K
  • 29,503
  • 10
  • 41
  • 64
fiddler
  • 1
  • 1

1 Answers1

0

Ok, I solved my issue. I needed to add code to the second file. The code added is:

     $query = "SELECT ".$columnName." FROM $tableName WHERE  
     VeteranInfo is not null AND VeteranInfo != ''"."
     ORDER BY Section, LotNumber, VeteranInfo"

I found the correct code in stackoverflow archives

Raymond A
  • 763
  • 1
  • 12
  • 29
fiddler
  • 1
  • 1