PHP next($ array)和Javascript [英] PHP next($array) and Javascript
问题描述
我最近开始学习网络开发,目前正在研究一个网页,该网页读取文件夹中的许多音频文件,并允许用户编写有关她/他所听到内容的记录.我是从几天前开始的,我设法通过php来获取文件列表,然后通过改组数组来播放(让用户实际玩)一个随机文件(很明显,每次加载页面时,一个随机文件都可以播放).这似乎工作正常.
I recently started learning web developing and I am currently working on a web page that reads a number of audio files in a folder and lets the user write a transcript about what she/he had heard. I started a few days ago and I managed to get a list of files via php and play(let the user play actually) a random file via shuffling the array(to be clear, every time the page is loaded a random file is ready to be played). This seems to be working fine.
我还想添加一些按钮,使用户可以收听下一个/上一个文件,但是无论何时何地我写next($array)
行都行不通,更糟糕的是shuffle
函数也停止工作.我不知道是否是因为我在标签中使用了php,但是我的直觉告诉了我.
I also wanted to add to buttons that would let the user listen to the next/previous files, but whenever or wherever I write the next($array)
line it doesn't work and what is worse is that the shuffle
function also stops working. I don't know whether or not it is because I am using php in tags but something in my gut tells me so.
我不知道我是否足够清楚,但我将不胜感激.所以这是我的代码:
I don't know if I was clear enough but I would appreciate any help. So here is my code:
<html>
<head>
</head>
<body>
<p align="center">
<button><-</button>
<audio src="./Sound data/AAA2028C4_0.wav" type="audio/wav" align="center" id="RandomAudio" controls="controls">
</audio>
<button name="nextButton" onClick="next()">-></button>
</p>
<script language="JavaScript" type="text/javascript">
<?php
//Scan the sound data folder for files
$dir = './Sound data';
$files = scandir($dir);
$random_file = shuffle($files);
?>;
function load(){
var test = "./Sound data/<?php
//Select random element from files array
echo $files[$random_file];
?>";
//Play a random file
document.getElementById('RandomAudio').src=test;
}
function next(){
var next = "./Sound data/<?php
$next_file = next($random_file);
echo $files[$next_file];
?>";
//Play the next file
document.getElementById('RandomAudio').src=next;
}
window.onload = load;
</script>
<form method="post" action="testecho.php" align="center">
<strong><label>Transcript : </label></strong>
<br>
<textarea name ="transcript" rows="2" cols="40"> </textarea>
<input type="submit" />
</form>
</body>
</html>
推荐答案
对于您使用的每个功能,请查看手册.这有助于他们习惯.
For each function you use, take a look in the manual. That helps to get used to them.
我们来看看:
$random_file = shuffle($files);
shuffle
函数返回TRUE
或FALSE
.因此,$random_file
变量仅在数组$files
是否已成功改组后才有效.
The shuffle
function returns TRUE
or FALSE
. So the $random_file
variable is only of information if the array $files
has been successfully shuffled or not.
echo $files[$random_file];
因此完全是伪造的. $files
已经被改组为 ,而$random_file
是TRUE
或FALSE
.但是数组键是整数或字符串.
Is therefore totally bogus. $files
has been shuffled already and $random_file
is TRUE
or FALSE
. But array keys are integers or strings.
我很确定您能弄清楚该如何解决.
I'm pretty sure you figure it out how to fix that.
与 next
类似:
$next_file = next($random_file);
echo $files[$next_file];
即使$next_file
已经是下一个文件(如果$random_file
是经过改组的$files
数组(不是,请参见上文)),也无需将其用作下一行的键:
Even though $next_file
is the next file already if $random_file
would have been the shuffled $files
array (it is not, see above), there would be no need to use it as key in the next line:
echo $files[$next_file];
那没有任何意义.逐步编写代码.使用var_dump
之类的调试功能来显示发生的情况.仔细检查PHP手册,了解您当前正在使用的功能.然后逐行编写代码,您可以快速准备并学到很多东西.
That does not make any sense. Write your code step by step. Use debugging function like var_dump
to display what happens. Double-check the PHP manual for the function you're currently using. Then write the code line-by-line and you have it ready quickly and learned a lot.
不要盲目飞行.在每次函数调用之前和之后打印输出,并检查您是否期望函数真正完成.
Don't fly blind. Print out before and after each function call and check if what you expect the function to do is really done.
这篇关于PHP next($ array)和Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!