0

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 which is an audio file "URL"..

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

I have multiple URLs 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 URLs so the functionality to replace the URL is my issue.

*** I basically want to load many mp3 URLs 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 PERFECTLY FOR ALL ITEMS TRACKS ARE LISTED PERFECTLY ***

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... }

10
  • 4
    Please add your code. What you have tried ? Commented Sep 4, 2017 at 4:41
  • Please add html+javascript+php code what you tried so far. Commented Sep 4, 2017 at 4:43
  • @frederick are the url being loaded via ajax or server calls ?? Commented Sep 4, 2017 at 4:45
  • Where is your for / foreach loop ? Commented Sep 4, 2017 at 4:45
  • need the complete code without that we cannot help. Sorry Commented Sep 4, 2017 at 4:46

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Hmm interesting. Did not tought about that. Will try ;) Thanks!
So far so good, in my debugger I see the correct url's loading in my function. So I'm almost there. Big Thanks.
you're welcome. If this helped you can you please accept it so that if any other has this issue he or she can also get help :)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.