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() 函数的问题,但我不完全确定。有任何想法吗?

感谢您的帮助。对此,我真的非常感激。

4

2 回答 2

0

似乎您正在使用数组$id,$likes等作为临时变量,但在第二次使用它们之前您没有重置它们,因此$id当您开始使用它进行评论时,您的数组中会有问题。

在您(重新)使用它们之前,您应该始终初始化您的数组:

$id = array();
// first loop

$id = array();
// second loop
于 2013-02-01T21:51:02.317 回答
0
foreach ($Class->foo() as $news) 
{
    if($news["text"]!=""){
        echo $news["text"];
    }
}
于 2013-02-01T21:51:15.683 回答