douxue4395 2017-11-23 04:44
浏览 187
已采纳

如何将<tr>插入<table>(使用PHP)

I am using PHP and I have a table that I want to populate dynamically.

<table>
    <tr>
         <th>This One</th>
         <th>That One</th>
         <th>The Other One</th>
    </tr>
    <tr>
         <td>Final This</td>
         <td>Final That</td>
         <td>Final The Other</td>
   </tr>
</table>

I am populating the table data onchange, and I want the data to be added above the row marked with "Final blah".

Each new row has data to populate each cell.

Is there some functionality that will do this with PHP, mayber leveraging some JavaScript?

EDIT: I want to get a final product that looks like this:

<table>
    <tr>
         <th>This One</th>
         <th>That One</th>
         <th>The Other One</th>
    </tr>
    <tr> Dynamically added row. Cells already populated from database</tr>
    <tr> Next row of data from the database</tr>
    <tr> The next row should **ALWAYS** be the last row</tr>
    <tr>
         <td>Final This</td>
         <td>Final That</td>
         <td>Final The Other</td>
   </tr>
</table>

Notice that there is a HEADER ROW THAT I WANT TO KEEP!
I want to insert rows between the header row and the row with "Final something". The last row IS ALWAYS PRESENT.

  • 写回答

1条回答 默认 最新

  • douli1872 2017-11-23 05:08
    关注

    If i am getting your query :

     <table id="myTable">
      <tr>
         <th>This One</th>
         <th>That One</th>
         <th>The Other One</th>
      </tr>
       <tbody id="myid">
        <tr><td>Final This</td><td>Final That</td><td>This is your Last Row</td></tr>
       </tbody>
        </table>
        <br>
    
    <button onclick="myCreateFunction()">Add row</button>
    
    <script>
    function myCreateFunction() {
        var table = document.getElementById("myid");
        var row = table.insertRow(0);
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        var cell3 = row.insertCell(2);
        cell1.innerHTML = "Final This";
        cell2.innerHTML = "Final That";
        cell3.innerHTML = "I am inserted between First and last row";
    }
    
    </script>
    

    Fiddle

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。