Dany 7elo
Dany 7elo

Reputation: 73

How to fix the display of the table using PHP and html

I have a code that display the fetched data in a table using foreach loop with if statement to check if the field is empty or not but the problem is that the table is displaying on each row the field name.

How to fix the display? I will appreciate any help I know that the error is in the foreach loop.

each row includes the field name and the value.... take a look after using the answer of Doomenik

enter image description here

code:

 $sql = $wpdb->prepare("select i.siteID
         , i.siteNAME
         , i.equipmentTYPE
         , c.latitude
         , c.longitude
         , c.height 
         , o.ownerNAME
         , o.ownerCONTACT
         , x.companyNAME
         , y.subcontractorCOMPANY
         , y.subcontractorNAME
         , y.subcontractorCONTACT
      from site_info i
      LEFT  
      JOIN owner_info o
        on i.ownerID = o.ownerID
      LEFT  
      JOIN company_info x
        on i.companyID = x.companyID
      LEFT 
      JOIN subcontractor_info y
        on i.subcontractorID = y.subcontractorID
        LEFT JOIN site_coordinates2 c
        on i.siteID=c.siteID 
        where 
        i.siteNAME = %s
        AND 
        o.ownerID = %d
        AND 
        x.companyID = %d
       ",$site_name,$owner_name,$company_name);

     $query_submit =$wpdb->get_results($sql, OBJECT);

    echo "<table class='t1' width='30%'> ";
  echo     "<tr>";
  echo           "<th>Site ID</th>";
  echo           "<th>Site Name</th>";

  if(!empty($query_submit->latitude)) { 
     echo "<th>Lattitude</th>";


  } else { echo '<th></th>'; } 



  if(!empty($query_submit->longitude)){ 
     echo "<th>Longitude</th>";


  } else { echo '<th></th>'; } 

  echo "<th>Owner Name</th>";

  if(!empty($query_submit->ownerCONTACT))
  { 
     echo "<th>Owner Contact</th>";


  } else { echo '<th></th>'; } 
  echo           "<th>Company Name</th>";
  if(!empty($query_submit->equipmentTYPE)){ 
     echo "<th>Equipment Type</th>";


  } else { echo '<th></th>'; } 

  if(!empty($query_submit->height)) { 
     echo "<th>Height</th>";


  } else { echo '<th></th>'; } 

  if(!empty($query_submit->subcontractorCONTACT)){ 
     echo "<th>Sub Contact</th>";


  } else { echo '<th></th>'; } 


  if(!empty($query_submit->subcontractorCOMPANY)) { 
     echo "<th>Sub company Name</th>";
  } else { echo '<th></th>'; } 
  echo   "</tr>";
  foreach ($query_submit as $obj) {
    echo   "<tr>";   
    echo         "<td>".$obj->siteID."</td>";
    echo         "<td>".$obj->siteNAME."</td>";
    if(!empty($obj->latitude)) { 
         echo "<td>".$obj->latitude."</td>";
    } else { echo '<td></td>'; } 


    if(!empty($obj->longitude)) { 
        echo "<td>".$obj->longitude."</td>";
    } else { echo '<td></td>'; } 

    echo "<td>".$obj->ownerNAME."</td>";
    if(!empty($obj->ownerCONTACT)) { 
        echo "<td>".$obj->ownerCONTACT."</td>";
    } else { echo '<td></td>'; } 

    echo "<td>".$obj->companyNAME."</td>";
    if(!empty ($obj->equipmentTYPE)){ 
        echo "<td>".$obj->equipmentTYPE."</td>";
    } else { echo '<td></td>'; } 

    if(!empty($obj->height)){ 
        echo "<td>".$obj->height."</td>";
    } else { echo '<td></td>'; } 

    if(!empty($obj->subcontractorCONTACT)){ 
        echo "<td>".$obj->subcontractorCONTACT."</td>";
    } else { echo '<td></td>'; } 


    if(!empty($obj->subcontractorCOMPANY)){ 
        echo "<td>".$obj->subcontractorCOMPANY."</td>";
    } else { 
        echo '<td></td>'; 
    } 
    echo "</tr>";
}
 echo "</table>";

Upvotes: 1

Views: 765

Answers (3)

Doomenik
Doomenik

Reputation: 866

** Removed cause of new Informations provided *

Basicly I moved the creation of the th in front of the for each, otherwise you would create every loop this line. So this one will create first the "" row and then create inside the for each every data line. You can see directly under the for each, I open a "" and close it at the end of the loop. After everything finished I close the table.

EDIT 3: Here is know the solution for your situation, cause I dont have so much times its a very quick and dirty one. There is much potential to improve it, but this will be up to you

echo "<table class='t1' width='30%'> ";
echo     "<tr>";
echo           "<th>Site ID</th>";
echo           "<th>Site Name</th>";

//First we gonna check which columns contain at least 1 value
$latitude = false;
$longitude = false;
$ownerCONTACT = false;
$equipmentTYPE = false;
$height = false;
$subcontractorCONTACT = false;
$subcontractorCOMPANY = false;

foreach ($query_submit as $header_obj) {
    //If only one cel in the column has an value we put his variable to true
    if(!empty($header_obj->latitude)) {
        $latitude = true;
    }
    if(!empty($header_obj->longitude)) {
        $longitude = true;
    }
    if(!empty($header_obj->ownerCONTACT)) {
        $ownerCONTACT = true;
    }
    if(!empty($header_obj->equipmentTYPE)) {
        $equipmentTYPE = true;
    }
    if(!empty($header_obj->height)) {
        $height = true;
    }
    if(!empty($header_obj->subcontractorCONTACT)) {
        $subcontractorCONTACT = true;
    }
    if(!empty($header_obj->subcontractorCOMPANY)) {
        $subcontractorCOMPANY = true;
    }
}

//Now we check and if there is content we place the th
if($latitude === true) {
    echo "<th>Lattitude</th>";
}

if($longitude === true){
    echo "<th>Longitude</th>";
}

echo "<th>Owner Name</th>";

if($ownerCONTACT === true) {
    echo "<th>Owner Contact</th>";
}

echo "<th>Company Name</th>";

if($equipmentTYPE === true){
    echo "<th>Equipment Type</th>";
}

if($height === true) {
    echo "<th>Height</th>";
}

if($subcontractorCONTACT === true){
    echo "<th>Sub Contact</th>";
}

if($subcontractorCOMPANY === true) {
    echo "<th>Sub company Name</th>";
}
echo "</tr>";

//Here we loop again trough the obj, we write now every row.
//If we had an empty column (so also no head) we will skip that value.
foreach ($query_submit as $obj) {
    echo   "<tr>";
    echo         "<td>".$obj->siteID."</td>";
    echo         "<td>".$obj->siteNAME."</td>";

    if($latitude === true) {
        //In this situation the column exists but we dont know if the specific cell has 
        // a value so we check this here
        if(!empty($obj->latitude)) {
            echo "<td>".$obj->latitude."</td>";
        } else {
            //If it doesnt has one we make at least an empty cell so the layout gets not broken
            echo "<td></td>";
        }
    }

    if($longitude === true){
        if(!empty($obj->longitude)) {
            echo "<td>".$obj->longitude."</td>";
        } else {
            echo "<td></td>";
        }
    }

    echo "<td>".$obj->ownerNAME."</td>";

    if($ownerCONTACT === true) {
        if(!empty($obj->ownerCONTACT)) {
            echo "<td>".$obj->ownerCONTACT."</td>";
        } else {
            echo '<td></td>';
        }
    }

    echo "<td>".$obj->companyNAME."</td>";

    if($equipmentTYPE === true){
        if(!empty ($obj->equipmentTYPE)){
            echo "<td>".$obj->equipmentTYPE."</td>";
        } else {
            echo '<td></td>';
        }
    }

    if($height === true) {
        if(!empty($obj->height)){
            echo "<td>".$obj->height."</td>";
        } else { echo '<td></td>'; }
    }

    if($subcontractorCONTACT === true){
        if(!empty($obj->subcontractorCONTACT)){
            echo "<td>".$obj->subcontractorCONTACT."</td>";
        } else {
            echo '<td></td>';
        }
    }

    if($subcontractorCOMPANY === true) {
        if(!empty($obj->subcontractorCOMPANY)){
            echo "<td>".$obj->subcontractorCOMPANY."</td>";
        } else {
            echo '<td></td>';
        }
    }
    echo "</tr>";
}
echo "</table>";

Upvotes: 3

rahul singh
rahul singh

Reputation: 449

your th tags depends on $obj which can be different each time in loop. so to overcome this what you can do is make the table header static and table row values will be blank or value depending on your $obj variable. so you need to move out you

<table class='t1' width='30%'>
<tr>
<th>Site ID</th>
<th>Site Name</th>
// all th tags
</tr>
 <?php foreach ($query_submit as $obj) { // then foreach 
  echo   "<tr>";   
  echo         "<td>".$obj->siteID."</td>";
  echo         "<td>".$obj->siteNAME."</td>";
   ?><td><?php if(!empty($obj->latitude))
  {  echo $obj->latitude;} ?></tr>  // your td will be fixed contain of td will depend on your value.
  echo "<td>".$obj->siteID."</td>"; // and so on

To get your table html right here is an example of basic html table using foreach loop . hope this will help you.

<table>
<tr>
<th>heading 1</th>
 <th>heading 2</th>
<th>heading 3</th>
<th>heading 4</th>
<th>heading 5</th>
</tr>
<?php foreach($array as $data){ ?>
<tr>
<td><?php if(!empty($data->data1))  { echo $data->data1; } ?></td>
<td><?php if(!empty($data->data2))  { echo $data->data2; } ?></td>
<td><?php if(!empty($data->data3))  { echo $data->data3; } ?></td>
<td><?php if(!empty($data->data4))  { echo $data->data4; } ?></td>
<td><?php if(!empty($data->data5))  { echo $data->data5; } ?></td>
</tr>
 <?php } ?>

Upvotes: 1

moza
moza

Reputation: 1

have a look here https://www.w3schools.com/html/html_tables.asp, the problem lies in that you're iterating the headers in the loop, when they're only supposed to be called one time at the start of the table tag. I.e only the tags should be looped/iterated through and take the header tags out of the for each.

Upvotes: 0

Related Questions