2013-04-01 5 views
0

foreach 루프에서 (긴) 코드를 사용할 수없는 이유를 아는 사람이 있습니까?foreach 루프가 포함되지 않는 경우

foreach 루프의 코드는 한 번만 실행됩니다.

이 코드는 topictweets.php에서 잘 작동하지만 각 포럼에서이를 반복하고 싶습니다. include 루프없이 foreach 루프가 제대로 작동합니다. foreach 루프에서 짹짹이 주제의 코드를 분명히하려고했으나 물론 작동하지 않았습니다.

포함 된 코드는 데이터베이스에서 포럼 주제를 얻고 관련 트윗을 찾아 데이터베이스에 저장하는 데 사용됩니다.

다른 방법이 있습니까?

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

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

    include 'topictweets.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

tweets.php 항목 코드 http://oudhollandsedrop.nl/webendata/FeedForum/fetchtweets.php

무리 :

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

DB에 먼저 연결 (포함하기 전에)합니까, 아니면 포함 된 파일에서 연결 등을합니까? 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

그러면 문제는 알고리즘 적으로 보인다 : 새로운'topictweets' 함수에서 $ fID를 잘 사용하고 있습니까? 루프의 반복에 따라 코드가 올바른 포럼을 목표로하고 있는지 확인해야합니다. – blint

관련 문제