循环遍历数据库中的每个$ RESULT

时间:2012-07-31 01:14:04

标签: php loops foreach

<?php
## Loop through results from mysql
try{
    #connection string
        // $dbconn = new PDO('mysql:host=localhost;port=3306;dbname=mydb',array(PDO::ATTR_PERSISTENT => true));
        $dbconn = new PDO('mysql:host=localhost;port=3306;dbname=mydb','user','pass',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){
        $xmlUrl = $r['FW_ArtSrcLink'];
        $ConvertToXml = simplexml_load_file($xmlUrl);
        # -> Setup XML
        $newsStory[] = $ConvertToXml->channel->item;
    }
    # -----> Load News Stories
        for($i = 0;$i<sizeof($newsStory); $i++){

    # Source of Article Info-->
            $SrcTitle=$newsStory[$i]->title;
            $SrcLink=$newsStory[$i]->link;

    # Actual News Article Info -->
            $title=$newsStory[$i]->title;
            $desc=$newsStory[$i]->description;

    # Output Results ------------>      
            echo '<hr>';
            echo '<strong>'.'Title:'.$title.'</strong>'.'(via: <a href=\''.$SrcLink.'\'>'.$SrcTitle.'</a>'.'<br />';
            //echo 'Link:'.$link.'<br />';
            echo 'Description'.$desc.'<br>';
            ##echo 'count '.count($result);
            echo '<hr>';

            # echo 'sizeof newstory: '.sizeof($newsStory);
        }   // for()
} // try

使用foreach($result as $r)我希望使用$ConvertToXml->channel->item;等内容获取所有xml项目,以便从每个链接收集所有item个。我该怎么做?

0 个答案:

没有答案