PHP next($ array)只移动指针一次

时间:2012-11-01 17:12:05

标签: php pointers next

我是PHP的新手,目前正在这个网页上工作,该网页读取文件夹中的许多音频文件,并让用户写下她/他听过的内容的记录。目前,我可以读取目录中的文件并随机播放该数组。我还想实现两个按钮,让用户可以收听下一个/上一个文件。这就是我现在遇到问题的地方。下一个按钮似乎正在工作但不是我想要的。问题是我只能移动指针一次。我的意思是我可以从第一个文件到第二个文件,我无法继续前进。这是我在用户点击按钮时调用的函数代码: 函数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;


          }

我猜测问题是当前($ array)和next($ array)函数按by-value原则工作,因此每次调用函数时,都会传递原始数组的副本每次单击按钮时,我将第一个元素作为当前元素,并使用next($ array)移动指针并不会产生任何影响。

所以我尝试编写自己的当前($ array)和next($ array)函数:

        <?php
               function &current_by_ref(&$arr) {
                    return $arr[key($arr)];
                }
                function &next_by_ref(&$arr) {
                    return $arr[key($arr)+1];
                }
        ?>;

但这些也没有帮助。我真的很感激任何帮助或提示。这开始让我感到紧张。 (PS:我开了一个关于(同一个项目,但不是同一个问题)的话题,这真的很有帮助。链接在这里PHP next($array) and Javascript。我在那里发布了所有代码,它不是我的最后一个版本现在正在努力,但它可以让你更多地了解我认为的页面。所以,无论如何,谢谢。

基于氟利昂的答案我改为代码:

               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]";
                    }

&GT;

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

但我仍然无法超越下一个元素。我做错了什么?

1 个答案:

答案 0 :(得分:2)

您正在混合使用PHP和JavaScript。你不能这样做。 代码将被解释,您将只有下一个文件可用。 可输出的输出是:

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

在第二次按下按钮时,将执行相同的代码。这就是为什么你不能走得更远。 你必须找到一种方法从服务器检索下一个音频,为此你必须跟踪现在播放的音频。

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(带代码优化)

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