I am new to PHP and am working currently on this 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. At the moment i can read files in a directory and shuffle that array. I also wanted to implement two buttons that would let the user listen to the next/previous files. That is where i am having problems right now. The next button seems to be working but not as i would like. The problem is that i can move the pointer just once. I mean i can just from the first file to the second and i can't advance any further. Here is my code for the function that is called when the user clicks on the button: function nextelem(){
var nextElement = "./Sound data/<?php
$current_id = current($files);
$current_index = array_search($current_id, $files);
$next_index = $current_index+1;
echo $files[$next_index];
next($files);
?>";
//Play the next file
document.getElementById('RandomAudio').src=nextElement;
}
I'm guessing that the problem is that current($array) and next($array) functions work by the by-value principle and so every time the function is being called, a copy of the original array is passed so everytime the button is clicked i get the first element as the current and moving the pointer with next($array) doesn't really have an effect.
So i tried writing my own current($array) and next($array) functions:
<?php
function ¤t_by_ref(&$arr) {
return $arr[key($arr)];
}
function &next_by_ref(&$arr) {
return $arr[key($arr)+1];
}
?>;
but these did not help either. I would really appreciate any help or tips. This is starting to get to my nerves. (PS: I opened a topic about (the same project but not the same problem) and that was really helpful. the link is here PHP next($array) and Javascript . I posted all of the code there, it is not the last version i am working on right now but it can give you more of an idea about the page i think. so thanks anyway)
based on freon's answer i changed to code to :
session_start();
$dir = './Sound data';
$files = scandir($dir);
$norp = $_GET['name'];
$current_id = current($files);
$current_index = array_search($current_id, $files);
$_SESSION['current'] = $current_index;
$_SESSION['songlist'] = $files;
$current_song = $_SESSION['current']; //index in songlist for your song
$songlist = $_SESSION['songlist']; //this session var has your $files array
if($norp == 'nextS'){
$current_song++; //changes currentsong to next song
if($current_song == count($songlist))
$current_song = 0;
echo "./Sound data/$songlist[$current_song]";
}
else if($norp == 'prevS'){
$current_song--; //changes currentsong to prev song
if($current_song == count($songlist))
$current_song = 0;
echo "./Sound data/$songlist[$current_song]";
}
?>
and
<script language="JavaScript" type="text/javascript" >
function getNextSong()
{
$.get('getsong.php', {name : 'nextS'}, function(song){
document.getElementById('RandomAudio').src = song;
document.getElementById('filename').innerHTML= song;
// alert(song);
});
}
function getPreviousSong()
{
$.get('getsong.php', {name : 'prevS'}, function(song){
document.getElementById('RandomAudio').src = song;
document.getElementById('filename').innerHTML= song;
// alert(song);
});
}
</script>
but i still can't reach beyond the immediate next element. am i doing something wrong?