所以我试图通过增加歌曲的名称和增加的歌曲的id
为相同的按钮创建一个onclick,但我似乎无法在js中获取正确的id
,当我按下所有按钮时,我总是收到第一首歌。我想过尝试将每个保存为cookie,然后使用不同的ids
作为cookie引用按钮,甚至尝试使用$_SESSIONS
,所以我很乐意接受任何建议
<!DOCTYPE HTML>
<?php
session_start();
//connecting to mysql db
include ('Oppkobling.php');
// making an sql query
$query = 'SELECT * FROM Band';
// running the query
$resultat = mysqli_query($db, $query);
// saving results to an variable
$antall = mysqli_num_rows($resultat);
// creating a table
echo('<table class="per">');
echo('<tr><th>Navn</th><th>Land</th><th>Avspillinger</th>
<th>Sjanger</th><th>Logo</th><th id=due>Top sang</th></tr>');
// loop for the table
for ($i=0; $i < $antall; $i++)
{
$rad = mysqli_fetch_array($resultat);
echo('<tr><td>
'.$rad['Band'].'</td><td>
'.$rad['Land'].'</td><td>
'.$rad['Avspillinger'].'</td><td>
'.$rad['Sjanger'].'</td><td>
<img src = "./bilder/'.$rad['Bilde'].' "id=due></td><td>
'//this part is where my audio is implemented
'
<audio src="sang'.$rad['Sang'].'.mp3"." id="ip'.$i.'">
</audio><img src="button.png" onclick="audio.play()" width=
"100px"height="100px"></td></tr>');
}
echo('</table>');
// disconnecting from db
mysqli_close($db);
mysqli_free_result($resultat);
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css.css">
<meta charset="utf-8">
</head>
<body>
<script type="text/javascript">
//I dont know how to solve it at all
var i;
i = -1;
for (i){
var c = "ip"+i;}
var x = document.getElementById(c).src;
var audio = new Audio(x);
audio.onclick = function(){
audio.play();
}
audio.loop = false;
</script>
</body>
</html>