0

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

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

topictweets.php 中的这段代码可以自己正常工作,但我想在每个论坛上重复它。没有包含的 foreach 循环可以正常工作。我还尝试将主题 tweets.php 中的代码清楚地放在 foreach 循环中,这当然也不起作用。

它包含的代码用于从数据库中获取论坛的主题并查找相关的推文,并将其保存在数据库中。

还有其他方法可以做到这一点吗?

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);
}


?>
4

3 回答 3

1

在循环中“粘贴”一堆代码并不是一个好习惯。实际上,您要查找的是函数或已定义类的使用。因此,如果可以,请在您的函数中定义一个topictweets.php包含您的代码并在循环中使用它的函数:

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

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

    /////////



    ////////
}
于 2013-04-01T21:13:19.300 回答
0

这应该可以正常工作:

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

你只需要include一次。

于 2013-04-01T21:07:43.763 回答
0

尝试 include_once()

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

您可以在此页面中进行查询等,然后在包含中循环遍历它

于 2013-04-01T21:08:22.393 回答