わかりました、問題があります。ニュース フィードを設定したいのですが、これにより、ユーザーが受け取った質問とコメントの両方が、配置された時点で並べ替えられて表示されます。これらすべてを行う関数を設定しました。繰り返しますが、それが最も効率的かどうかはわかりませんが、機能させたいだけです。だからこれは私が持っているものです
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() 関数に問題があるに違いないと思っていますが、完全にはわかりません。何か案は?
お手伝いありがとう。ほんとうにありがとう。