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 ループの 2 つの追加の空白ランスルーが常に提供されます。または、実際に配列の一部であるかどうかはわかりません。通常、foreach ループに問題がある場合はエラーが送信されるためです。array_merge() 関数に問題があるに違いないと思っていますが、完全にはわかりません。何か案は?

お手伝いありがとう。ほんとうにありがとう。

4

2 に答える 2

0

配列$id$likesなどを一時変数として使用しているようですが、2 回目に使用する前にそれらをリセットしていない$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 に答える