如何动态地将SQLSRV结果中的每一行分配给元素并使它们可点击?

huangapple go评论75阅读模式
英文:

How to dynamically assign each row in SQLSRV results to <td> element and make them clickable?

问题

I am very new to JS/PHP/web dev in general so apologies if this is a very basic question, but I'm trying to make each <td> element in a table clickable, and also get the text of that <td> on click.

Right now I have a SQL query that runs when a date input is selected, and returns a column of basketball game IDs that occurred on the date that is selected.

What I'm having trouble with is making each individual <td> clickable when the query populates the table. Currently my code works, but only the first <td> is clickable.

Here is the PHP code I have that gives me the Game IDs for the date selected (apologies for bad formatting)

<table>
  <tbody>
  <tr>                
      <?php 
        while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
      ?>
        <td id='matchuptable' onclick="game_ids()">
            <?php echo $row['GAME_ID']?>
        </td>
      <?php
        }
      ?>
  </tr>
  </tbody>
</table>

Here is my JS function that is called when the <td> is clicked:

<script type="text/javascript" src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
  <script>
    function game_ids()
      {
          var gid = document.getElementById("matchuptable").innerText;
        alert(gid)
      $.post('gamelogs.php',{game_ids_name:gid},function(data){$('#gamelogs_here').html(data)});
  </script>

I am positive there is a much much cleaner way to do this, and any tips/advice on formatting would be much appreciated. Thanks.

英文:

I am very new to JS/PHP/web dev in general so apologies if this is a very basic question, but I'm trying to make each &lt;td&gt; element in a table clickable, and also get the text of that &lt;td&gt; on click.

Right now I have a SQL query that runs when a date input is selected, and returns a column of basketball game IDs that occurred on the date that is selected.

What I'm having trouble with is making each individual &lt;td&gt; clickable when the query populates the table. Currently my code works, but only the first &lt;td&gt; is clickable.

Here is the PHP code I have that gives me the Game IDs for the date selected (apologies for bad formatting)

&lt;table&gt;
  &lt;tbody&gt;
  &lt;tr&gt;                
      &lt;?php 
        while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
      ?&gt;
        &lt;td id=&#39;matchuptable&#39; onclick=&quot;game_ids()&quot;&gt;
            &lt;?php echo $row[&#39;GAME_ID&#39;]?&gt;
        &lt;/td&gt;
      &lt;?php
        }
      ?&gt;
  &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

Here is my JS function that is called when the &lt;td&gt; is clicked:

&lt;script type=text/javascript src=&quot;https://code.jquery.com/jquery-3.7.0.min.js&quot;&gt;&lt;/script&gt;
  &lt;script&gt;
    function game_ids()
      {
          var gid = document.getElementById(&quot;matchuptable&quot;).innerText;
        alert(gid)
      $.post(&#39;gamelogs.php&#39;,{game_ids_name:gid},function(data){$(&#39;#gamelogs_here&#39;).html(data)})};
  &lt;/script&gt;

I am positive there is a much much cleaner way to do this, and any tips/advice on formatting would be much appreciated. Thanks

答案1

得分: 1

id 属性应该是唯一的,我建议您添加一个计数器并将其附加到您的基本 id 上。您还可以向您的 game_ids() 函数添加一个参数,并传递 this(表示当前点击的元素),避免 DOM 查找:

<tr>
    <?php
    $rowCount = 0;
    while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)):
        $rowCount++;
    ?>
        <td id="matchuptable-<?php=$rowCount?>" onclick="game_ids(this)">
            <?php echo $row['GAME_ID']?>
        </td>
    <?php
    endwhile;
    ?>
</tr>

我使用了 while(): - endWhile; 语法以提高可读性。

在 JavaScript 代码中,您将点击的元素作为参数传递:

function game_ids(el)
{
    alert(el.innerText);
    $.post('gamelogs.php',{game_ids_name:gid},function(data){$('#gamelogs_here').html(data)});
}
英文:

The id attribute should be unique, I suggest you to add a counter and append it to your base id. You can also add a parameter to your game_ids() function and pass this (that represent the current element clicked), avoiding a DOM lookup:

&lt;tr&gt;
    &lt;?php
    $rowCount = 0;
    while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)):
        $rowCount++;
    ?&gt;
        &lt;td id=&quot;matchuptable-&lt;?=$rowCount?&gt;&quot; onclick=&quot;game_ids(this)&quot;&gt;
            &lt;?php echo $row[&#39;GAME_ID&#39;]?&gt;
        &lt;/td&gt;
    &lt;?php
    endwhile;
    ?&gt;
&lt;/tr&gt;

I've used the while(): - endWhile; syntax for better readability

In the JS code you get the clicked element as a parameter:

function game_ids(el)
{
    alert(el.innerText);
    $.post(&#39;gamelogs.php&#39;,{game_ids_name:gid},function(data){$(&#39;#gamelogs_here&#39;).html(data)});
}

huangapple
  • 本文由 发表于 2023年7月18日 06:29:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定