0
我試圖用下面的代碼完成:它執行SELECT
,它應該返回(2)結果,這些結果每個拉~10項(總項目: 〜20)並將其顯示在頁面上。循環遍歷結果並聚合並吐出php
實際發生的:它拉動第二結果,從而輸出只〜10個結果出來可能20左右,在$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
正確的,應該做的'$ newsStory =陣列();'的foreach – mask8 2012-07-25 00:17:03
@前mask8:是的,這太:) – LeleDumbo 2012-07-25 00:19:36
@LeleDumbo - 我已經編輯我上面的代碼...但它只能輸出兩個項目,不再是10 ...我做錯了什麼?應輸出〜20:/ – CodeTalk 2012-07-25 00:26:02