2013-04-01 114 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