调整Php xml代码以仅提取三个元素,而不是全部
更新代码调整Php xml代码以仅提取三个元素,而不是全部,php,xml,parsing,xml-parsing,Php,Xml,Parsing,Xml Parsing,更新代码 try{ function processLink( $link , $appendArr ){ ## gets url from database as outlined above. $xmlUrl = $link; #Loads the url above into XML $ConvertToXml = simplexml_load_file($xmlUrl); # -> Setup
try{
function processLink( $link , $appendArr ){
## gets url from database as outlined above.
$xmlUrl = $link;
#Loads the url above into XML
$ConvertToXml = simplexml_load_file($xmlUrl);
# -> Setup XML
$appendArr[] = $ConvertToXml->channel->item;
}
#Connect to DB
require_once '../../src/conn/dbc.php';
$dbconn = new PDO('mysql:host=localhost;port=3306;dbname=thedb',$db_user,$db_pass,array(PDO::ATTR_PERSISTENT => true));
$q = $dbconn->prepare("SELECT FW_ArtSrcLink FROM FW_ArtSrc WHERE OneSet=:OneSet and leagID = :TheLeagueID");
$q->execute(array(':OneSet' => 1, ':TheLeagueID' => 14)); # SET LEAGUE HERE.
$result = $q->fetchAll();
$newsStory = array();
$title = $newsStory->title;
$link = $newsStory->link;
foreach ($result as $value ){
if ( is_array($value) ){
foreach ( $value as $secondValue ){
processLink($secondValue , &$newsStory);
}
continue;
}
processLink($value , $newsStory);
}
//print_r($newsStory);
echo 'TITLE: '.$title;
echo 'LINK'.$link;
}
如何修改代码以仅获取[title]
和[link]
?-这意味着它不会输出任何内容:
它目前的产出是:
答案在注释部分代码块的底部
# Source of Article Info-->
# $SrcTitle=$newsStory[$i]->title;
# $SrcLink=$newsStory[$i]->link;
使用print\r($newstory)底部的code>将打印$newsStory
数组中的所有内容。
要仅打印标题和链接,请使用代码中提供的示例访问数组。
您的输出将显示该对象的第四条新闻。如果你想把它们全部打印出来,你需要在最后有一个循环
for($i=0; $i < count($newsStory); $i++){
$title = $newsStory[$i]->title;
$link = $newsStory[$i]->link;
//use this next line if you just want to test and see output.
echo "<p>Newstory $i: Title:$title Link:$link </p>";
//use this to provide to a user
echo "<p><a href='$link'>$title</a></p>";
}
for($i=0;$ititle;
$link=$newstory[$i]->link;
//如果您只想测试和查看输出,请使用下一行。
echo“Newstory$i:Title:$Title-Link:$Link”;
//使用此选项可向用户提供
回声“”;
}
谢谢你的帖子。问题是,它没有输出任何东西。。。请查看更新后的代码abov。请在//print\r($newstory)
之后立即将我的循环代码复制并粘贴到脚本中,谢谢Jmm!那很有帮助!