2012-09-05 201 views
0

嘗試從xml文件中提取id,將其傳遞到api查詢中,並將結果加載到dom文檔中。事情是我的foreach循環只返回第一次迭代,然後似乎停止。foreach循環在第一次迭代後停止

爲什麼不回去取下一個PROGRAM_ID

//load results of first api call into simplexml - print_r here gives me a big array with all the expected rows in it 
$progsitecontent = simplexml_load_file($progsiteapi); 

//set which nodes to step through to reach required information 
$totalprogsitecontent = $progsitecontent->matrix->rows->row; 

//for each instance of a program id in this simplexml file: 
foreach($totalprogsitecontent->PROGRAM_ID as $progid) 
{ 

    //...substitute the program id into the api call 
    $programdetails = $progdetailsapi_start.$progid.$progdetailsapi_end; 
    $complete_program_details = simplexml_load_file($programdetails); 

    //now for each instance of a programs info, load into a DOM document and carry out the below actions - from here down already works in another script so im sure the problem has to be above this point 

    $prog_info = $complete_program_details->matrix->rows->row; 

    //create the top line container tag 
    $row = $doc->createElement ("programInformation"); 

    //create the container tag 
    $progID = $doc->createElement("programId"); 
    //fill it with the information you want 
    $progID->appendChild ($doc->createTextNode ($prog_info->PROGRAM_ID)); 
    //attach this information to the row 
    $row->appendChild($progID); 

    //repeat for each element you want to include 
    $progName = $doc->createElement("programName"); 
    $progName->appendChild ($doc->createTextNode ($prog_info->PROGRAM_NAME)); 
    $row->appendChild($progName); 

    $progURLs = $doc->createElement("programUrls"); 
    $progURLs->appendChild ($doc->createTextNode ($prog_info->PROGRAM_URLS)); 
    $row->appendChild($progURLs); 

    $progLogo = $doc->createElement("programLogo"); 
    $progLogo->appendChild ($doc->createTextNode ($prog_info->MERCHANT_LOGO)); 
    $row->appendChild($progLogo); 

    $r->appendChild ($row); 

} 

echo $doc->saveXML(); 

隨意評論這是怎麼寫的。我仍然在「bodge,它和觀望」的舞臺:)

+0

'$ totalprogsitecontent'的結果是什麼? –

+0

爲$ totalprogsitecontent發佈xml部分 –

+0

是'$ totalprogsitecontent-> PROGRAM_ID'數組? – nick

回答

1

不能沒有看到的$totalprogsitecontent全部結果說太多,但我認爲它應該是這個樣子:

foreach($totalprogsitecontent as $progid) 
{ 
... 
} 

既然$totalprogsitecontent->PROGRAM_ID已經是一個單一的值 - 所以你迭代這個元素而不是數組。

另外,您的$progid是for循環中的小寫字母,但您指的是$progID - PHP區分大小寫。


查看您的XML代碼後,看看它應該是什麼樣子。

foreach($progsitecontent->matrix->rows->row as $row){ 
    $progid = $row['PROGRAM_ID']; 
    $affid = $row['AFFILIATE_ID']; 
} 
+0

感謝您的支持他指出。我已經將變量更改爲相同的大小寫,但刪除 - > matrix-> rows-> row-> PROGRAM_ID已經阻止它返回,即使是第一次迭代 – dex711

+0

我更新了我的答案。讓我知道這是怎麼回事 – nick

+0

嗨,尼克,它排序,謝謝! 問題在於我在xml中向下鑽取了一個節點。 我正在爲矩陣 - >行 - >行 - > programID 下的每一塊數據去我真正想要的是去矩陣 - >行 - >行下的每一塊數據,然後從$行對象抓取細節 乾杯 – dex711

相關問題