是否可以在基于PHP $变量的表中仅显示一个JSON对象?

eaf3rand  于 2023-08-02  发布在  PHP
关注(0)|答案(1)|浏览(75)

我有一个表,它使用foreach()显示数组中的每个对象。我想有一个搜索栏,使表被过滤,只显示一个对象,它的信息。我已经实现了这一点,使用这个系统的每一个关键:

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

字符串

然而,这会使每隔一行为空,但由于else ''},仍会被打印为一行。

我已经尝试通过this question,的一些JavaScript查询隐藏每个'空'行

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";
    }
  }
}


但这似乎也不起作用,每一行仍然显示。

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>");
        }
    }


现在,在搜索海滩“Mooloolaba海滩”后,我留下了这样的东西:
| 巡逻人员| Patrolled by |
| --| ------------ |
| | |
| | |
| 理事会| Council |
| | |
| | |
| | |
| | |
| QLD冲浪救生| QLD Surf Life Saving |

  • 我还尝试了isset()和!if()查询中的empty(),但似乎isset()不支持'=='运算符。*
    是否有任何方法可以隐藏这些空行,或者只是防止它们首先打印?
qeeaahzv

qeeaahzv1#

我建议你只有一个IF条件,因为它在任何情况下都是一样的,然后把所有的变量赋值都放在里面。然后,您还在IF条件中插入printf,并在循环外部为表插入两个print,以避免为每次迭代创建一个表。

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>");

字符串

相关问题