Jump to content

How do I get a php variable into a table?


elentz
 Share

Recommended Posts

I have a php page that gets data from several txt files.  I want to take those variables and put them into a table.  I don't quite know how to make it work. 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="refresh" CONTENT="15" />
    <title>Lentz Abode</title>
</head>

<?php
//File to read
$file = '/sys/bus/w1/devices/28-000004d139da/w1_slave';

//Read the file line by line
$lines = file($file);

//Get the temp from second line 
$temp = explode('=', $lines[1]);

//Setup some nice formatting (i.e. 21,3)
$temp = number_format($temp[1] / 1000, 2, '.', '');
$temp_f = number_format(($temp * 9.0 / 5.0 + 32.0),2,'.','');
$fahrenheit=$temp*9/5+32;
$temp1=number_format($fahrenheit,1);

print"<h4>The Kitchen Temp is: $temp1</h4>";

//*************BME280 code ********************
//Barometric PressureBME280
$pressure=file_get_contents("/var/www/html/BME280/pressure.txt");
$press=$pressure*0.0002952998751;
$p=number_format($press,2);

//Humidity BME280
$humidity=file_get_contents("/var/www/html/BME280/humidity.txt");
$hum=number_format($humidity/1000,2);

//Temperature BME280
$temp2=file_get_contents("/var/www/html/BME280/temp.txt");
$t=number_format(($temp2 /1000 * 9.0 / 5.0 + 32.0),1,'.','');


print "<h4>The Barometric Pressure is: $p</h4>";
print "<h4>The Humidity is: $hum%</h4>";
print "<h4>The Temperature is: $t</h4>";

?>
<body>
<style>
table, th, td {
    border: 1px solid black;
}
</style>
<table width="30%" <tr>
    <th>Temp</th>
    <th>Pressure</th>
    <th>Humidity</th>
  </tr>
  <tr>
    <th>$t</th>
    <th>$p</th>
    <th>%hum</th>
  </tr>
  </table>

</body>
</html>

Link to comment
Share on other sites

your opening <table ... tag is broken, so either all the markup is being considered as being inside that tag and isn't being rendered when it is sent to the browser or the markup is being rendered, but not as a table.

 

rather than to just tell us something didn't work, tell us what result you are getting and if it's not obvious what is wrong with that result, tell us what result you expected.

Link to comment
Share on other sites

Here is what I have now.  I think I fixed the broken tag.  The table renders with 3 cols and 2 rows with borders all around.

<body>
<style>
table, th, td {
    border: 1px solid black;
}
</style>
<table>
<tr>
    <th>Temp</th>
    <th>Pressure</th>
    <th>Humidity</th>
  </tr>
  <tr>
    <th>?php echo $t; ?</th>
    <th>?php echo $p; ?</th>
    <th>?php echo $hum; ?</th>
  </tr>
  </table>

</body>
</html>

I result I get in the 1st col 2nd row  ?php echo $t; ?  the next ?php echo $p; ? and the last ?php echo $hum; ?

The result I expect are the vaiables that print in the PHP code.  I really appreciate the help

Link to comment
Share on other sites

You are using php in your table elements currently.  Not the greatest way but you are.
 
Try writing your code and reading it.  YOu are outputting the table column headers twice here.  You are moving in and out of php mode without using the proper tags.  Get yourself some php reference material (the online php manual is excellent if you haven't discovered it yet) and read up on what a php tag looks like. 
 
Ex.

//(in php mode already)
echo "<tr>";
echo "<td>$t</td>";
echo "<td>$p</td>";
echo "<td>$hum</td>";
echo "</tr>";

 

This is the easy way but there are other ways too.  Thought this would be clearer for your untrained eyes.

Link to comment
Share on other sites

I got netbeans and used it to make the page, and (After the light went on)and I realized what benanamen was saying, voila it all came together and works the way I want it to.  Thanks everyone for the patience and nudging in the direction I needed to go.

Link to comment
Share on other sites

I use something called HTML-Kit Tools. Great editor - colorizes different things so that you can easily recognize a typo/mistake. Connects to your server to do uploads easily, has browser preview windows as well. Very inexpensive. Has a trial version too I think. Google it.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.