Jump to content
ParkerShannon

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

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 . . .

Share this post


Link to post
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>

 

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...