在foreach循环中输出XML项目
问题描述:
try{
#connection string
// $dbconn = new PDO('mysql:host=localhost;port=3306;dbname=mycooldb',array(PDO::ATTR_PERSISTENT => true));
$dbconn = new PDO('mysql:host=localhost;port=3306;dbname=mycooldb','root','toshiba1',array(PDO::ATTR_PERSISTENT => true));
$q = $dbconn->prepare("SELECT FW_ArtSrcLink FROM FW_ArtSrc WHERE OneSet=1 and leagID=20");
#call stored proc
$q->execute();
#get the rows into an array
$result = $q->fetchAll();
$newsStory = array();
foreach($result as $r){
while($xmlUrl = $r['FW_ArtSrcLink']){
// WHILE there is a link out of 11 matching the $dbconn above, load XML file and convert to channel->item
$ConvertToXml = simplexml_load_file($xmlUrl);
$newsStory[] = $ConvertToXml->channel->item;
# -> Setup XML
} //while
}
# -----> Load News Stories
for($i = 0;$i<sizeof($newsStory); $i++){
//// judging by your code, these are the things you wanted to echo
# Source of Article Info-->
echo $newsStory[$i]->item->title;
echo $newsStory[$i]->item->link;
# Actual News Article Info -->
echo $newsStory[$i]->item->title;
echo 'desc: '.$newsStory[$i]->item->description;
} // for()
} // try
我需要上述代码的帮助。 $ dbconn
查询有11个结果,但我需要每个结果加载并使用simplexml_load_file();
进行转换,以便可以加载所有11条具有15-20个链接的记录。共计约* 15 * 11 = 165个宾馆在foreach循环中输出XML项目
foreach($result as $r){
while($xmlUrl = $r['FW_ArtSrcLink']){
// WHILE there is a link out of 11 matching the $dbconn above, load XML file and convert to channel->item
答
你开始一个foreach循环,你并不需要里面的for循环,删除和更改分配给变量$故事变成相呼应。
////// modify your example code as follows:
# -----> Load News Stories
//// if you haven't already, this is the place to send the content-type text/xml header
header("Content-Type: application/xml"); //// if you've already sent this, then comment out this line
//// note, many browsers will not display raw xml if it has not been styled - you may have to "view source" to see the xml - this is normal
//// if you omit this header, the browser will probably assume it got text/HTML instead.
for($i = 0;$i<sizeof($newsStory); $i++){
//// judging by your code, these are the things you wanted to echo
# Source of Article Info-->
echo $newsStory[$i]->title;
echo $newsStory[$i]->link;
# Actual News Article Info -->
echo $newsStory[$i]->title;
echo $newsStory[$i]->description;
//// foreach ($newsStory[$i] as $story){ //// remove this loop, you do not need it - the for($i) loop is already iterating through your news stories. :)
} // for()
虽然这让我回到了原点。我需要得到所有标题,desc,从每个来源链接。目前有11个来源正在被拉。每个来源有〜15-20项。所以15 * 11 = 165链接...我只有11个,请参阅上面的更新代码。我会很感激,如果你进一步帮助@Steve – CodeTalk 2012-08-01 23:28:53
当你echo $ newsStory [$ i] - >标题;它说“数组()”还是它给一个字符串标题? – Steve 2012-08-02 04:27:31
不,它回应所有的标题,但只有其中的11个 – CodeTalk 2012-08-03 23:45:40