0

Good morning,

I'm just stuck within an issue which I can't follow even in the logical way. Maybe you can help me out by leading me to the right path.

I have a simple button which starts a simple function handing over an id. Right now I do have multiple buttons in a row with different ids

<a class="buttonControl btn btn-default btn-xs" href="javascript:play(999)" id="999">Play</a> <br> <a class="buttonControl btn btn-default btn-xs" href="javascript:play(888)" id="888">Play</a>

Next thing is I have a multidimensional array in php out of a php session.

$arr_a = array(
    "999" => array(
        "start" => "180",       
        "files" => array(
            "mp3" => "http://example.com/999.mp3"           
         )
    )
    "888" => array(
        "start" => "120",       
        "files" => array(
            "mp3" => "http://example.com/888.mp3"           
         )
    )
)

The JS function play(id) starts playing the audio file via the html5 audio object.

function play(x) {
  ...some code... get audio object ...
}

So am I right, when I describe the following:

  • convert the php array with json_encode [ ? ]
  • make JSON output available in js function [ ? ]
  • iterate through the json data until id match with key [ ? ]
  • get the 'mp3' value and hand it over to src audio object [ ? ]
  • play the file

So is this the right way from your point of view?

Thank you so much!

Best Regards Aaren

2 Answers 2

1

use json to change the context from php to javascript. The other steps are all right. But where do you struggle?

function play(x) {
  let jsonData = <?=json_encode($arr_a);?>;
  let mp3 = jsonData[x].files.mp3;
  document.querySelector('audio').src = mp3;
  document.querySelector('audio').play();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Right now I think it was really good to re-think and to write everything down, so I think my struggle was only because I was somehow unsure (lack of coffee maybe) ... however writing it down and think about it while writing it down AND your re-check about my thoughts helped me so far! Thank you!
you're welcome. If you have any further questions, please feel free to ask. If this is the answer to your question, please select it as your prefered answer :)
Would be yours but I have to wait for 8 minutes due to some time restrictions. Thank you! Now I can enjoy my coffee :)
0

If your list can change, you can use a PHP script (audio_list.php, for example) to return the list as a JSON, and use an ajax call to get the updated list.

1 Comment

Thank you. Right now the list comes from the php session therefore I can easily convert it to json. So Joshuas attempt helped me further.

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.