0
for (let index = 0; index < <? php echo $w_hours?>; index++) {
      table += "<td>" + '<? php $a=0; echo $tab [0]->sch [0] [index] ;>' +"</td>";
}

How to use index here, I have also tried using $a=0 and here global $a and then instead of using index I used $a++, but still it doesn't increment.

  • i didn't see any php code in you question, what i see is `for` loop to append into string named `table` using **javascript** – GNassro Aug 15 '20 at 05:58
  • please explain more what you want to do with this code, are you want to generate a javascript code using php, or what ? – GNassro Aug 15 '20 at 06:06
  • actually i am working on a huge php project just i am using one function to generate a table using js (table
    )
    – GetIntoReality Aug 15 '20 at 06:12
  • actually all work has been done i just have to create a table using javaScript like that thats why i have to use js just for a function – GetIntoReality Aug 15 '20 at 06:14
  • So the file from which you copied this code is php? – GNassro Aug 15 '20 at 06:18
  • $tab [0]->sch[a][b] is a multi dimensional table generated using php and filled i just have to get a and b changed with javaScript within for loop – GetIntoReality Aug 15 '20 at 06:19
  • you can't do that, PHP and JavaScript are deffirent programming language, they cannot execute with each other https://stackoverflow.com/a/6369454/8010101 – GNassro Aug 15 '20 at 06:29

2 Answers2

0

I think you are trying to render a table using Javascript for the data that is available in PHP script.

What you need to do is assign the data from PHP to Javascript and then Javascript will be able to use this data to generate the table.

If the data is JSON encodable you can have PHP transfer data to a Javascript variable using:

var dataString = '<?php echo json_encode($tab); ?>';
var tab = JSON.parse(dataString);

var tableRows = '';

tab.forEach((row, index) => {
    //  You can console.log(row) here to check the structure of data in row
    //  and accordingly modify the next line to get desired output
    tableRows = tableRows + '<tr><td>' + row['sch'][0][index] + '</td></tr>';
});
0

js run on browser and PHP runs on server so obviously, their loop does not have to do anything with each other.

ideally you would get json object from php to js and then js iterate over it.

so it should be like this

var x = '<?php echo json_encode( $tab[0]->sch[0]);?>';
var w_hours = parseInt('<?php echo $w_hours?>');

Parse it

var rows = JSON.parse(x);  // https://www.w3schools.com/js/js_json_parse.asp

Now you should be able to use for loop on it.

for (let index = 0; index < w_hours ; index++) {
      table += "<td>" + rows[index] + "</td>";
}
Jitesh Dhamaniya
  • 1,326
  • 2
  • 8
  • 17