2013-02-01 3 views
0

그래, 문제가있다. 뉴스 피드를 설정하고 싶습니다. 그러면 사용자가 배치 한 시간까지 주문한 댓글과 댓글이 모두 표시됩니다. 나는이 모든 것을 할 수있는 함수를 만들었다. 다시 말하지만 그것이 가장 효율적인지는 모르겠지만 단지 효과를 얻고 싶습니다. 그래서 내가내 어레이와 관련된 문제

public function foo() 
{ 
    //Retrieve all of the questions 
    $Statement = $this->Database->prepare("SELECT * FROM table WHERE condition = ?"); 
    $Statement->execute(array($this->variable)); 

    while ($row = $Statement->fetch(PDO::FETCH_ASSOC)) 
    { 
     $id[]   = $row["id"]; 
     $question[]  = $row["question"]; 
     $asker[]  = $row["asker"]; 
     $timestamp[] = $row["timestamp"]; 
     $likes[]  = $row["likes"]; 
    } 

    $iLimit = count($id); //Set a limit based on the size of the array 

    if ($iLimit > 0) 
    { 
        //Save everything in an array for the questions 
     for ($iCount = 0; $iCount < $iLimit; $iCount++) 
     { 
      $question[$iCount] = Array ("id"   => $id[$iCount], 
             "text"   => $question[$iCount], 
             "username"  => $asker[$iCount], 
             "timestamp" => $timestamp[$iCount], 
             "likes"  => $likes[$iCount]); 
     } 
    } 

    //Retrieve all of the comments 
    $Statement = $this->Database->prepare("SELECT * FROM table WHERE condition = ?"); 
    $Statement->execute(array($this->variable)); 

    while ($row = $Statement->fetch(PDO::FETCH_ASSOC)) 
    { 
     $id[]   = $row["id"]; 
     $comment[]  = $row["comment"]; 
     $commenter[] = $row["commenter"]; 
     $timestamp[] = $row["timestamp"]; 
     $likes[]  = $row["likes"]; 
    } 

    $iLimit = count($id); //Set a limit based on the size of the array 

    if ($iLimit > 0) 
    { 
        //Save everything in an array for comments 
     for ($iCount = 0; $iCount < $iLimit; $iCount++) 
     { 
      $comment[$iCount] = Array ("id"   => $id[$iCount], 
             "text"   => $comment[$iCount], 
             "username"  => $commenter[$iCount], 
             "timestamp"  => $timestamp[$iCount], 
             "likes"   => $likes[$iCount]); 
     } 
    } 

    //Merge the two arrays 
    $aNewsFeed = array_merge($question, $comment); 

    foreach ($aNewsFeed as $row) 
    { 
     $aOrdered[] = $row["timestamp"]; 
    } 

    array_multisort($aOrdered, SORT_DESC, $aNewsFeed); //Sort the array 

    return $aNewsFeed; 
} //end getNewsFeed 

그때 나는 foreach 루프

foreach ($Class->foo() as $news) 
{ 
    echo $news["text"] 
} 

그러나 항상 foreach 루프의 두 개의 추가 빈 실행을 통해의를 제공 할 때마다 사용하여 전화를 가지고있는 것입니다. 또는 실제로 배열의 일부인지 모르겠다. foreach 루프에 문제가있는 경우 일반적으로 오류가 발생하기 때문입니다. array_merge() 함수에 문제가 있다고 생각하고 있지만 완전히 확실하지는 않습니다. 어떤 아이디어?

도움 주셔서 감사합니다. 정말 감사.

+1

명백한 질문 : 당신은 (재) 사용하기 전에

당신은 항상 당신의 배열을 초기화한다 : //php.net/manual/en/function.array-merge.php)와'$ array1 + $ array2'? – ficuscr

답변

0

당신이 등의 임시 변수를 배열 $id, $likes를 사용하는 것처럼 보이지만 당신이 코멘트를 사용하기 시작할 때 그래서 당신의 $id 배열이 그 질문을해야합니다 그들에게 두 번째 시간을 사용하기 전에 당신이 그들을 재설정되지 않습니다 . 당신이 [`array_merge`]의 차이를 이해한다됩니다 같아요 자기 (HTTP를 물어

$id = array(); 
// first loop 

$id = array(); 
// second loop 
+1

나는 그것을 잡지 못했다고 나는 믿을 수 없다. 나는 좌절감을 느끼고 있었지만 꽤 분명했습니다. 도와 주셔서 감사합니다. – Alex

0
foreach ($Class->foo() as $news) 
{ 
    if($news["text"]!=""){ 
     echo $news["text"]; 
    } 
} 
+0

나는 그것을 고칠 것이라고 알고 있지만 실제로 문제를 해결하지는 않습니다. 그래도 입력 주셔서 감사합니다. – Alex