简体   繁体   中英

How to display variables into table

Just looking for some help with how to display the variables from the foreach statement in a table.

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. Ive tried echoing each variable within each else if statement to no success.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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