Hello all, fairly new to PHP and having some difficulty with some loops. First let explain what I am doing. The PHP function is populated with a list passed from another function. This list contains category items such as Comedy, Horror, History, Reality and so on. In the first Foreach the item of the list is used to curl and return $html. The $html contains multiple shows that fall into the category or $textarray. The second Foreach will parse the $html for all img tags and save the value to the appropriate field. The third Foreach will parse the $html for the span class as indicated in the $titlepath. It will parse the value for 'title' and store in appropriate field. When all is done I will have an xml file for each category or $textarrary named $textarray.xml. All works with the exception of adding the item element to the xml structure. When the script is executed, the xml structure is created, the $textarray is parse as desired, the curl is performed, the $html is returned, the img is parsed and written and the file is saved. Instead of ending up with the following xml structure:
Assuming the category has 2 shows listed
<feed>
<resultLength>2</resultLength>
<endIndex>2</endIndex>
<item>
<sdImg>PATH TO JPG</sdImg>
<hdImg>PATH TO JPG</hdImg>
<title>NAME OF SHOW</title>
<item>
<sdImg>PATH TO JPG</sdImg>
<hdImg>PATH TO JPG</hdImg>
<title>NAME OF SHOW</title>
</feed>
I end up with multiple title elements under each item. If the category has 10 shows in the $html, I get 10 item elements as expected with each having sdImg, hdImg but then 10 title under each item. I am guessing it has something to do with closing the foreach but I really haven't been able to get it to work. Any advice will be greatly appreciated and remember if something looks wrong it probably is since I am new to doing this.
Thanks
Here is the code:
foreach($textarray as $text){ //textarray-SHOW NAME PASSED FROM SCRAPPER
$doc=new DOMDocument('1.0', 'UTF-8'); //SETUP NEW XML DOCUMENT
$feed=$doc->createElement("feed"); //CREATE FEED ELEMENT FOR XML ROOT
$item= $doc->createElement("item");
$dom = new DOMDocument(); //NEW dom FOR EACH SHOW
$dom->formatOutput = true;
$dom->preserveWhiteSpace = true;
libxml_use_internal_errors(true);
$resultstring = rtrim(" ".$text);
$startpos = strpos($resultstring,"("); //FIND START OF RESULT COUNT
$endpos=strpos($resultstring,")"); //FIND END OF RESULT COUNT
$resultText=substr($resultstring,$startpos); //CUT RESULT COUNT (NUMBER)
$len=strlen($resultText);
$resultcount=substr($resultText,1,$len);
$resultcount=substr($resultcount,0,-1); //TOTAL COUNT OF SHOWS IN EACH CATEGORY
$xmlname=ltrim($text);
$curlpath=rtrim($curlpath); //TRIM SPACES FROM RIGHT OF URL
$text=rtrim($text); //TRIM SPACES FROM RIGHT OF TEXT NAME
$string=$curlpath.$text.".htm"; //BUILD STRING
list($urlpath,$querystring)=explode('(', $string, 2 ); //EXPLODE STRING AT (
$url= $urlpath. ".htm"; // ADD EXTENSION
$url= str_replace(' ','',$url); //PATH TO CURL
$string2= $xmlpath.$xmlname;
list($xmlstring, $querystring)=explode('(', $string2, 2);
$xmlsave=rtrim($xmlstring).".xml"; //SAVE XML TO PATH
$html=curlurl($url); //CURL EACH SHOW NAME TO PARSE
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$titlepath=$xpath->query('//span[@class="ui-txt"]'); //TITLE OF SHOW FOR EACH CATEGORY
$images= $dom->getElementsByTagName('img');
//PARSE EACH SHOW INFORMATION
foreach($images as $img){
$icon= $img ->getAttribute('src');
if( preg_match('/\.(jpg|jpeg|gif)(?:[\?\#].*)?$/i', $icon) ) { //only matching types
#ECHO $icon."<br>";
// ITEM TAG
$sdAttribute = $doc->createAttribute("sdImage");
$sdAttribute->value = $icon;
$item->appendChild($sdAttribute); //HOLD LOCATION
$hdAttribute = $doc->createAttribute("hdImage");
$hdAttribute->value = $icon;
$item->appendChild($sdAttribute);
$item->appendChild($hdAttribute); //HOLD LOCATION
}
foreach ($titlepath as $titles){
// PARSE TITLE FROM
$title=$doc->createElement("title");
$titleText=$doc->createTextNode($titles->nodeValue);
$title->appendChild($titleText);
$item->appendChild($title);
}
//CREATE RESULT AND ENDINDEX ELEMENTS FOR XML/FEED
$result=$doc->createElement("resultLength");
$endIndex=$doc->createElement("endIndex");
$resultText= $doc->createTextNode($resultcount);
$endIndexText= $doc->createTextNode($resultcount);
$result->appendChild($resultText);
$endIndex->appendChild($endIndexText);
$feed->appendChild($item);
}
$feed->appendChild($result);
$feed->appendChild($endIndex);
$doc->appendChild($feed);
$doc->save($xmlsave);
}