foreach循环不适用于include

时间:2013-04-01 21:06:35

标签: php foreach include

有人会知道为什么我不能在foreach循环中使用(长)代码吗?

foreach循环中的代码只执行一次。

topictweets.php上的这段代码可以自行运行,但我想为每个论坛重复一遍。 foreach循环在没有包含的情况下工作正常。我还尝试在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);
}


?>

3 个答案:

答案 0 :(得分:1)

在循环中“粘贴”一堆代码并不是一个很好的做法。事实上,您正在寻找的是功能或使用已定义的类。因此,如果可以,请在topictweets.php中定义一个包含代码的函数,并在循环中使用它:

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

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

    /////////



    ////////
}

答案 1 :(得分:0)

这应该可以正常工作:

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

您只需要include一次。

答案 2 :(得分:0)

尝试include_once()

但是,为什么不在topictweets.php中有一个循环?

你可以在这个页面中进行查询等,但是然后在include

中循环它
相关问题