2013-04-01 110 views
0

有人會知道我爲什麼不能在foreach循環中使用一段(長)的代碼嗎?foreach循環不能與include

foreach循環中的代碼只執行一次。

此主題topictweets.php上的代碼工作正常,但我想重複它爲每個論壇。 foreach循環沒有include就能正常工作。我也嘗試在foreach循環中明確地使用tweets.php主題的代碼,這當然不起作用。

它包含的代碼用於從數據庫中獲取論壇的主題並查找相關的推文,並將其保存在數據庫中。

有沒有其他方法可以做到這一點?

foreach ($forumlist as $x => $fID) { 

    echo 'id:'.$fID.'<br>'; 

    include 'topictweets.php'; 
    ///////// 



    //////// 
} 

在線版本:http://oudhollandsedrop.nl/webendata/FeedForum/fetchtweets.php

一堆的主題代碼tweets.php

<?php 

//?/ VVVV ---- SELECT TOPICS FOR CURRENT FORUM ----- VVVV //// 




echo $fID; 

$sql = "SELECT Topics_TopicID 
FROM Topics_crosstable 
WHERE Forums_ForumID = '$fID'"; 

$result = mysql_query($sql); 

if (!$result) { 
//echo 'The topiclist could not be displayed, please try again later.'; 
} else { 
if (mysql_num_rows($result) == 0) { 
    // echo 'This topic doesn&prime;t exist.'; 
} else { 
    while ($row = mysql_fetch_assoc($result)) { 
     //display post data 
     // echo $row['Topics_TopicID']; 
     // echo': '; 

     $topic = "SELECT Name 
    FROM Topics 
     WHERE TopicID = " . mysql_real_escape_string($row['Topics_TopicID']); 

     $topicname = mysql_query($topic); 

     if (!$topicname) { 
      // echo 'The topic could not be displayed, please try again later.'; 
     } else { 
      if (mysql_num_rows($topicname) == 0) { 
       // echo 'This topic doesn&prime;t exist.'; 
      } else { 
       while ($row = mysql_fetch_assoc($topicname)) { 
        //display post data 
        // echo $row['Name']; 
        // echo'<br>'; 
        $topiclist[] = $row['Name']; 
       } 
      } 
     } 
    } 
} 
} 

foreach ($topiclist as $key => $value) { 
    $terms .= "" . $value . ","; 
} 
//echo'<p>'; 
//echo rtrim($terms, ","); 
//echo'<p>'; 
//echo'<p>'; 
//echo $terms; 
//$terms="vintage"; 
//Twitter account information 
$username = "Username"; 
$password = "Password"; 



while (true) { 

//$terms="vintage"; 
//echo "search terms: " . substr_replace($terms, "", -1) . "\n"; 
$url = "https://stream.twitter.com/1/statuses/filter.json"; 
$cred = sprintf('Authorization: Basic %s', base64_encode("$username:$password")); 
$param = "track=" . urlencode(substr_replace($terms, "", -1)); 
$opts = array(
    'http' => array(
     'method' => 'POST', 
     'header' => $cred, 
     'content' => $param, 
     'Content-type' => 'application/x-www-form-urlencoded'), 
    'ssl' => array('verify_peer' => false) 
); 
$ctx = stream_context_create($opts); 
$handle = fopen($url, 'r', false, $ctx); 
//var_dump($handle); 
$content = ""; 
$flag = true; 
while ($flag) { 
    $buffer = fread($handle, 100); 
    //$buffer = stream_get_line($handle, 1024, "\n"); 

    $a = explode("\n", $buffer, 2); 
    $content = $content . $a[0]; 
    #var_dump($a); 
    if (count($a) > 1) { 
     #echo $content; 
     #echo "\n"; 
     $r = json_decode($content, true); 
     #var_dump($r); 
     // echo '<p>'; 
     // echo "text: " . $r["text"]; 
     // echo '<br>'; 
     // echo "\nrceated_at: " . $r["created_at"]; 
     // echo '<br>'; 
     // echo "\nuser screen name: " . $r["user"]["screen_name"]; 
     // echo '<br>'; 
     // echo "\nuser id: " . $r["user"]["id"]; 
     // echo '<br>'; 
     // echo "\nid : " . $r["id"]; 
     // echo '<br>'; 
     // echo "\nin_reply_to_status_id: " . $r["in_reply_to_status_id"]; 
     // echo '<p>'; 
     // echo "\n\n"; 
     $created_at = $r["created_at"]; 
     $created_at = strtotime($created_at); 
     $mysqldate = date('Y-m-d H:i:s', $created_at); 
     // 

     // echo'<p>'; 
     foreach ($topiclist as $key => $value) { 
      // echo'getshere!'; 
      //$whichterm = $r["text"]; 
      $whichterm = '"' . $r["text"] . '"'; 
      //echo $whichterm; 
      if (stripos($whichterm, $value) !== false) { 
       // echo 'true:' . $value . ''; 
       //find topicid 
       $whattopic = "SELECT TopicID 
       FROM Topics 
       WHERE Name = '$value'"; 


       //var_dump($whattopic); 
       $tID = mysql_query($whattopic); 
       //var_dump($tID); 

       if (!$tID) { 
        // echo 'topic id not found.'; 
       } else { 
        if (mysql_num_rows($tID) == 0) { 
         // echo 'This topic doesn&prime;t exist.'; 
        } else { 
         while ($rec = mysql_fetch_assoc($tID)) { 

          $inserttweets = "INSERT INTO 
       Tweets(Topics_TopicID, AddDate, Tweetcontent) 
      VALUES('" . mysql_real_escape_string($rec['TopicID']) . "', 
            '" . mysql_real_escape_string($mysqldate) . "', 
            '" . mysql_real_escape_string($r["text"]) . "')"; 

          //WHERE TopicID = " . mysql_real_escape_string($row['Topics_TopicID']) 
         } 
        } 
        $addtweet = mysql_query($inserttweets); 
        if (!$addtweet) { 
         //something went wrong, display the error 
         //echo 'Something went wrong while adding tweet.'; 
         //echo mysql_error(); //debugging purposes, uncomment when needed 
        } else { 
         echo 'Succesfully added tweet'; 
        } 
       } 
      } 
     } 





     die(); 
     $content = $a[1]; 
    } 
} 
fclose($handle); 
} 


?> 
+0

我發現了問題是在topic_tweets.php代碼。它工作正常,但不知何故,它不可能在它後面添加代碼。甚至沒有一個簡單的回聲。有人知道我要去哪裏嗎? – user1803370

回答

0

這應該很好地工作:

include 'topictweets.php'; 
foreach ($forumlist as $x => $fID) { 
    echo 'id:'.$fID.'<br>'; 
} 

你只需要include一次。

+0

整個問題是每個論壇都需要重複topictweets.php。那麼它應該在循環中嗎? – user1803370

+0

'topictweets.php'的內容是什麼? – Mooseman

+1

我會在我的問題中加入它。包括一次不會改變結果不幸的是 – user1803370

0

嘗試include_once()的

但是,爲什麼沒有topictweets.php內的循環?

可以在包括

+0

。我在之前的每個循環中清楚地說明了topictweets.php的整個代碼。這沒關係。或者那不是你的意思? – user1803370

+0

你是否首先連接到數據庫(在包含之前)還是在包含文件中進行連接等?你能提供topictweets.php的代碼嗎? – cantsay

+0

我會在我的問題中加入它。 – user1803370

1

「粘貼」一串代碼通過它做查詢,等等。在此頁面,但隨後循環內循環是不是一個偉大的實踐。實際上,您要查找的是函數或使用定義的類。所以,如果你可以定義你的topictweets.php的功能將包含的代碼,並在循環使用它:

include 'topictweets.php'; 
foreach ($forumlist as $x => $fID) { 

    echo 'id:'.$fID.'<br>'; 
    processYourForums($fID); 

    ///////// 



    //////// 
} 
+0

嗯,聽起來很有希望。我會考慮的! – user1803370

+0

我是一個帶有php功能和全部功能的小菜鳥。 但我想我可以只使用:topictweets($ fID);在foreach循環中添加'function topictweets($ fID){.... return; }在主題tweets.php中循環我的代碼。但是,這似乎有相同的結果。 } – user1803370

+0

然後你的問題似乎算法:是你的$ fID在你的新'topictweets'函數中很好用?您必須確保您的代碼根據您的循環迭代而定位到正確的論壇。 – blint