如何将变量显示为 table

How to display variables into table

只是寻求一些关于如何在 table.

中显示来自 foreach 语句的变量的帮助

到目前为止,我已经能够将结果打印到页面上了。但是,我试图在 HTML table 中获取所有结果,以便添加搜索等。如有任何帮助,我们将不胜感激。如果语句不成功,我尝试在每个其他变量中回显每个变量。

这是目前的代码:

  <!DOCTYPE html>
<html>
    <head>
        
    </head>
<body>
<?php
// Start the session
session_start();
?>   


<?php
  $curl_handle=curl_init();
  curl_setopt($curl_handle,CURLOPT_URL,'https://services-ap1.arcgis.com/YQyt7djuXN7rQyg4/arcgis/rest/services/Historical_ParksList_2017/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json');
  curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
  curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
  $buffer = curl_exec($curl_handle);
  curl_close($curl_handle);
    
  if (empty($buffer)){
      print "We still do not know what da dog doin";
  }
  else{
     $json=json_decode($buffer, true);
     $info = $json["features"];
     $imposter = count($info);
     print "GREAT SUCCSESS monke brain work". "<br>" . "Results found: " . $imposter . "<br>" . '<hr style="height: 2px;">';
     foreach ($info as $dastuff){
             if(empty($dastuff["attributes"]["name"])){
                 $parkname= "Unavailable";
                 $_SESSION["Parkname"] = $parkname;
                 
            }
            else{
                $parkname = $dastuff["attributes"]["name"];
                $_SESSION["Parkname"] = $parkname;
                 echo '<td><?php echo "$_SESSION[Parkname]"</td>';
            }
             if(empty($dastuff["attributes"]["location"])){
                 $location= "Unavailable";
                 $_SESSION["Location"] = $location;
             
            }
            else{
                $location = $dastuff["attributes"]["location"];
                $_SESSION["Location"] = $location;
            }
             if(empty($dastuff["attributes"]["suburb"])){
                 $suburb= "Unavailable";
                 $_SESSION["Suburb"] = $suburb;
             
            }
            else{
                $suburb = $dastuff["attributes"]["suburb"];
                $_SESSION["Suburb"] = $suburb;
            }
             if(empty($dastuff["attributes"]["facilities"])){
                 $things= "Unavailable";
                 $_SESSION["Things"] = $things;
             
            }
            else{
                $things = $dastuff["attributes"]["facilities"];
                $_SESSION["Things"] = $things;
            }
            echo "Name:" . " " . $parkname . "<br>" . "Suburb:" . " " .  $suburb .  "<br>" . "Location:" . " " .  $location  . "<br>" . "Faccilities:" . " " .  $things .'<br><hr style="width:50%;text-align:left;margin-left:0">';
          
     }
     
  }
    
  
?>

</body>
</html>

任何帮助都会很棒!

像这样:

  <!DOCTYPE html>
<html>
    <head>
        
    </head>
<body>
  <table>
    <tr>
      <th>Name</th><th>Suburb</th><th>Location</th><th>Faccilities</th>
    </tr>
<?php
// Start the session
session_start();
?>   


<?php
  $curl_handle=curl_init();
  curl_setopt($curl_handle,CURLOPT_URL,'https://services-ap1.arcgis.com/YQyt7djuXN7rQyg4/arcgis/rest/services/Historical_ParksList_2017/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json');
  curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
  curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
  $buffer = curl_exec($curl_handle);
  curl_close($curl_handle);
  if (empty($buffer)){
      print "We still do not know what da dog doin";
  }
  else{
     $json=json_decode($buffer, true);
     $info = $json["features"];
     $imposter = count($info);
     print "GREAT SUCCSESS monke brain work". "<br>" . "Results found: " . $imposter . "<br>" . '<hr style="height: 2px;">';
     foreach ($info as $dastuff){
             if(empty($dastuff["attributes"]["name"])){
                 $parkname= "Unavailable";
                 $_SESSION["Parkname"] = $parkname;
                 
            }
            else{
                $parkname = $dastuff["attributes"]["name"];
                $_SESSION["Parkname"] = $parkname;
                 echo '<td><?php echo "$_SESSION[Parkname]"</td>';
            }
             if(empty($dastuff["attributes"]["location"])){
                 $location= "Unavailable";
                 $_SESSION["Location"] = $location;
             
            }
            else{
                $location = $dastuff["attributes"]["location"];
                $_SESSION["Location"] = $location;
            }
             if(empty($dastuff["attributes"]["suburb"])){
                 $suburb= "Unavailable";
                 $_SESSION["Suburb"] = $suburb;
             
            }
            else{
                $suburb = $dastuff["attributes"]["suburb"];
                $_SESSION["Suburb"] = $suburb;
            }
             if(empty($dastuff["attributes"]["facilities"])){
                 $things= "Unavailable";
                 $_SESSION["Things"] = $things;
             
            }
            else{
                $things = $dastuff["attributes"]["facilities"];
                $_SESSION["Things"] = $things;
            }
            echo "<tr><td>" . $parkname . "</td><td>" .  $suburb .  "</td><td>" .  $location  . "</td><td>" .  $things .'</td></tr>';
          
     }
     
  }
    
  
?>
  </table>
</body>
</html>

如果您希望自动拥有搜索、分页、每页行数等功能,请使用 datatables 这是将数据打印到表格中的方法

<!DOCTYPE html>
<html>
    <head>
        
    </head>
<body>
<?php
// Start the session
session_start();
?>   
<table border="1" style="width:100%">

<?php
  $curl_handle=curl_init();
  curl_setopt($curl_handle,CURLOPT_URL,'https://services-ap1.arcgis.com/YQyt7djuXN7rQyg4/arcgis/rest/services/Historical_ParksList_2017/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json');
  curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
  curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
  $buffer = curl_exec($curl_handle);
  curl_close($curl_handle);
    
  if (empty($buffer)){
      print "<tr><td>We still do not know what da dog doin</td></tr>";
  }
  else{
     $json=json_decode($buffer, true);
     $info = $json["features"];
     $imposter = count($info);
     print "GREAT SUCCSESS monke brain work". "<br>" . "Results found: " . $imposter . "<br>" . '<hr style="height: 2px;">';
     foreach ($info as $dastuff){
             if(empty($dastuff["attributes"]["name"])){
                 $parkname= "Unavailable";
                 $_SESSION["Parkname"] = $parkname;
                 
            }
            else{
                $parkname = $dastuff["attributes"]["name"];
                $_SESSION["Parkname"] = $parkname;
                 echo '<td><?php echo "$_SESSION[Parkname]"</td>';
            }
             if(empty($dastuff["attributes"]["location"])){
                 $location= "Unavailable";
                 $_SESSION["Location"] = $location;
             
            }
            else{
                $location = $dastuff["attributes"]["location"];
                $_SESSION["Location"] = $location;
            }
             if(empty($dastuff["attributes"]["suburb"])){
                 $suburb= "Unavailable";
                 $_SESSION["Suburb"] = $suburb;
             
            }
            else{
                $suburb = $dastuff["attributes"]["suburb"];
                $_SESSION["Suburb"] = $suburb;
            }
             if(empty($dastuff["attributes"]["facilities"])){
                 $things= "Unavailable";
                 $_SESSION["Things"] = $things;
             
            }
            else{
                $things = $dastuff["attributes"]["facilities"];
                $_SESSION["Things"] = $things;
            }
            echo "<tr><th>Name</th>" . "<td>" . $parkname . "<td><th>Suburb</th>" . "<td>" .  $suburb .  "</td>" . "<th>Location</th>" . "<td>" .  $location  . "</td>" . "<th>Faccilities</th>" . "<td>" .  $things ."</td></tr>";
          
     }
     
  }
    
  
?>
</table>
</body>
</html>

你可以根据你的行列,因为我把你的一个循环放在一个数组中,你可以根据你的要求将它们分成不同的行