0

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 &current_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?

3
  • Do you need the array to be available in JavaScript? Commented Nov 1, 2012 at 17:14
  • I think maybe you need to put some form of loop in there, chief. Commented Nov 1, 2012 at 17:14
  • @dpk2442 no i just need the current audio file actually. Commented Nov 2, 2012 at 14:14

1 Answer 1

2

You're mixing PHP and JavaScript. You can't do that. The code will be interpreted, and you'll have only the next file available. Your output prabably is:

var nextElement = "./Sound data/2.mp3";
   document.getElementById('RandomAudio').src=nextElement;

In the second time you press the button, the same code will be executed. That's why you can't go further. You have to find a way to retrieve from the server the next audio, and for that you must keep track of which audio is beeing played now.

getsong.php

<?php
session_start(); //init session

if(!isset($_SESSION['songlist']) or empty($_SESSION['songlist']))
{ //we'll create our songlist JUST ONCE, when it's not created yet. And then, we'll put this list in a SESSION, so we can access it later.
    $dir = './Sound data';
    $files = scandir($dir);
    $_SESSION['songlist'] = array();
    foreach($files as $file)
    {//loop through song list and just add actual files, disposing "." and ".."
        if(is_file($file) and $file != "." and $file != "..")
        {
            $_SESSION['songlist'][] = $file; //add $file to songlist array.
        }
    }
    shuffle($_SESSION['songlist']);//shuffle list
    $_SESSION['current'] = 0; //iniate current song with first song in the list.
}//note that we'll create and shuffle the song list just once.

$norp = $_GET['name'];

$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)) //if current is after the last element, set it to the first song
        $current_song = 0;
    echo "./Sound data/$songlist[$current_song]";
}
else if($norp == 'prevS'){
    $current_song--;     //changes currentsong to prev song
    if($current_song == -1) //if current is before the fist song, set it to the last song
        $current_song = count($songlist) - 1;

    echo "./Sound data/$songlist[$current_song]";
}

$_SESSION['current'] = $current_song; //store current song for later use.
?>

listensong.php //page that plays your songs (with code optimization)

<script type="text/javascript">
    //using jQuery
    function getSong(type)
    {
        $.get('getsong.php', {name: type}, function(song){
            document.getElementById('RandomAudio').src = song;
            document.getElementById('filename').innerHTML= song;
        });
    }
</script>
<a href="javascript:getSong('prevS')">Previous Song</a>
<a href="javascript:getSong('nextS')">Next Song</a>
Sign up to request clarification or add additional context in comments.

6 Comments

thanks a lot! this was really helpful. i tried making it this way. the reading of the directory and the shuffling of the array is being done in another .php file and in getsong.php i am including that file. the next button seems to be working but i don't know whether i am shuffling the array everytime i click on 'Next Song' and getting the first element or actually going forward. therefore i wanna ask: is there a way to call a specific function from 'getsong.php'using jquery?
i tried using something like $.get('getsong.php', { nextorprev = 'next' }, function(song){} for the next button and writing an if statement in the getsong.php but i am having difficulty using the passed nextorprev variable in the getsong.php .any tips on how i can do that?
ok i managed to get the passed variable but i found out that i am shuffling the array everytime i click on next and i am listening to the first song.
well i still can't advance beyond 1. i disabled the shuffle and its no good.
I've changed the answer with the code you need. Please, read it carefully and try to learn what is happening.
|

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.