Jump to content

How do I express the value of variables in table cells?


ParkerShannon

Recommended Posts

How do I get the value of a variable to display in an HTML table:

    <tr>
      <td>Anarchist</td>
      <td>$anarchistyes</td>
      <td>$anarchistno</td>
      <td>$anarchisthuh</td>
    </tr>
    <tr>
      <td>Conservative</td>
      <td>$conservativeyes</td>
      <td>$conservativeno</td>
      <td>$conservativehuh</td>
    </tr>

Thank you . . .

Link to comment
Share on other sites

I have no idea where your data is coming from
but if you require a JS solution (not PHP) then you could modify this.

	<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width-device-width,initial-scale=1.0, user-scalable=yes"/>
<title> HTML5 Test Page </title>
<!-- For: http://w3schools.invisionzone.com/topic/60204-how-do-i-express-the-value-of-variables-in-table-cells/ -->
<style>
 table {
    border-collapse: collapse;
    border-spacing: 0;
    border: 1px solid #ddd;
    width: 25%;
 }
 th, td {
    border: 1px solid black;
    padding: 2px;
 }
 tr:nth-child(even){background-color: #d2d2d2; } /* for table stripes */
	</style>
	</head>
<body>
<table id="tbl"></table>
	<script>
 let $anarchistyes = 1,     $anarchistno = 2,     $anarchisthuh = 3,
     $conservativeyes = 4,  $conservativeno = 5,  $conservativehuh = 6;
 let tableArray = [
     ['Anarchist',    $anarchistyes,     $anarchistno,     $anarchisthuh],
     ['Conservative', $conservativeyes , $conservativeno , $conservativehuh ]
    ];
 let str = '';
 for (let i=0; i<tableArray.length; i++) {
   str += '<tr>';
   for (let v of tableArray[i]) { str += `<td> ${v} </td>`; }
   str += '</tr>';
 }
 document.getElementById('tbl').innerHTML = str;
</script>
	</body>
</html>

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...