繁体   English   中英

遍历结果并汇总并吐出php

[英]Loop through results and aggregate and spit out php

我在下面的代码中尝试完成的工作:它执行SELECT ,它应该返回(2)个结果,这些结果各拉〜10个项目(总项目:〜20个),并将其显示在页面上。

实际发生的情况:它拉出第二个结果,因此仅在可能的20个结果中输出约10个结果。因此, $result var在循环后实际上应该总共有约20个项目……但它只有10个。已经测试了实际的输出和查询...

有什么想法吗?

<?php

## Loop through results from mysql
try{
    #connection string
        // $dbconn = new PDO('mysql:host=localhost;port=3306;dbname=thdb',array(PDO::ATTR_PERSISTENT => true));
        $dbconn = new PDO('mysql:host=localhost;port=3306;dbname=thdb','user','pass',array(PDO::ATTR_PERSISTENT => true));
        $q = $dbconn->prepare("SELECT FW_ArtSrcLink FROM FW_ArtSrc WHERE OneSet=1");
    #call stored proc
        $q->execute();
    #get the rows into an array
        $result = $q->fetchAll();
        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 '<hr>';
        }
} // try

catch(Exception $e){

    $errorStored = $e->getMessage() . ' on ' .'/errors/fanwire_loop.php';  #where errors are stored
    $pageDateOfError = '/aggregate_looping.php'.date('l jS \of F Y h:i:s A'); #inc the file and date into the file too
    file_put_contents($errorStored,$pageDateOfError, FILE_APPEND | LOCK_EX);
} // catch

?>

* *编辑try{}

# Only outputs (2) `items`
    try{
        #connection string
            // $dbconn = new PDO('mysql:host=localhost;port=3306;dbname=thdb',array(PDO::ATTR_PERSISTENT => true));
            $dbconn = new PDO('mysql:host=localhost;port=3306;dbname=thdb','user','pass',array(PDO::ATTR_PERSISTENT => true));
            $q = $dbconn->prepare("SELECT FW_ArtSrcLink FROM FW_ArtSrc WHERE OneSet=1");
        #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 '<hr>';
            }
    } // try
foreach($result as $r){
  $xmlUrl = $r['FW_ArtSrcLink'];
  $ConvertToXml = simplexml_load_file($xmlUrl);
  # -> Setup XML
  $newsStory = $ConvertToXml->channel->item;
}

您在这里对$newsStory了破坏性分配,我假设它是一个数组,所以请使用$newsStory[] = ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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