-1

I'm new in this language. I'm working on a website. I use HTML file like this:

<html lang="en">
...
<div class="" id="temperatura" name="temp">
        <?php require 'php/staticsTemp.php'; ?>
        <h3 class="centered">Temperature</h3>
            <hr> 
        <br>
            <table class="tg" border="5">
                <tr>
                    <th class="tg-031e">Temperature ºC</th>
                    <th class="tg-031e">Date & Time</th>
                </tr>
                <tr>
                    <td class="tg-031e">33</td>
                    <td class="tg-031e">44</td>

                </tr>
            </table>
       </div>
  ...
</html>

And I want to substitute the value 33 and 44 in the table to values that are inside the PHP file. My PHP looks like this:

<?php
include("ligacaobd.php"); 
$sql="SELECT * FROM Valores ORDER BY Momento DESC LIMIT 20";
$result = mysql_query($sql, $ligacaobd) or die(mysql_error());
$rowValor = mysql_fetch_assoc($result);
do
{
    $data[date('d/m/Y H:i:s', $rowValor['Momento'])]=$rowValor['Temperatura'];
}
while ($rowValor= mysql_fetch_assoc($result)); 
?>

Any thoughts? I tried with function POST, but in HTML doesn't work.

3 Answers3

1

You must convert your html in .php as it is and include this php code, either directly in the page or via another php page.

You will then be able to manipulate your variables and do something such as:

<td class="tg-031e"><?php echo $myVariable1; ?></td>
<td class="tg-031e"><?php echo $myVariable2; ?></td>
user1835565
  • 141
  • 1
  • 8
  • This is the best way to do it? – user3763489 Jul 02 '14 at 15:35
  • I don't know what you mean by best. One sure thing, is that in order for your server to know that he has to generate a html page with replaced variables in it, it must be a .php And I recommend you to follow the tips given above on sql queries. – user1835565 Jul 02 '14 at 15:36
0

I'm not quite certain what you mean, but I will try to help you.

First of all don't* use mysql functions anymore. These functions are no longer maintained. Use **mysqli or PDO instead.

If you want to show variables in a HTML document you can do the following thing.

<tr>
    <td class="tg-031e"><?php echo $var1; ?></td>
    <td class="tg-031e"><?php echo $var2; ?></td>
</tr>

Also I would recommend you separating your HTML files from the PHP or at least place your PHP code at the top of your document. For example:

<?php 
    $var1 = 'Example Variable';
?>
<html lang="en">
<head>
    <title>Example</title>
</head>
<body>
<?php echo $var1; ?>
</body>
<html>

Yet the best practice is separating HTML from PHP.

Since you are new to the PHP language, I have some great tutorials for you. Take a look at http://www.w3schools.com. They have some basic PHP tutorials for you to start with.

Good luck.

Melvin Koopmans
  • 2,994
  • 2
  • 25
  • 33
  • w3schools often have out of date and incorrect information, a much better place to learn the workings of PHP is their own manual - http://www.php.net/manual/en/index.php – Eeji Jul 05 '14 at 12:57
  • For the real basics (if else etc.) W3schools is fine but youre right the official documentation is way better – Melvin Koopmans Jul 05 '14 at 12:59
0

Don't use mysql_* functions anymore! They are deprecated in PHP 5.5 and should be removed in 5.6. Use PDO instead.

Not tested yet, but should work. You just need to change your <table /> code to this:

<table class="tg" border="5">
    <tr>
        <th class="tg-031e">Temperature ºC</th>
        <th class="tg-031e">Date & Time</th>
    </tr>
<?php foreach ($data as $datetime => $temperature): ?>
    <tr>
        <td class="tg-031e"><?php echo $temperature; ?></td>
        <td class="tg-031e"><?php echo $datetime; ?></td>
    </tr>
<?php endforeach; ?>
</table>

Hope it helps :)

Community
  • 1
  • 1
Ian Mustafa
  • 598
  • 4
  • 18