2013-05-04 68 views
0

我現在有正確插入數據的工作代碼。我正在嘗試添加新的movie_Id列。我試圖從XML中獲取電影ID值並將其與電影標題循環,但現在插入的數據不正確。 movie_name對象有3-4個電影名稱。我正在嘗試添加電影ID。無法在新列中插入數據

$movie_name = $arrTitle[0]; 
    $xml = simplexml_load_file("http://api.themoviedb.org/2.1/Movie.search/en/xml/myapi/$movie_name"); 
     foreach($xml->movies->movie as $movies){ 
     $arrMovie_id= $movies->id; 
     } 
     $movie_ids= $arrMovie_id[0]; 
    $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)values('$movie_name','null','$cast','$release','$director',250,230,'$movie_ids')"; 

以下是完整的工作代碼

<?php 
// Include the handy XML data extraction functions. 
include 'xml_regex.php'; 
include 'simple_html_dom.php'; 

$con = mysql_connect('localhost','test','test'); 
mysql_select_db('test',$con); 


// Use cURL to get the RSS feed into a PHP string variable. 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,'mytest.xml'); 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$xml = curl_exec($ch); 
curl_close($ch); 

$arrData = array(); 
// Create an array of item elements from the XML feed. 
$news_items = element_set('item', $xml); 
$del_movie = "delete from jos_movie"; 
mysql_query($del_movie); 

$del_cinema = "delete from jos_cinema"; 
mysql_query($del_cinema); 

foreach($news_items as $item) { 
    $title = value_in('title', $item); 
    $url = value_in('link', $item); 
    $cast = value_in('description', $item); 
    //curl_setopt($ch, CURLOPT_URL,$url); 
    //curl_setopt($ch, CURLOPT_HEADER, false); 
    //curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    //$html = curl_exec($ch); 
    $arrTitle = explode('-',$title); 
    $html = file_get_html($url); 
    $htmlShowTime = ''; 

    // find all span tags with class=gb1 moviTimes moviTmngBox 
    foreach($html->find('ul[style=line-height:2em;]') as $e) 
     $htmlShowTime = $e->plaintext; 

    $movie_name = $arrTitle[0]; 
    $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)values('$movie_name','null','$cast','$release','$director',250,230)"; 
    //echo $sql.'<br>'; 
    //echo $sql_movie; 

    mysql_query($sql_movie); 

    $sqlCount = 'select max(id) from jos_movie'; 
    $data = mysql_query($sqlCount); 
    echo $data; 
    print_r($data); 
    $result = mysql_fetch_array($data); 
    $id = $result[0]; 
    echo '<br>'.$id.'<br>'; 

    //$id = mysql_insert_id(); 
    //echo $id; 

     // find all span tags with class=gb1 
    foreach($html->find('div.moviTmngBox') as $e){ 
     $tagTitle = $e->find('a',0); 
     $tagTime = $e->find('div.moviTimes',0); 
     $name = $tagTitle->title; 
     $time = $tagTime->innertext; 

    $trimName = ''; 
    $temName = strtolower(str_replace(' ','',$name)); 

    if(strpos($temName,'indraaudi1') !== false) 
     $trimName = 'Indra Audi 1' and $cinemaId = '1' and $long='32.726602' and $lat='74.857026'; 
    elseif(strpos($temName,'indraaudi2') !== false) 
    $trimName = 'Indra Audi 2' and $cinemaId = '2'and $long='32.726602' and $lat='74.857026'; 
    elseif(strpos($temName,'indraaudi3') !== false) 
     $trimName = 'Indra Audi 3'and $cinemaId = '3' and $long='32.726602' and $lat='74.857026'; 
    elseif(strpos($temName,'apsra') !== false) 
     $trimName = 'Apsra' and $cinemaId = '4' and $long='32.700314' and $lat='74.858023'; 
    else{ 
     $trimName = trim(substr($name,18,strlen($name))) and $cinemaId = '5' and $long='32.7300' and $lat='74.8700' ; 
    } 

     //echo $tagTime->innertext.'<br/>'; 
     $sql = "insert into jos_cinema(cinema_name,show_time,movie_id,cinemaId,logitude,latitude)values('$trimName','$time',$id,$cinemaId,$long,$lat)"; 
     //echo $sql.'<br/>'; 
     mysql_query($sql); 
     //$arrTem = array($tagTitle->title,$tagTime->innertext); 

    } 

}//end rss feed loop 

?> 

這裏是我的要求。

我將$ movie_name中的影片名稱作爲數組獲取 我已將名稱附加到xml中,以便我可以從中獲取moviedbId。我正在成功獲取movieID。我想插入那個movieDB ID到各自的電影。

請告訴我我錯在哪裏。

+0

首先,您使用的串插,而不是準備。 (下一個最好的事情是使用轉義)。如果由於它不是HTTPS並且您的網站代碼位於此處,所以如果服務器最終處於MITM'd狀態?此外,無論何時發佈代碼,都要完成。我們還沒有得到'$ htmlShowTime'的線索。另外,Whitespace是免費的。用它! – Amelia 2013-05-04 15:14:55

+0

@Hiroto它只是代碼的一部分,它正在工作。我已經添加了一個新的部分來添加代碼中的movieID列。 – Yogus 2013-05-04 15:22:53

回答

2

試試這個:

<?php 

$sql = ""; 
foreach ($arrTitle as $movie_name) { 
    $xml = simplexml_load_file("http://api.themoviedb.org/2.1/Movie.search/en/xml/myapi/$movie_name"); 
    foreach ($xml->movies->movie as $movies) { 
     $arrMovie_id = $movies->id; 
    } 
    $movie_ids = $arrMovie_id[0]; 
    $arrStr = explode(':', $htmlShowTime); 
    $release = substr($arrStr[3], 0, strlen($arrStr[3]) - 8); 
    $director = substr($arrStr[5], 0, strlen($arrStr[5]) - 11); 
    $sql .= "('$movie_name','null','$cast','$release','$director',250,230,'$movie_ids'),"; 
} 
$sql  = substr($sql, 0, -1); 
$sql_movie = "INSERT INTO `jos_movie`(`movie_name`,`language`,`cast`,`movie_release`,`director`,`rating`,`rating_count`,`movie_ids`) VALUES" . $sql; 

?> 
+0

它只是整個代碼的一部分。如果你看到我獲得電影名稱。我在xml中添加電影名稱,以便我可以將movieDb movieIds附加到它。 – Yogus 2013-05-04 15:18:34

+0

我打開了原始數組。在foreach結尾處,您現在可以在一行中完成整個查詢。 – 2013-05-04 15:24:43

+0

謝謝@David我回應了$ movieids,它顯示44425 44425.沒有一個id對於movie_name數組中的任何電影都是正確的,並且它回顯了兩次:( – Yogus 2013-05-04 15:36:33