【问题标题】:Unique button for each row in a table?表格中每一行的唯一按钮?
【发布时间】:2021-05-20 21:55:39
【问题描述】:

我正在尝试创建一个填充了数据库数据的表。表中的每一行都需要在末尾有一个按钮,该按钮有一个标识“艺术家”姓名的查询字符串,并且需要链接到特定于该艺术家的内容。在我当前的表格中,按钮只是重复的,不是单独的,我不知道如何让它们查询它们所属的艺术家。

  <?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);

    

    
    
?> 

我正在使用 jQuery 填充 div,该 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>

【问题讨论】:

  • 您想在哪里查看艺术家的详细信息?在模式中,在新页面中?
  • 艺术家详细信息(名字、姓氏)显示在同一页面的表格中,正如您在上面的代码中看到的那样,当单击按钮时。然后,我需要在每一行的末尾添加一个按钮,该按钮将使用与该艺术家相关的内容填充一个 div(每个艺术家一个 div)。

标签: php


【解决方案1】:

这样的事情应该可以解决问题。将艺术家的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>

【讨论】:

  • 谢谢!我会试试这个,但是,看看它,它似乎总是会填充 div“div1”。我需要填充不同的 div,具体取决于单击了哪个艺术家的按钮(每个艺术家都有自己的 div)。
  • 我已经尝试过了,但它不起作用..当我单击按钮时没有任何反应。我在想脚本中的“ArtistNumber”可能无法识别? &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script&gt; $(document).ready(function(){ $("button").click(function(){ $("#div1").load("demo_test" + ArtistNumber + ".txt"); }); }); &lt;/script&gt;{ echo "&lt;tr&gt;&lt;td&gt;" . $row['FirstName'] . "&lt;/td&gt;&lt;td&gt;" . $row['LastName'] . "&lt;/td&gt;&lt;td&gt;&lt;button id='" . $row['ArtistNumber'] . "'&gt;Get External Content&lt;/button&gt;&lt;/tr&gt;" ;
  • 抱歉,我还添加了var ArtistNumber = $(this).attr('id');,但现在每个按钮都显示一个文本文件,甚至不再存在!
  • 好的,快速重启显然已经清除了缓存,这正在工作!谢谢你。有没有办法让每个艺术家拥有自己的 div,而不是让每个按钮都填充同一个 div?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-20
  • 2022-01-04
  • 2021-09-09
  • 2019-01-23
  • 1970-01-01
相关资源
最近更新 更多