使用数组获取细节时不规范

时间:2013-05-12 02:34:05

标签: php arrays

我有一个表结构,我试图在插入查询之前从moviedb.org获取movie_id。

enter image description here

这是代码的一部分

      $movie_name = $arrTitle[0];

    $apiKey = '30f44b6ef9472d414e50d2acaa058b60';
    $url = simplexml_load_file("http://api.themoviedb.org/2.1/Movie.search/en/xml/30f44b6ef9472d414e50d2acaa058b60/"$movie_name"");
    $xml = simplexml_load_file($url);
    $movies = $xml->movies->movie;
    $images = $xml->xpath("//image");
   foreach ($movies as $movie){
        $arrMovie_id = $movie->id;
    }
    foreach ($images as $image){
    $attr = $image->attributes();
     if($image['size'] == "thumb" && $image['type']=="poster"){       
        $images= $image['url'];
        }
    }
    $arrStr = explode(':',$htmlShowTime);
    $release = substr($arrStr[3],0,strlen($arrStr[3])-8);
    $director = substr($arrStr[5],0,strlen($arrStr[5])-11); 

    $sql_movie = "insert into jos_movie(movie_name,language,cast,movie_release,director,rating,rating_count,movie_ids,image)values('$movie_name','null','$cast','$release','$director',250,230,'$arrMovie_id','$images')";
    //echo $sql.'<br>';
   // echo $sql_movie;

    mysql_query($sql_movie);

正如您所看到的代码我试图从$url获取movie_id,但在图像字段中获取0和某些情况下的“数组”

例如第一部电影“Go Goa Gone”

网址为http://api.themoviedb.org/2.1/Movie.search/en/xml/30f44b6ef9472d414e50d2acaa058b60/%22Go%20Goa%20Gone%22

它应该获取id和图像

<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
  <opensearch:Query searchTerms="&quot;Go+Goa+Gone&quot;"/>
  <opensearch:totalResults>1</opensearch:totalResults>
  <movies>
    <movie>
      <score>42.34326</score>
      <popularity>3</popularity>
      <translated>true</translated>
      <adult>false</adult>
      <language>en</language>
      <original_name>Go Goa Gone</original_name>
      <name>Go Goa Gone</name>
      <alternative_name></alternative_name>
      <type>movie</type>
      <id>191562</id>
      <imdb_id>tt2436516</imdb_id>
      <url>http://www.themoviedb.org/movie/191562</url>
      <votes>0</votes>
      <rating>0.0</rating>
      <certification>NR</certification>
      <overview>A rave party off the coast of Goa, goes horrifyingly and hilariously wrong when the island is overrun with zombies.</overview>
      <released>2013-05-10</released>
      <images>
        <image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w92/cSDvyWmh7rSD7WtSoXScVw3GDnD.jpg" size="thumb" width="92" height="133" id="51880dd7760ee3144531d0b9"/>
        <image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w154/cSDvyWmh7rSD7WtSoXScVw3GDnD.jpg" size="w154" width="154" height="222" id="51880dd7760ee3144531d0b9"/>
        <image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185/cSDvyWmh7rSD7WtSoXScVw3GDnD.jpg" size="cover" width="185" height="267" id="51880dd7760ee3144531d0b9"/>
        <image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w342/cSDvyWmh7rSD7WtSoXScVw3GDnD.jpg" size="w342" width="342" height="493" id="51880dd7760ee3144531d0b9"/>
        <image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w500/cSDvyWmh7rSD7WtSoXScVw3GDnD.jpg" size="mid" width="500" height="721" id="51880dd7760ee3144531d0b9"/>
        <image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/cSDvyWmh7rSD7WtSoXScVw3GDnD.jpg" size="original" width="810" height="1168" id="51880dd7760ee3144531d0b9"/>
        <image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w300/mjxJT7skpStIGyp1bAe7XCsTVEN.jpg" size="thumb" width="300" height="169" id="51880e1f760ee3059e36a012"/>
        <image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w780/mjxJT7skpStIGyp1bAe7XCsTVEN.jpg" size="poster" width="780" height="439" id="51880e1f760ee3059e36a012"/>
        <image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w1280/mjxJT7skpStIGyp1bAe7XCsTVEN.jpg" size="w1280" width="1280" height="720" id="51880e1f760ee3059e36a012"/>
        <image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/mjxJT7skpStIGyp1bAe7XCsTVEN.jpg" size="original" width="1280" height="720" id="51880e1f760ee3059e36a012"/>
      </images>
      <version>1</version>
      <last_modified_at>2013-05-06 20:14:04 UTC</last_modified_at>
    </movie>
  </movies>
</OpenSearchDescription>

以下是$movie_name

的var_dump
string(12) "Go Goa Gone "
string(6) "Gippi "
string(19) "Shootout at Wadala "
string(23) "Lucky Di Unlucky Story "
string(11) "Aashiqui 2 "

我在这种不规范的代码中出错了吗?

1 个答案:

答案 0 :(得分:1)

你需要在找到多个图像的for循环中执行这样的操作。我在XML文件中创建了多个电影列表的列表,并在下面的代码中进行测试。

//$movies = $xml->movies;
    $movies = $xml->xpath("//movies/descendant::movie");
    //echo "<pre>";print_r($movies);die;
    //$images = $xml->xpath("//image");
    foreach ($movies as $movie){
        echo $arrMovie_id = $movie->id;
        $images = $movie->xpath("//image");
        //echo "<pre>";print_r($images);
            $imageUrl = array();
        foreach ($images as $image){
            if($image['size'] == "thumb" && $image['type']=="poster"){       
                $imageUrl[] = $image['url'];
            }
        }

        //then you need to implode $imageUrl array to string so that you can add multiple found image urls
        $imageUrl = implode(",", $imageUrl);

        $arrStr = explode(':',$htmlShowTime);
        $release = substr($arrStr[3],0,strlen($arrStr[3])-8);
        $director = substr($arrStr[5],0,strlen($arrStr[5])-11); 

        $sql_movie = "insert into jos_movie(movie_name,language,cast,movie_release,director,rating,rating_count,movie_ids,image)values('$movie_name','null','$cast','$release','$director',250,230,'$arrMovie_id','$imageUrl')";
        //echo $sql.'<br>';
        echo $sql_movie;

        //mysql_query($sql_movie);
        echo "<hr/>";
    }

编辑: imageUrl在代码中创建为数组 $ imageUrl在sql查询而不是$ images(它是XML数组,所以不会被使用)

从上面的代码我可以在表中插入多个电影,见下图 multiple movie list