Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8772897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:08:58+00:00 2026-06-13T18:08:58+00:00

I am new to PHP and am working currently on this web page that

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:08:59+00:00Added an answer on June 13, 2026 at 6:08 pm

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on a PHP/MySQL script that does the following, in this order:
I am working on a web application in php mysql.Here,the requirement says,that the user
I am VERY new to PHP, I'm currently working on a service, part which
Currently, I am working on restructuring an existing code base. I'm new to php
I'm not sure why this SQL query is not working. I'm new to SQL/PHP
This is working fine in core php, but not in magento. $party_payment = new
I am working on a Symfony project and I currently have this: <?php echo
I'm working on a PHP parser that parses my school's HTML 'groups' page. These
I have a working PHP web service that returns data (if I input the
I'm currently working with PHP, and this footer is one of the components of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.