在PHP中使用多个变量时如何更改href?

So I didn't found what I was looking for in last two days.

To be clear I have a table "tracks" in my database. So I can show from "tracks" the "demo" field wich is an audio file "url"..

 <?= $tracks['demo']; ?>

I have multiple url's but then I need to replace the a href

<a href="<?php echo $tracks['demo'];?>" 

in a logical way.

In simple terms it's like I want to click on button 1 that loads url 1 into this a href with the ID, title field from that track.

When you press button 2 you need to load another url and replace url 1 with url2.

I tried many ways including javascript but it won't work.

Problem now is that when I list 4 items it always loads the latest "URL" i have posted in my source code. :)

When I echo out <?php echo $tracks['demo]; ?> on all items I can see the correct url's so the functionality to replace the url is my issue.

* I basically want to load many mp3 url's in a player I made in the footer of my website. It works by hardcoding the urls in the url field but this is not what it should be... *

Code:

<?php while($tracks = mysqli_fetch_assoc($muziek)) : ?>

   <div class="col-md-2 viny" id="muziek">
        <h4><?= $tracks['title']; ?></h4>
        <img src="<?= $tracks['img']; ?>" 
         alt=" <?= $tracks['title']; ?> />
        <p class="artist">Artist: <?= $tracks['artist']; ?></p>
    </div>
    <?= $tracks['demo'] = $audiourl ; ?>
    <button type="button" onclick="play">Play</button>

</div>
  <?php endwhile; ?>

*THIS LOOPS PERFECT FOR ALL ITEMS TRACKS ARE LISTED PERFECT *

In my player I have

<a href="<?php echo $audiourl;?>" ><?= $tracks['artist']; ?></b> - <?= $tracks['title']; ?></a> 

function play(){

I'm not good at JS... And this is my issue because it now loads the latest output from the php loop... }

I don't know how you are replacing it but

Why not pass the audio in the function. Here like this

 <button type="button" onclick="play(<?= $tracks['demo'] = $audiourl ; ?>)">Play</button>

assign a id to anchor tag

<a id="someId" href="<?php echo $audiourl;?>" ><?= $tracks['artist']; ?></b> - <?= $tracks['title']; ?></a> 

and in the play function, you receive it like this

function play(audioUrl){

}

and change the href like this in the play function

If you are using jquery

$('#someId').attr("href", audioUrl);

sometimes the above does not works. Can't remember why but if that does not works try this

$('#someId').href = audioUrl

If you are using javascript

document.getElementById('abcd').href = audioUrl

So I tried many things but I had many issues with the player it's js file using the same id's and so on.

The new logic I tried out seems to work:

 <form action="" method="POST">
          <input type="submit" value="Play" name="Play">
          </form>


          <?php 

             if (isset($_POST["Play"]))
             {

              echo $tracks['demo'];
          }else{
            echo '&nbsp;';
          }
          ?>

There is more going on but I tried to write it down as simple as this to show the logic. All Track url's are hidden and when the page loads there is an empty $audiourl by default loaded in the a href from my audioplayer. By clicking the button play I show the url in the source but not on the site. Then the latest $audiourl variable changes to this value.