-1

好吧,基本上这段代码所做的就是从网站的源代码中获取一些链接并将它们发送到 mp3 播放器。最大的问题在于 get_link 函数,我想将 url 存储到数组中。我有问题的部分被评论。很抱歉发布所有这些代码,但这些功能是相互连接的。

function getHost($db,$id){
        if(isset($_GET['id'])){

            $sql1 = "SELECT host FROM mixtape WHERE id=?";
            $stm = $db->prepare($sql1);
            $stm->execute(array($id));
            $row1 = $stm->fetch(PDO::FETCH_ASSOC);

            if($row1['host']=='host1'){

                $sql2 = "SELECT link1 FROM faixa WHERE id_mixtape IN(SELECT id FROM mixtape WHERE id=?)";
                $stm = $db->prepare($sql2);
                $stm->execute(array($id));

                $rows_affected = $stm->rowCount();
                        $array=array();
                if (count($rows_affected) > 0) {
                    for($i=1; $i <= $rows_affected; $i++) {

                        $row2 = $stm->fetch(PDO::FETCH_ASSOC);
                        $url=$row2['link1'];

                        get_Link($db,$url,$i,$rows_affected,$array);                        
                    }
                }
            }
        }
    }

    function get_Link($db,$url,$pos,$rows_affect,$array){ 

        $find = 'url:';
        $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
        $data = file_get_contents($url);
        $data = explode("\n", $data);

        for ($line = 0; $line < count($data); $line++) {
            if (strpos($data[$line], $find) !== false) {
                $link = preg_replace($reg_exUrl,"", $data[$line]);
                $v[]=$link;
            }   
        }

        if($pos!=$rows_affect-1){
            $url="mylink.com/".$link."|";
        }else{
            $url="mylink.com/".$link."&amp";
        }

        $array[$pos]=$url;  
        var_dump($array); // Here says that are 3 values in the array. True

        if($pos==$rows_affect-1){
            var_dump($array); // Here is only showing the last value in the array. Why?
            player($db,$array);
        }
    }

    function player($db,$array){
        if(isset($_GET['id'])){
            foreach($array as $i=>$item){ 
                echo $item; 
            }
        }
    }
4

2 回答 2

1

这段代码:

$c=0;

    for ($line = 0; $line < count($data); $line++) {
        if (strpos($data[$line], $find) !== false) {
            $link = preg_replace($reg_exUrl,"", $data[$line]);
            $v[$c]=$link;
        }   
    }

应该是这样的:

 $c=0;

    for ($line = 0; $line < count($data); $line++) {
        if (strpos($data[$line], $find) !== false) {
            $link = preg_replace($reg_exUrl,"", $data[$line]);
            $v[$c]=$link;
            $c = $c+1; //this is missing or $c++;
        }   
    }

或者:

    for ($line = 0; $line < count($data); $line++) {
        if (strpos($data[$line], $find) !== false) {
            $link = preg_replace($reg_exUrl,"", $data[$line]);
            $v[]=$link; //That way works too
        }   
    }
于 2013-04-29T14:09:40.297 回答
0

Miguelfsf,您必须首先了解变量范围。

您的问题是,您创建了 $array=array() ($array 显示为 $row2)。您声明了一个新变量,但您没有使用,$array 只是告诉您,以这种方式声明您的数组

$row2=array().

之后你需要做

$row2[] = $stm->fetch(PDO::FETCH_ASSOC);

为什么?因为 assoc 返回一个关联数组,所以它会做 $array => {title => "last title} = $newData => {title => "new title"} 它将替换值使用 [] 每次你做这将创建一个新元素。然后

{
   0 => { title => "title1"}
   1 => { title => "title2"}
   2 => { title => "title3"}
   3 => { title => "title4"}
}
于 2013-04-30T16:42:43.327 回答