phpjsonvariablesshow-hideisset

Is it possible to display only one JSON object in a table based on a PHP $variable?


I have a table that displays every object from an array using foreach(). I am wanting to have a search bar that enables the table to be filtered, displaying only one object and it's information. I have achieved this using this system for each key:

$searchname = $_SESSION["searchname"];
if($contents["attributes"]["Description"] == $searchname) {
    $description = $contents["attributes"]["Description"];
} 
else {
    $description = '';
}

However, this then leaves every other row empty, but still being printed as a row due to else {''}.

I've tried hiding every 'empty' row with some JavaScript queries from this question,

deleteEmptyRows();
function checkIfCellsAreEmpty(row) {
  var cells =  row.cells;
  var isCellEmpty = false;
  for(var j = 0; j < cells.length; j++) {
    if(cells[j].innerHTML !== '') {
      return isCellEmpty;
    }
  }
  return !isCellEmpty;
}
function deleteEmptyRows() {
  var myTable = document.getElementById("myTable");
  for(var i = 0; i < myTable.rows.length; i++) {
    var isRowEmpty = checkIfCellsAreEmpty(myTable.rows[i]);
    if (isRowEmpty) {
     myTable.rows[i].style.display = "none";
    }
  }
}

but that doesn't seem to work either, with every row still being displayed.

if(isset($_POST['submit_search']) && !empty($_POST['submit_search'])) {
        $_SESSION["searchname"] = $_POST['submit_search'];
    }
$json = json_decode($buffer, true);
     $info = $json["features"];
     
     ?><div><table id="myTable"><tr><th>Beach Name</th><th>Patrolled by</th><th>Patrol freq</th><th>Updated</th></div><?php
     foreach ($info as $contents){
            $searchname = $_SESSION["searchname"];
            if($contents["attributes"]["Description"] == $searchname) {
                $description = $contents["attributes"]["Description"];
            } 
            else {
                $description = '';
            }
            if($contents["attributes"]["Description"] == $searchname) {
                $patrolledby = $contents["attributes"]["PatrolledBy"];
            } 
            else {
                $patrolledby = '';
            }
            if($contents["attributes"]["Description"] == $searchname) {
                $mil = $contents["attributes"]["last_edited_date"];
                $seconds = $mil / 1000;
                $date = date("d/m/Y", $seconds);
            } 
            else {
                $date = '';
            }
            if($contents["attributes"]["Description"] == $searchname) {
                $patrolfreq = $contents["attributes"]["PatrolFrequency"];
            } 
            else {
                $patrolfreq = '';
            }
           
            print("<table>");
            printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>", $description, $patrolledby, $patrolfreq, $date);
            print("</table>");
        }
    }

Now after searching for the beach 'Mooloolaba Beach', I'm left with something like:

Beach Name Patrolled by
Mooloolaba Beach Council
Mooloolaba Beach QLD Surf Life Saving

I've also tried isset() and !empty() in the if() query, but it seems as though isset() does not work with the '==' operator.

Is there anyway to hide these empty rows or just preventing them from printing in the first place?


Solution

  • I suggest you to just have one IF condition since it's the same in every case and then put all your variables assignations in it. Then you also insert the printf in this IF condition and both print for the table outside of the loop to avoid creating a table for every iteration.

     print("<table>");
     foreach ($info as $contents){
            $searchname = $_SESSION["searchname"];
            if($contents["attributes"]["Description"] == $searchname) {
                $description = $contents["attributes"]["Description"];
                $patrolledby = $contents["attributes"]["PatrolledBy"];
                $mil = $contents["attributes"]["last_edited_date"];
                $seconds = $mil / 1000;
                $date = date("d/m/Y", $seconds);
                $patrolfreq = $contents["attributes"]["PatrolFrequency"];
                printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>", $description, $patrolledby, $patrolfreq, $date);           
            } 
        }
     print("</table>");