简体   繁体   English

如何在表格中显示变量

[英]How to display variables into table

Just looking for some help with how to display the variables from the foreach statement in a table.只是在寻找有关如何在表中显示 foreach 语句中的变量的帮助。

So far I have been able to print the results to the page.到目前为止,我已经能够将结果打印到页面上。 However I am trying to get all the results in a HTML table, in order to add searching and etc. any help would be appreciated.但是,我试图在 HTML 表中获取所有结果,以便添加搜索等。任何帮助将不胜感激。 Ive tried echoing each variable within each else if statement to no success.我试过在 else if 语句中回显每个变量,但没有成功。

This is the code so far:这是到目前为止的代码:

  <!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>

any help would be great!任何帮助都会很棒!

Something like this:像这样的东西:

  <!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>

If you want to have functionality like search, pagination, no of rows in each page automatically then use datatables here is how you can print you data into tables如果你想有一个像搜索,分页功能,无需在每个页面的行自动然后使用数据表这里是你可以打印你的数据到表

<!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>

You can make rows columns according to you as I put your one loop into one array, you can break them different rows as per your requirements当我将一个循环放入一个数组时,您可以根据您制作行列,您可以根据您的要求将它们分成不同的行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM