简体   繁体   English

表中每一行的唯一按钮?

[英]Unique button for each row in a table?

I'm trying to create a table populated with data from a database.我正在尝试创建一个用数据库中的数据填充的表。 Each row in the table needs to have a button on the end which has a query string identifying the "artist"'s name and needs to link to content specific to that individual artist.表中的每一行都需要在末尾有一个按钮,该按钮具有标识“艺术家”姓名的查询字符串,并且需要链接到特定于该艺术家的内容。 With my current table, the buttons are just repeated and are not individual, and I don't know how to make them query the artist they pertain to.在我当前的表格中,按钮只是重复而不是单独的,我不知道如何让它们查询它们所属的艺术家。

  <?php


//Query to get artist data
    $result = mysqli_query($con,"SELECT * FROM artist LIMIT 10");
if (!$result) {
    printf("Error: %s\n", mysqli_error($con));
    exit();
}
   
      
      
      
      //show artist data in table
       echo "<table><th>BadNoise Artists</th>";
 while($row = mysqli_fetch_array($result))
          {
          echo "<tr><td>" . $row['FirstName'] . "</td><td>" . $row['LastName'] . "</td><td><button>Get External Content</button></td></tr>" ;
          }
 echo "</table>";
 mysqli_close($con);

    

    
    
?> 

I'm using jQuery to populate the div which will show the content specific to each artist:我正在使用 jQuery 来填充 div,它将显示特定于每个艺术家的内容:

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script>
$(document).ready(function(){
  $("button").click(function(){
    $("#div1").load("demo_test.txt");
  });
});
</script>

Something like this should do the trick.像这样的事情应该可以解决问题。 Store the artist's ID in the ID field of the button and obtain it via Jquery:将艺术家的 ID 存储在按钮的 ID 字段中,并通过 Jquery 获取:

<?php

//Query to get artist data
$result = mysqli_query($con,"SELECT * FROM artist LIMIT 10");
if (!$result) {
printf("Error: %s\n", mysqli_error($con));
exit();
}

//show artist data in table
echo "<table><th>BadNoise Artists</th>";
while($row = mysqli_fetch_array($result)){
    echo "<tr><td>" . $row['FirstName'] . "</td><td>" . $row['LastName'] . "</td><td><button id='" . $row['artistId'] . "'>Get External Content</button></td></tr>" ;
}
echo "</table>";
mysqli_close($con);

?> 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script>
$(document).ready(function(){
  $("button").click(function(){
    var artistId = $(this).attr('id');
    $("#div1").load("demo_test" + artistId + ".txt");
  });
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM