0

I am trying to create a table and give values to its rows but it is not working. Any hint or advice will be appreciated:

<!DOCTYPE html>
<html>

<head>
<style>
table,th,td
{
border:1px solid black;
border-collapse:collapse;
}
th,td
{
padding:5px;
}
</style>
</head>
<?php
    $subject = "ISIT307";
    $location = "3.123";
    $time = "4:30";

<table style="width:300px">
<tr>
  <th>Subject</th>
  <th>location</th> 
  <th>time</th> 
</tr>
<tr>
  <td>$subject</td>
  <td>$location</td> 
  <td>$time</td>
</tr>
</table>
?>
</body>
</html>

3 Answers 3

2

Try the following:

<tr>
  <td><?php echo $subject; ?></td>
  <td><?php echo $location; ?></td> 
  <td><?php echo $time; ?></td>
</tr>

You need to use <?php and ?> whenever you would like to run php code.

Sign up to request clarification or add additional context in comments.

Comments

0

You have a syntax error after $time = "4:30"; You can't output html directly through php else you call echo or print.

<!DOCTYPE html>
<html>

<head>
<style>
table,th,td
{
border:1px solid black;
border-collapse:collapse;
}
th,td
{
padding:5px;
}
</style>
</head>
<?php
    $subject = "ISIT307";
    $location = "3.123";
    $time = "4:30";
?>

<table style="width:300px">
<tr>
  <th>Subject</th>
  <th>location</th> 
  <th>time</th> 
</tr>
<tr>
  <td><?= $subject ?></td>
  <td><?= $location ?></td> 
  <td><?= $time ?></td>
</tr>
</table>
</body>
</html>

1 Comment

I just copy paste this but the table is empty!
0

You can't have html in your php, and you can't have php in your html.

<?php
    $subject = "ISIT307";
    $location = "3.123";
    $time = "4:30";
?>

<table style="width:300px">
<tr>
  <th>Subject</th>
  <th>location</th> 
  <th>time</th> 
</tr>
<tr>
  <td><?=$subject ?></td>
  <td><?=$location ?></td> 
  <td><?=$time ?></td>
</tr>
</table>

2 Comments

I tried this but it is not showing data in the table :(
Then try combining my answer with Chris'. You probably don't have short_open_tag activated (for some reason). Just use the simple echo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.