简体   繁体   中英

Adjusting Php-xml code to just pull three elements, not all

UPDATED CODE

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;
}

How do I modify my code to ONLY grab the [title] and [link] ? - which means its not outputting anything::

It currently outputs: img1

The answer is at the bottom of the code block in the commented section.

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

Using print_r($newsStory); at the bottom will print everything inside $newsStory array. To print out just title and link, access the array using the example provided in the code. Your output appears to display the 4th news story of the object. If you want to print them all, you need a loop at the end.

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>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM